]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
New architecture for equationview. All interactions between components are defined...
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 13 Jan 2010 13:55:24 +0000 (13:55 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 13 Jan 2010 13:55:24 +0000 (13:55 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13498 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionComposite.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionController.java [deleted file]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionTypeSelector.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ShortcutTabs.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/UnitSelector.java

index f3ac8a384705a667b8f06dc3eee66f109e2a57f3..1819e6ffb115e5aebe4c6456785a639d80aeeb0c 100644 (file)
@@ -1,10 +1,6 @@
 package org.simantics.sysdyn.ui.equation;\r
 \r
 \r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-\r
 import org.eclipse.jface.layout.GridDataFactory;\r
 import org.eclipse.jface.layout.GridLayoutFactory;\r
 import org.eclipse.jface.viewers.ISelection;\r
@@ -14,13 +10,13 @@ import org.eclipse.swt.events.FocusEvent;
 import org.eclipse.swt.events.FocusListener;\r
 import org.eclipse.swt.events.MouseEvent;\r
 import org.eclipse.swt.events.MouseListener;\r
+import org.eclipse.swt.events.SelectionEvent;\r
+import org.eclipse.swt.events.SelectionListener;\r
 import org.eclipse.swt.graphics.Font;\r
 import org.eclipse.swt.graphics.Point;\r
 import org.eclipse.swt.layout.RowLayout;\r
 import org.eclipse.swt.widgets.Composite;\r
 import org.eclipse.swt.widgets.Label;\r
-import org.eclipse.swt.widgets.TabFolder;\r
-import org.eclipse.swt.widgets.TabItem;\r
 import org.eclipse.swt.widgets.Table;\r
 import org.eclipse.swt.widgets.TableItem;\r
 import org.eclipse.swt.widgets.Text;\r
@@ -34,9 +30,7 @@ import org.simantics.db.Resource;
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.db.procedure.Listener;\r
 import org.simantics.db.request.Read;\r
-import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.ui.SimanticsUI;\r
-import org.simantics.utils.ui.ExceptionUtils;\r
 \r
 public class EquationView extends ViewPart implements ISelectionListener {\r
 \r
@@ -46,39 +40,23 @@ public class EquationView extends ViewPart implements ISelectionListener {
     Resource variable;\r
     Resource configuration;\r
     \r
-    ArrayList<String> units;\r
-    \r
     boolean disposed = false;\r
+    org.eclipse.ui.IPartListener2 focusLostListener;   \r
     \r
-    Composite parent;\r
+    ShortcutTabs shortcutTabs;\r
+    ExpressionTypeSelector expressionController;    \r
+    Composite unitComposite;\r
+    UnitSelector unitSelector;    \r
+    ExpressionComposite expressionComposite;      \r
 \r
+    // Dummies\r
     Text nameText;\r
-\r
-    \r
-\r
+    Composite unitsAndRange;  \r
     Composite emptyComposite;\r
-    String originalText;\r
-    TabFolder tabFolder;\r
-    TabItem variables;\r
-    Table variableTable;\r
-    TabItem functions;\r
-    \r
-    Composite unitsAndRange;\r
     \r
-    Composite typeComposite;\r
-    ExpressionController expressionController;\r
     \r
-    Composite unitComposite;\r
-    UnitSelector unitSelector;\r
-    \r
-    Composite equationComposite;\r
-    \r
-    org.eclipse.ui.IPartListener2 focusLostListener;\r
-\r
     @Override    \r
     public void createPartControl(Composite parent) {\r
-        this.parent = parent;\r
-        this.units = new ArrayList<String>();\r
         // Listeners\r
         getSite().getPage().addPostSelectionListener(this);\r
         focusLostListener = new org.eclipse.ui.IPartListener2()\r
@@ -91,18 +69,14 @@ public class EquationView extends ViewPart implements ISelectionListener {
             {\r
                 if(partRef.getPart(false) instanceof EquationView) {\r
                     // Save expressions\r
-                    if(expressionController != null) {\r
-                        expressionController.save();\r
+                    if(expressionComposite != null) {\r
+                        expressionComposite.save();\r
                     }\r
 \r
                     \r
                     // Save unit\r
                     if(unitSelector != null) {\r
-                        String unitNew = unitSelector.save();                       \r
-                        if(unitNew != null && !unitNew.equals("") && !units.contains(unitNew)) {\r
-                            units.add(unitNew);\r
-                            Collections.sort(units);\r
-                        }\r
+                        unitSelector.save();                       \r
                     }\r
                 }\r
 \r
@@ -113,7 +87,7 @@ public class EquationView extends ViewPart implements ISelectionListener {
         };\r
         getViewSite().getPage().addPartListener(focusLostListener);\r
 \r
-        GridLayoutFactory.fillDefaults().numColumns(4).margins(5, 5).applyTo(parent);\r
+        GridLayoutFactory.fillDefaults().numColumns(5).margins(3, 3).applyTo(parent);\r
 \r
 \r
 \r
@@ -122,81 +96,22 @@ public class EquationView extends ViewPart implements ISelectionListener {
         nameText.setFont(FONT);\r
         GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText);\r
 \r
-        // After the variable name: Variable type\r
-        Composite variableType = new Composite(parent, SWT.NONE);\r
-        GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 0).applyTo(variableType);\r
-        GridDataFactory.fillDefaults().applyTo(variableType);\r
-        \r
-        typeComposite = new Composite(variableType, SWT.NONE);\r
-        GridDataFactory.fillDefaults().applyTo(typeComposite);\r
-        GridLayoutFactory.fillDefaults().applyTo(typeComposite);\r
-        expressionController = new ExpressionController(typeComposite, SWT.NONE, variable, equationComposite);\r
+       \r
+        // Upper part of the view: Expression type\r
+        expressionController = new ExpressionTypeSelector(parent, SWT.NONE);\r
+        GridDataFactory.fillDefaults().applyTo(expressionController);\r
+        addVariableTypeSelectionListener();\r
         \r
-        unitComposite = new Composite(variableType, SWT.NONE);\r
+        // Upper part of the view: Unit input/selection\r
+        unitComposite = new Composite(parent, SWT.NONE);\r
         GridDataFactory.fillDefaults().applyTo(unitComposite);\r
         GridLayoutFactory.fillDefaults().applyTo(unitComposite);\r
-        units = getUnits();\r
-        unitSelector = new UnitSelector(unitComposite, SWT.NONE, variable, units);\r
+        unitSelector = new UnitSelector(unitComposite, SWT.NONE);\r
  \r
-        // Center side of the view: (related) variables and possible functions\r
-        TabFolder tabFolder = new TabFolder (parent, SWT.NONE);\r
-        GridDataFactory.fillDefaults().span(1, 4).applyTo(tabFolder);\r
-        variables = new TabItem(tabFolder, SWT.NULL);\r
-        variables.setText("Variables");\r
-        variableTable = new Table (tabFolder, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);\r
-        variableTable.addMouseListener(new MouseListener(){\r
-\r
-            @Override\r
-            public void mouseDoubleClick(MouseEvent e) {\r
-                TableItem item = variableTable.getItem(new Point(e.x, e.y));\r
-                if(item != null) {\r
-                    final String var = item.getText();\r
-                    equationComposite.getDisplay().asyncExec(new Runnable() {\r
-\r
-                        @Override\r
-                        public void run() {\r
-                            expressionController.getExpressionViewFactor().replaceSelection(var);\r
-                        }\r
-                    });\r
-                }\r
-            }\r
-\r
-            @Override\r
-            public void mouseDown(MouseEvent e) {\r
-                expressionController.getExpressionViewFactor().focus();\r
-            }\r
-\r
-            @Override\r
-            public void mouseUp(MouseEvent e) {\r
-                expressionController.getExpressionViewFactor().focus();\r
-            }\r
-\r
-        });\r
-\r
-        variableTable.addFocusListener(new FocusListener() {\r
-\r
-            @Override\r
-            public void focusGained(FocusEvent e) {    \r
-            }\r
-\r
-            @Override\r
-            public void focusLost(FocusEvent e) {\r
-                variableTable.deselectAll();\r
-            }\r
-\r
-        });\r
-        variables.setControl(variableTable);\r
-\r
-        functions = new TabItem(tabFolder, SWT.NULL);\r
-        functions.setText("Functions"); \r
-        Table table = new Table (tabFolder, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);\r
-        TableItem item = new TableItem(table, SWT.NONE);\r
-        item.setText("min");\r
-        item = new TableItem(table, SWT.NONE);\r
-        item.setText("max");\r
-        item = new TableItem(table, SWT.NONE);\r
-        item.setText("abs");\r
-        functions.setControl(table);\r
+        // Center of the view: variables and possible functions\r
+        shortcutTabs = new ShortcutTabs(parent, SWT.NONE);\r
+        GridDataFactory.fillDefaults().span(1, 4).applyTo(shortcutTabs);\r
+        addVariableTableListeners();\r
 \r
         // Right side of the view: Details, comments\r
         emptyComposite = new Composite(parent, SWT.NONE);\r
@@ -204,14 +119,13 @@ public class EquationView extends ViewPart implements ISelectionListener {
         GridLayoutFactory.fillDefaults().applyTo(emptyComposite);\r
 \r
         // Below variable name: Equation\r
-        // equationComposite content is controlled by ExpressionController \r
-        equationComposite = new Composite(parent, SWT.NONE);\r
-        GridDataFactory.fillDefaults().span(2, 1).grab(true, true).applyTo(equationComposite);\r
-        GridLayoutFactory.fillDefaults().applyTo(equationComposite);\r
+        expressionComposite = new ExpressionComposite(parent, SWT.NONE);\r
+        GridDataFactory.fillDefaults().span(3, 1).grab(true, true).applyTo(expressionComposite);\r
+        \r
 \r
         // Lower part of the view: Units and Range\r
         unitsAndRange = new Composite(parent, SWT.NONE); \r
-        GridDataFactory.fillDefaults().span(2, 1).applyTo(unitsAndRange);\r
+        GridDataFactory.fillDefaults().span(3, 1).applyTo(unitsAndRange);\r
         RowLayout layout = new RowLayout();\r
         layout.spacing = 3;\r
         unitsAndRange.setLayout(layout);\r
@@ -231,9 +145,7 @@ public class EquationView extends ViewPart implements ISelectionListener {
     }   \r
 \r
     static class Auxiliary {\r
-        ArrayList<String> dependencies;\r
         String name;\r
-        String type;\r
         Resource configuration;\r
     }\r
 \r
@@ -252,13 +164,6 @@ public class EquationView extends ViewPart implements ISelectionListener {
             variable = resource;\r
             a.configuration = graph.getPossibleObject(variable, b.PartOf);\r
             a.name = graph.getRelatedValue(resource, b.HasName);\r
-            a.dependencies = getDependencies(resource);\r
-            SysdynResource sr = SysdynResource.getInstance(graph);\r
-            if (graph.isInstanceOf(variable, sr.Auxiliary) || graph.isInstanceOf(variable, sr.Valve))\r
-                a.type = "NORMAL";\r
-            else if (graph.isInstanceOf(variable, sr.Stock))\r
-                a.type = "STOCK";\r
-\r
             return a;\r
         }\r
     }\r
@@ -277,38 +182,35 @@ public class EquationView extends ViewPart implements ISelectionListener {
 \r
                                 @Override\r
                                 public void exception(Throwable t) {\r
-                                    // TODO Auto-generated method stub\r
 \r
                                 }\r
 \r
                                 @Override\r
                                 public void execute(final Auxiliary result) {\r
-                                    equationComposite.getDisplay().asyncExec(new Runnable() {\r
+                                    expressionComposite.getDisplay().asyncExec(new Runnable() {\r
 \r
                                         @Override\r
                                         public void run(){\r
-\r
-                                            variableTable.removeAll();\r
-                                            TableItem item;\r
-                                            for(String d : result.dependencies) {\r
-                                                item = new TableItem(variableTable, SWT.NONE);\r
-                                                item.setText(d);\r
-                                            }\r
                                             \r
-                                            if(unitSelector!= null) unitSelector.dispose();\r
                                             if(configuration == null || !configuration.equals(result.configuration)) {\r
                                                 configuration = result.configuration;\r
-                                                units = getUnits();\r
+                                                if(unitSelector!= null) {\r
+                                                    unitSelector.dispose();\r
+                                                    unitSelector = new UnitSelector(unitComposite, SWT.NONE);\r
+                                                }\r
+                                            } else {\r
+                                                unitSelector.updateUnits(variable);\r
                                             }\r
-                                            unitSelector = new UnitSelector(unitComposite, SWT.NONE, variable, units);\r
-                                            unitComposite.layout();\r
                                             \r
-                                            if(expressionController!= null) expressionController.dispose();\r
-                                            expressionController = new ExpressionController(typeComposite, SWT.NONE, variable, equationComposite);\r
-                                            typeComposite.layout();\r
+                                            String expressionType = expressionComposite.resetExpressionView(variable);\r
+                                            expressionController.setExpressionTypes(expressionComposite.getExpressionTypes());  \r
+                                            expressionController.select(expressionType);\r
                                             \r
                                             nameText.setText(result.name);\r
                                             \r
+                                            if(shortcutTabs != null) {\r
+                                                shortcutTabs.updateTables(variable);\r
+                                            }\r
                                         }\r
                                     });      \r
 \r
@@ -327,8 +229,8 @@ public class EquationView extends ViewPart implements ISelectionListener {
 \r
     @Override\r
     public void setFocus() {\r
-        if(expressionController.getExpressionViewFactor() != null)\r
-            expressionController.getExpressionViewFactor().focus();\r
+        if(expressionComposite != null)\r
+           expressionComposite.focus();\r
     }\r
     \r
     @Override\r
@@ -337,84 +239,68 @@ public class EquationView extends ViewPart implements ISelectionListener {
         getViewSite().getPage().removePartListener(focusLostListener);\r
         super.dispose();\r
     }\r
+   \r
     \r
-    // Returns all the used units in the model\r
-    private ArrayList<String> getUnits() {\r
-        ArrayList<String> units = new ArrayList<String>();\r
-        if(configuration == null)\r
-            return units;\r
-        \r
-        try {\r
-            units = SimanticsUI.getSession().syncRequest(new Read<ArrayList<String>>() {\r
+    private void addVariableTypeSelectionListener() { \r
+        expressionController.getTypeCombo().addSelectionListener(new SelectionListener() {\r
 \r
-                @Override\r
-                public ArrayList<String> perform(ReadGraph graph)\r
-                throws DatabaseException {\r
-                    ArrayList<String> units = new ArrayList<String>();\r
-                    \r
-                    SysdynResource sr = SysdynResource.getInstance(graph);\r
-                    Builtins b = graph.getBuiltins();\r
-                    //Resource model = graph.getPossibleObject(variable, b.PartOf);\r
-                    Resource model = configuration;\r
-                    if (model != null) {\r
-                        Collection<Resource> variables = graph.getObjects(model, b.ConsistsOf);\r
-                        for(Resource v : variables) {\r
-                            Object unit = graph.getPossibleRelatedValue(v, sr.HasUnit);\r
-                            if (unit != null && !units.contains((String) unit)) {\r
-                                units.add((String)unit);\r
-                            }\r
-                        }\r
-                    }\r
+            @Override\r
+            public void widgetSelected(SelectionEvent e) {\r
+                expressionComposite.displayExpressionView(expressionController.getSelecetedType());\r
+            }\r
 \r
-                    return units;\r
+            @Override\r
+            public void widgetDefaultSelected(SelectionEvent e) {\r
+            }\r
+        });\r
+    }\r
+    \r
+    private void addVariableTableListeners() {\r
+        final Table variableTable = this.shortcutTabs.getVariableTable();\r
+        variableTable.addMouseListener(new MouseListener(){\r
 \r
-                }\r
+            @Override\r
+            public void mouseDoubleClick(MouseEvent e) {\r
+                TableItem item = variableTable.getItem(new Point(e.x, e.y));\r
+                if(item != null) {\r
+                    final String var = item.getText();\r
+                    variableTable.getDisplay().asyncExec(new Runnable() {\r
 \r
+                        @Override\r
+                        public void run() {\r
+                            expressionComposite.replaceSelection(var);\r
+                        }\r
+                    });\r
+                }\r
+            }\r
 \r
-            });\r
-        } catch (DatabaseException ee) {\r
-            ExceptionUtils.logAndShowError(ee);\r
-        }\r
-        return units;\r
-    }\r
+            @Override\r
+            public void mouseDown(MouseEvent e) {\r
+                expressionComposite.focus();\r
+            }\r
 \r
-    // Returns the names of the related variables (dependencies)\r
-    private ArrayList<String> getDependencies(final Resource r){\r
-        ArrayList<String> variables = null;\r
-        try {\r
-            variables = SimanticsUI.getSession().syncRequest(new Read<ArrayList<String>>() {\r
-\r
-                @Override\r
-                public ArrayList<String> perform(ReadGraph graph)\r
-                throws DatabaseException {\r
-                    ArrayList<String> variables = new ArrayList<String>();\r
-                    SysdynResource sr = SysdynResource.getInstance(graph);\r
-                    Builtins b = graph.getBuiltins();\r
-\r
-                    Collection<Resource> dependencies = graph.getObjects(r, sr.IsHeadOf);\r
-\r
-                    for(Resource d : dependencies) {\r
-                        if(graph.isInstanceOf(d, sr.Dependency)) {\r
-                            Resource tail = graph.getPossibleObject(d, sr.HasTail);\r
-                            if(tail != null) {\r
-                                Object name = graph.getPossibleRelatedValue(tail, b.HasName);\r
-                                if(name != null)\r
-                                    variables.add((String)name);\r
-                            }\r
-                        }\r
-                    }\r
+            @Override\r
+            public void mouseUp(MouseEvent e) {\r
+                expressionComposite.focus();\r
+            }\r
 \r
-                    return variables;\r
+        });\r
 \r
-                }\r
+        variableTable.addFocusListener(new FocusListener() {\r
 \r
+            @Override\r
+            public void focusGained(FocusEvent e) {    \r
+            }\r
 \r
-            });\r
-        } catch (DatabaseException ee) {\r
-            ExceptionUtils.logAndShowError(ee);\r
-        }\r
+            @Override\r
+            public void focusLost(FocusEvent e) {\r
+                variableTable.deselectAll();\r
+            }\r
 \r
-        return variables;\r
+        });\r
     }\r
+    \r
+\r
+\r
 \r
 }
\ No newline at end of file
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionComposite.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionComposite.java
new file mode 100644 (file)
index 0000000..bb5bc09
--- /dev/null
@@ -0,0 +1,220 @@
+package org.simantics.sysdyn.ui.equation;\r
+\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import org.eclipse.jface.layout.GridLayoutFactory;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Control;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.ui.equation.expressions.AuxiliaryExpressionViewFactor;\r
+import org.simantics.sysdyn.ui.equation.expressions.ConstantExpressionViewFactor;\r
+import org.simantics.sysdyn.ui.equation.expressions.DelayExpressionViewFactor;\r
+import org.simantics.sysdyn.ui.equation.expressions.IExpressionViewFactor;\r
+import org.simantics.sysdyn.ui.equation.expressions.LookupExpressionViewFactor;\r
+import org.simantics.sysdyn.ui.equation.expressions.ParameterExpressionViewFactor;\r
+import org.simantics.sysdyn.ui.equation.expressions.StockExpressionViewFactor;\r
+import org.simantics.sysdyn.ui.equation.expressions.WithLookupExpressionViewFactor;\r
+import org.simantics.ui.SimanticsUI;\r
+\r
+public class ExpressionComposite extends Composite {\r
+\r
+    static enum ExpressionType {Auxiliary, Parameter, Constant, Lookup, WithLookup, Stock, Delay};\r
+\r
+    ExpressionType[] auxiliaryExpressions = new ExpressionType[] {\r
+            ExpressionType.Parameter, \r
+            ExpressionType.Auxiliary, \r
+            ExpressionType.Constant, \r
+            ExpressionType.Lookup, \r
+            ExpressionType.WithLookup};\r
+\r
+    ExpressionType[] valveExpressions = new ExpressionType[] {\r
+            ExpressionType.Auxiliary, \r
+            ExpressionType.Parameter, \r
+            ExpressionType.Constant, \r
+            ExpressionType.WithLookup};\r
+\r
+    ExpressionType[] stockExpressions = new ExpressionType[] {\r
+            ExpressionType.Stock, \r
+            ExpressionType.Delay};\r
+\r
+\r
+    IExpressionViewFactor expressionViewFactor;\r
+    Map<String, Object> data;\r
+    Map<String, Object> origData;\r
+    Resource variable;\r
+    ArrayList<String> dependencies;\r
+\r
+    public ExpressionComposite(Composite parent, int style) {\r
+        super(parent, style);\r
+       \r
+        GridLayoutFactory.fillDefaults().applyTo(this);\r
+        \r
+        this.data = new HashMap<String, Object>();\r
+        this.origData = new HashMap<String, Object>();\r
+    }\r
+\r
+    public void save() {\r
+        if(variable != null && data != null)\r
+            this.expressionViewFactor.writeData(variable, data);\r
+    }\r
+\r
+    public void displayExpressionView(String expressionType) {\r
+        displayExpressionView(expressionType, false);\r
+    }\r
+\r
+    public String resetExpressionView(Resource variable) {\r
+        data.clear();\r
+        origData.clear();\r
+        this.variable = variable;\r
+        this.expressionViewFactor = null;\r
+        String origExpressionType = getOriginalExpressionType().toString();\r
+        displayExpressionView(origExpressionType, true);\r
+        return origExpressionType;\r
+    }\r
+\r
+\r
+    public void displayExpressionView(String expressionType, boolean original) {\r
+        if(expressionType == null)\r
+            return;\r
+\r
+        if(this.expressionViewFactor != null) expressionViewFactor.updateData(data);\r
+        ExpressionType et = ExpressionType.valueOf(expressionType);\r
+        IExpressionViewFactor evf = null;\r
+        switch (et) {\r
+            case Auxiliary: \r
+                evf = new AuxiliaryExpressionViewFactor(); break;\r
+            case Parameter: \r
+                evf = new ParameterExpressionViewFactor(); break;\r
+            case Constant: \r
+                evf = new ConstantExpressionViewFactor(); break;\r
+            case Lookup: \r
+                evf = new LookupExpressionViewFactor(); break;\r
+            case WithLookup: \r
+                evf = new WithLookupExpressionViewFactor(); break;\r
+            case Stock: \r
+                evf = new StockExpressionViewFactor(); break;\r
+            case Delay: \r
+                evf = new DelayExpressionViewFactor(); break;\r
+            default: ;\r
+        }\r
+\r
+        if (evf != null) {\r
+            for(Control c : this.getChildren()) {\r
+                c.dispose();\r
+            } \r
+\r
+            if(original) {\r
+                evf.readData(variable, data);\r
+                for(String key : data.keySet()) {\r
+                    origData.put(key, data.get(key));\r
+                }\r
+            }\r
+             \r
+            evf.createView(this, data);\r
+            this.expressionViewFactor = evf;\r
+            this.layout();\r
+        }  \r
+    }\r
+\r
+\r
+    public ExpressionType[] getExpressionTypes() {\r
+        ExpressionType[] expressionTypes = null;\r
+\r
+        if(variable != null) {\r
+            try {\r
+                expressionTypes = SimanticsUI.getSession().syncRequest(new Read< ExpressionType[]>() {\r
+\r
+                    @Override\r
+                    public  ExpressionType[] perform(ReadGraph graph) throws DatabaseException {\r
+                        SysdynResource sr = SysdynResource.getInstance(graph);\r
+                        ExpressionType[] expressionTypes;\r
+                        if(graph.isInstanceOf(variable, sr.Auxiliary)) {\r
+                            expressionTypes = auxiliaryExpressions;\r
+                        }\r
+                        else if(graph.isInstanceOf(variable, sr.Valve)) {\r
+                            expressionTypes = valveExpressions;\r
+                        }\r
+                        else if(graph.isInstanceOf(variable, sr.Stock)) {\r
+                            expressionTypes = stockExpressions;\r
+                        }\r
+                        else\r
+                            expressionTypes = null;\r
+\r
+                        return expressionTypes;\r
+                    }\r
+                });\r
+            } catch (DatabaseException e1) {\r
+                e1.printStackTrace();\r
+            }\r
+        }\r
+\r
+        return expressionTypes;\r
+    }\r
+    \r
+    public void focus() {\r
+        if(this.expressionViewFactor != null)\r
+            this.expressionViewFactor.focus();\r
+    }\r
+    \r
+    public void replaceSelection(String var) {\r
+        this.expressionViewFactor.replaceSelection(var);\r
+    }\r
+    \r
+    private ExpressionType getOriginalExpressionType() {\r
+\r
+        ExpressionType et = null;\r
+\r
+        try {\r
+            et = SimanticsUI.getSession().syncRequest(new Read< ExpressionType>() {\r
+\r
+                @Override\r
+                public  ExpressionType perform(ReadGraph graph) throws DatabaseException {\r
+\r
+                    ExpressionType et = null;\r
+                    SysdynResource sr = SysdynResource.getInstance(graph);\r
+                    Resource expression = graph.getPossibleObject(variable, sr.HasExpression);\r
+                    if(expression == null) {\r
+                        if(graph.isInstanceOf(variable, sr.Auxiliary)) {\r
+                            et = ExpressionType.Auxiliary;\r
+                        }\r
+                        else if(graph.isInstanceOf(variable, sr.Valve)) {\r
+                            et = ExpressionType.Auxiliary;\r
+                        }\r
+                        else if(graph.isInstanceOf(variable, sr.Stock)) {\r
+                            et = ExpressionType.Stock;\r
+                        }\r
+                    } else {\r
+\r
+                        if(graph.isInstanceOf(expression, sr.NormalExpression)) {\r
+                            et = ExpressionType.Auxiliary;\r
+                        } else if (graph.isInstanceOf(expression, sr.StockExpression)) {\r
+                            et = ExpressionType.Stock;\r
+                        } else if (graph.isInstanceOf(expression, sr.ParameterExpression)) {\r
+                            et = ExpressionType.Parameter;\r
+                        } else if (graph.isInstanceOf(expression, sr.ConstantExpression)) {\r
+                            et = ExpressionType.Constant;\r
+                        } else if (graph.isInstanceOf(expression, sr.DelayExpression)) {\r
+                            et = ExpressionType.Delay;\r
+                        } else if (graph.isInstanceOf(expression, sr.LookupExpression)) {\r
+                            et = ExpressionType.Lookup;\r
+                        } else if (graph.isInstanceOf(expression, sr.WithLookupExpression)) {\r
+                            et = ExpressionType.WithLookup;\r
+                        } else {\r
+                            et =  ExpressionType.Auxiliary;\r
+                        }\r
+                    }\r
+                    return et;\r
+                }\r
+            });\r
+        } catch (DatabaseException e) {\r
+            e.printStackTrace();\r
+        }\r
+        return et;\r
+    }\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionController.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionController.java
deleted file mode 100644 (file)
index d331d23..0000000
+++ /dev/null
@@ -1,229 +0,0 @@
-package org.simantics.sysdyn.ui.equation;\r
-\r
-import java.util.HashMap;\r
-import java.util.Map;\r
-\r
-import org.eclipse.jface.layout.GridDataFactory;\r
-import org.eclipse.jface.layout.GridLayoutFactory;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.events.SelectionEvent;\r
-import org.eclipse.swt.events.SelectionListener;\r
-import org.eclipse.swt.graphics.FontMetrics;\r
-import org.eclipse.swt.graphics.GC;\r
-import org.eclipse.swt.widgets.Combo;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.swt.widgets.Control;\r
-import org.eclipse.swt.widgets.Label;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.exception.ServiceException;\r
-import org.simantics.db.request.Read;\r
-import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.sysdyn.ui.equation.expressions.AuxiliaryExpressionViewFactor;\r
-import org.simantics.sysdyn.ui.equation.expressions.ConstantExpressionViewFactor;\r
-import org.simantics.sysdyn.ui.equation.expressions.DelayExpressionViewFactor;\r
-import org.simantics.sysdyn.ui.equation.expressions.IExpressionViewFactor;\r
-import org.simantics.sysdyn.ui.equation.expressions.LookupExpressionViewFactor;\r
-import org.simantics.sysdyn.ui.equation.expressions.ParameterExpressionViewFactor;\r
-import org.simantics.sysdyn.ui.equation.expressions.StockExpressionViewFactor;\r
-import org.simantics.sysdyn.ui.equation.expressions.WithLookupExpressionViewFactor;\r
-import org.simantics.ui.SimanticsUI;\r
-\r
-public class ExpressionController extends Composite {\r
-\r
-    Combo typeCombo;\r
-    Resource variable;\r
-    Map<String, Object> data;\r
-    Map<String, Object> origData;\r
-    Composite equationComposite;\r
-    IExpressionViewFactor expressionViewFactor;\r
-\r
-    //private enum VariableClass {Auxiliary, Valve, Stock};\r
-    private enum ExpressionType {Auxiliary, Parameter, Constant, Lookup, WithLookup, Stock, Delay};\r
-\r
-    ExpressionType[] auxiliaryExpressions = new ExpressionType[] {\r
-            ExpressionType.Parameter, \r
-            ExpressionType.Auxiliary, \r
-            ExpressionType.Constant, \r
-            ExpressionType.Lookup, \r
-            ExpressionType.WithLookup};\r
-\r
-    ExpressionType[] valveExpressions = new ExpressionType[] {\r
-            ExpressionType.Auxiliary, \r
-            ExpressionType.Parameter, \r
-            ExpressionType.Constant, \r
-            ExpressionType.WithLookup};\r
-\r
-    ExpressionType[] stockExpressions = new ExpressionType[] {\r
-            ExpressionType.Stock, \r
-            ExpressionType.Delay};\r
-\r
-    ExpressionType[] expressionTypes;\r
-    ExpressionType originalExpressionType;\r
-\r
-    HashMap<String, Map<String, Object>> expressions = new HashMap<String,  Map<String, Object>>();\r
-\r
-    public ExpressionController(final Composite parent, int style, final Resource variable, Composite equationComposite) {\r
-        super(parent, style);\r
-        this.variable = variable;\r
-        this.equationComposite = equationComposite;\r
-        this.expressionViewFactor = null;\r
-        this.data = new HashMap<String, Object>();\r
-        this.origData = new HashMap<String, Object>();\r
-        GridDataFactory.fillDefaults().applyTo(this);\r
-        GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(this);\r
-\r
-        if(variable != null) {\r
-            try {\r
-                expressionTypes = SimanticsUI.getSession().syncRequest(new Read< ExpressionType[]>() {\r
-\r
-                    @Override\r
-                    public  ExpressionType[] perform(ReadGraph graph) throws DatabaseException {\r
-                        SysdynResource sr = SysdynResource.getInstance(graph);\r
-                        ExpressionType[] expressionTypes;\r
-                        if(graph.isInstanceOf(variable, sr.Auxiliary)) {\r
-                            expressionTypes = auxiliaryExpressions;\r
-                            originalExpressionType = ExpressionType.Auxiliary;\r
-                        }\r
-                        else if(graph.isInstanceOf(variable, sr.Valve)) {\r
-                            expressionTypes = valveExpressions;\r
-                            originalExpressionType = ExpressionType.Auxiliary;\r
-                        }\r
-                        else if(graph.isInstanceOf(variable, sr.Stock)) {\r
-                            expressionTypes = stockExpressions;\r
-                            originalExpressionType = ExpressionType.Stock;\r
-                        }\r
-                        else\r
-                            expressionTypes = null;\r
-\r
-                        ExpressionType oet = getOriginalExpressionType(graph, graph.getPossibleObject(variable, sr.HasExpression));\r
-                        if (oet != null)  originalExpressionType = oet;\r
-                        return expressionTypes;\r
-                    }\r
-                });\r
-            } catch (DatabaseException e1) {\r
-                e1.printStackTrace();\r
-            }\r
-        }\r
-\r
-        Label label = new Label(this, SWT.SINGLE );\r
-        label.setFont(EquationView.FONT);\r
-        label.setText("Type:");\r
-        GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
-\r
-        typeCombo = new Combo(this, SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY);\r
-        typeCombo.setFont(EquationView.FONT);\r
-\r
-        int columns = 6;\r
-        GC gc = new GC (typeCombo);\r
-        FontMetrics fm = gc.getFontMetrics ();\r
-        int width = columns * fm.getAverageCharWidth ();\r
-        int height = fm.getHeight ();\r
-        gc.dispose ();        \r
-        GridDataFactory.fillDefaults().hint(typeCombo.computeSize(width, height)).applyTo(typeCombo);\r
-\r
-        typeCombo.removeAll();\r
-        if(this.expressionTypes != null) {\r
-            for(ExpressionType et : this.expressionTypes) {\r
-                typeCombo.add(et.toString());\r
-            }\r
-        }\r
-\r
-        if(originalExpressionType != null) {\r
-            displayExpressionView(variable, originalExpressionType, true);\r
-        }\r
-\r
-        typeCombo.addSelectionListener(new SelectionListener() {\r
-\r
-            @Override\r
-            public void widgetSelected(SelectionEvent e) {\r
-                getExpressionViewFactor().updateData(data);\r
-                displayExpressionView(variable, ExpressionType.valueOf(typeCombo.getItem(typeCombo.getSelectionIndex())), false);\r
-            }\r
-\r
-            @Override\r
-            public void widgetDefaultSelected(SelectionEvent e) {\r
-            }\r
-        });\r
-    }\r
-\r
-    private ExpressionType getOriginalExpressionType(ReadGraph graph, Resource expression) {\r
-        if(expression == null)\r
-            return null;\r
-        SysdynResource sr = SysdynResource.getInstance(graph);\r
-        ExpressionType et = null;\r
-        try {\r
-            if(graph.isInstanceOf(expression, sr.NormalExpression)) {\r
-                et = ExpressionType.Auxiliary;\r
-            } else if (graph.isInstanceOf(expression, sr.StockExpression)) {\r
-                et = ExpressionType.Stock;\r
-            } else if (graph.isInstanceOf(expression, sr.ParameterExpression)) {\r
-                et = ExpressionType.Parameter;\r
-            } else if (graph.isInstanceOf(expression, sr.ConstantExpression)) {\r
-                et = ExpressionType.Constant;\r
-            } else if (graph.isInstanceOf(expression, sr.DelayExpression)) {\r
-                et = ExpressionType.Delay;\r
-            } else if (graph.isInstanceOf(expression, sr.LookupExpression)) {\r
-                et = ExpressionType.Lookup;\r
-            } else if (graph.isInstanceOf(expression, sr.WithLookupExpression)) {\r
-                et = ExpressionType.WithLookup;\r
-            } else {\r
-                et = ExpressionType.Auxiliary;\r
-            }\r
-        } catch (ServiceException e) {\r
-            e.printStackTrace();\r
-        }\r
-\r
-        return et;\r
-    }\r
-\r
-\r
-    private void displayExpressionView(Resource variable, ExpressionType et, boolean original) {\r
-        int index = typeCombo.indexOf(et.toString());\r
-        if (index > -1) {\r
-            typeCombo.select(index);\r
-        }\r
-\r
-        IExpressionViewFactor evf = null;\r
-        switch (et) {\r
-            case Auxiliary: \r
-                evf = new AuxiliaryExpressionViewFactor(); break;\r
-            case Parameter: \r
-                evf = new ParameterExpressionViewFactor(); break;\r
-            case Constant: \r
-                evf = new ConstantExpressionViewFactor(); break;\r
-            case Lookup: \r
-                evf = new LookupExpressionViewFactor(); break;\r
-            case WithLookup: \r
-                evf = new WithLookupExpressionViewFactor(); break;\r
-            case Stock: \r
-                evf = new StockExpressionViewFactor(); break;\r
-            case Delay: \r
-                evf = new DelayExpressionViewFactor(); break;\r
-            default: ;\r
-        }\r
-        if (evf != null) {\r
-            for(Control c : equationComposite.getChildren()) {\r
-                c.dispose();\r
-            } \r
-            if(original) {\r
-                evf.readData(variable, data);\r
-                for(String key : data.keySet()) {\r
-                    origData.put(key, data.get(key));\r
-                }\r
-            }\r
-            evf.createView(equationComposite, data);\r
-            expressionViewFactor = evf;\r
-            equationComposite.layout();\r
-        }\r
-    }\r
-\r
-    public IExpressionViewFactor getExpressionViewFactor() {\r
-        return this.expressionViewFactor;\r
-    }\r
-\r
-    public void save() {\r
-        this.expressionViewFactor.writeData(variable, data);\r
-    }\r
-}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionTypeSelector.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionTypeSelector.java
new file mode 100644 (file)
index 0000000..8049c65
--- /dev/null
@@ -0,0 +1,63 @@
+package org.simantics.sysdyn.ui.equation;\r
+\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.jface.layout.GridLayoutFactory;\r
+import org.eclipse.swt.SWT;\r
+\r
+import org.eclipse.swt.graphics.FontMetrics;\r
+import org.eclipse.swt.graphics.GC;\r
+import org.eclipse.swt.widgets.Combo;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Label;\r
+\r
+public class ExpressionTypeSelector extends Composite {\r
+\r
+    Combo typeCombo;\r
+\r
+    public ExpressionTypeSelector(final Composite parent, int style) {\r
+        super(parent, style);\r
+        GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(this);\r
+\r
+\r
+        Label label = new Label(this, SWT.SINGLE );\r
+        label.setFont(EquationView.FONT);\r
+        label.setText("Type:");\r
+        GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
+\r
+        typeCombo = new Combo(this, SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY);\r
+        typeCombo.setFont(EquationView.FONT);\r
+\r
+        int columns = 6;\r
+        GC gc = new GC (typeCombo);\r
+        FontMetrics fm = gc.getFontMetrics ();\r
+        int width = columns * fm.getAverageCharWidth ();\r
+        int height = fm.getHeight ();\r
+        gc.dispose ();        \r
+        GridDataFactory.fillDefaults().hint(typeCombo.computeSize(width, height)).applyTo(typeCombo);\r
+        \r
+    }\r
+    \r
+    public Combo getTypeCombo() {\r
+        return this.typeCombo;\r
+    }\r
+    \r
+    public String getSelecetedType() {\r
+        return this.typeCombo.getItem(this.typeCombo.getSelectionIndex());\r
+    }\r
+    \r
+    public void setExpressionTypes(ExpressionComposite.ExpressionType[] expressionTypes) {\r
+        typeCombo.removeAll();\r
+        if(expressionTypes != null) {\r
+            for(ExpressionComposite.ExpressionType et : expressionTypes) {\r
+                typeCombo.add(et.toString());\r
+            }\r
+        }\r
+    }\r
+    \r
+    public void select(String expressionType) {\r
+        int index = typeCombo.indexOf(expressionType);\r
+        if (index > -1) {\r
+            typeCombo.select(index);\r
+        }\r
+    }\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ShortcutTabs.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ShortcutTabs.java
new file mode 100644 (file)
index 0000000..533b631
--- /dev/null
@@ -0,0 +1,112 @@
+package org.simantics.sysdyn.ui.equation;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.jface.layout.GridLayoutFactory;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.TabFolder;\r
+import org.eclipse.swt.widgets.TabItem;\r
+import org.eclipse.swt.widgets.Table;\r
+import org.eclipse.swt.widgets.TableItem;\r
+import org.simantics.db.Builtins;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.ui.ExceptionUtils;\r
+\r
+public class ShortcutTabs extends Composite {\r
+\r
+    \r
+    TabItem variables;\r
+    TabItem functions;\r
+    Table variableTable;\r
+    \r
+    public ShortcutTabs(Composite parent, int style) {\r
+        super(parent, style);\r
+\r
+        GridLayoutFactory.fillDefaults().applyTo(this);\r
+\r
+        TabFolder tabFolder = new TabFolder (this, SWT.NONE);\r
+        GridDataFactory.fillDefaults().grab(false, true).applyTo(tabFolder);\r
+        GridLayoutFactory.fillDefaults().applyTo(tabFolder);\r
+        variables = new TabItem(tabFolder, SWT.NULL);\r
+        variables.setText("Variables");\r
+        variableTable = new Table (tabFolder, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);\r
+\r
+        variables.setControl(variableTable);\r
+\r
+        functions = new TabItem(tabFolder, SWT.NULL);\r
+        functions.setText("Functions"); \r
+        Table table = new Table (tabFolder, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);\r
+        TableItem item = new TableItem(table, SWT.NONE);\r
+        item.setText("min");\r
+        item = new TableItem(table, SWT.NONE);\r
+        item.setText("max");\r
+        item = new TableItem(table, SWT.NONE);\r
+        item.setText("abs");\r
+        functions.setControl(table);\r
+        \r
+        this.layout();\r
+    }\r
+    \r
+    public ArrayList<String> updateTables(Resource variable) {\r
+        ArrayList<String> dependencies = getDependencies(variable);\r
+        variableTable.removeAll();\r
+        TableItem item;\r
+        for(String d : dependencies) {\r
+            item = new TableItem(variableTable, SWT.NONE);\r
+            item.setText(d);\r
+        }\r
+        return dependencies;\r
+    }\r
+    \r
+    // Returns the names of the related variables (dependencies)\r
+    private ArrayList<String> getDependencies(final Resource r){\r
+        ArrayList<String> variables = null;\r
+        try {\r
+            variables = SimanticsUI.getSession().syncRequest(new Read<ArrayList<String>>() {\r
+\r
+                @Override\r
+                public ArrayList<String> perform(ReadGraph graph)\r
+                throws DatabaseException {\r
+                    ArrayList<String> variables = new ArrayList<String>();\r
+                    SysdynResource sr = SysdynResource.getInstance(graph);\r
+                    Builtins b = graph.getBuiltins();\r
+\r
+                    Collection<Resource> dependencies = graph.getObjects(r, sr.IsHeadOf);\r
+\r
+                    for(Resource d : dependencies) {\r
+                        if(graph.isInstanceOf(d, sr.Dependency)) {\r
+                            Resource tail = graph.getPossibleObject(d, sr.HasTail);\r
+                            if(tail != null) {\r
+                                Object name = graph.getPossibleRelatedValue(tail, b.HasName);\r
+                                if(name != null)\r
+                                    variables.add((String)name);\r
+                            }\r
+                        }\r
+                    }\r
+\r
+                    return variables;\r
+\r
+                }\r
+\r
+\r
+            });\r
+        } catch (DatabaseException ee) {\r
+            ExceptionUtils.logAndShowError(ee);\r
+        }\r
+\r
+        return variables;\r
+    }\r
+    \r
+    public Table getVariableTable() {\r
+        return this.variableTable;\r
+    }\r
+\r
+}\r
index 26e282f4856ef19631aec6c32f06450f0822ea1f..925aadfaab645ba65f89169d9c59c670a1668f8a 100644 (file)
@@ -1,6 +1,8 @@
 package org.simantics.sysdyn.ui.equation;\r
 \r
 import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
 \r
 import org.eclipse.jface.layout.GridDataFactory;\r
 import org.eclipse.jface.layout.GridLayoutFactory;\r
@@ -10,6 +12,7 @@ import org.eclipse.swt.graphics.GC;
 import org.eclipse.swt.widgets.Combo;\r
 import org.eclipse.swt.widgets.Composite;\r
 import org.eclipse.swt.widgets.Label;\r
+import org.simantics.db.Builtins;\r
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
@@ -18,6 +21,7 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.request.Read;\r
 import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.ui.ExceptionUtils;\r
 \r
 public class UnitSelector extends Composite {\r
 \r
@@ -26,29 +30,11 @@ public class UnitSelector extends Composite {
     String unit = null;\r
     ArrayList<String> units;\r
 \r
-    public UnitSelector(Composite parent, int style, final Resource variable, ArrayList<String> units) {\r
+    public UnitSelector(Composite parent, int style) {\r
         super(parent, style);\r
-        this.variable = variable;\r
-        this.units = units;\r
-        GridDataFactory.fillDefaults().applyTo(this);\r
         GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(this);\r
 \r
-        if(variable != null) {\r
-            try {\r
-                SimanticsUI.getSession().syncRequest(new Read<String>() {\r
-\r
-                    @Override\r
-                    public String perform(ReadGraph graph) throws DatabaseException {\r
-                        SysdynResource sr = SysdynResource.getInstance(graph);\r
-                        unit = graph.getPossibleRelatedValue(variable, sr.HasUnit);\r
-                        return null;\r
-                    }\r
-                });\r
-            } catch (DatabaseException e1) {\r
-                e1.printStackTrace();\r
-            }\r
-        }\r
-\r
+        this.units = getUnits();\r
 \r
         Label label = new Label(this, SWT.SINGLE );\r
         label.setFont(EquationView.FONT);\r
@@ -68,21 +54,19 @@ public class UnitSelector extends Composite {
         gc.dispose ();        \r
         GridDataFactory.fillDefaults().hint(unitCombo.computeSize(width, height)).applyTo(unitCombo);\r
 \r
-        unitCombo.removeAll();\r
-        for(String unit : this.units) {\r
-            unitCombo.add(unit);\r
-        }\r
+        parent.layout();\r
 \r
-        if (unit != null)\r
-            if(unitCombo.indexOf(unit) >= 0)\r
-                unitCombo.select(unitCombo.indexOf(unit));\r
-            else\r
-                unitCombo.setText(unit);\r
     }\r
 \r
     public String save(){\r
         final String unitNew = unitCombo.getText();\r
-        if(unitNew != null && !unitNew.equals(unit)) {\r
+\r
+        if(unitNew != null && !unitNew.equals("") && !units.contains(unitNew)) {\r
+            units.add(unitNew);\r
+            Collections.sort(units);\r
+        }\r
+\r
+        if(unitNew != null && !unitNew.equals(unit) && variable != null) {\r
             try {\r
                 SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
 \r
@@ -104,4 +88,81 @@ public class UnitSelector extends Composite {
         }\r
         return null;\r
     }\r
+\r
+    public void updateUnits(Resource variable) {\r
+        if(unitCombo != null) {\r
+            this.variable = variable;\r
+            unitCombo.removeAll();\r
+            for(String unit : this.units) {\r
+                unitCombo.add(unit);\r
+            }\r
+\r
+            setUnit();\r
+        }\r
+    }\r
+\r
+    private void setUnit() {\r
+\r
+        if(variable != null) {\r
+            try {\r
+                SimanticsUI.getSession().syncRequest(new Read<String>() {\r
+\r
+                    @Override\r
+                    public String perform(ReadGraph graph) throws DatabaseException {\r
+                        SysdynResource sr = SysdynResource.getInstance(graph);\r
+                        unit = graph.getPossibleRelatedValue(variable, sr.HasUnit);\r
+                        return null;\r
+                    }\r
+                });\r
+            } catch (DatabaseException e1) {\r
+                e1.printStackTrace();\r
+            }\r
+        }\r
+\r
+        if (unit != null)\r
+            if(unitCombo.indexOf(unit) >= 0)\r
+                unitCombo.select(unitCombo.indexOf(unit));\r
+            else\r
+                unitCombo.setText(unit);\r
+    }\r
+\r
+\r
+    // Returns all the used units in the model\r
+    private ArrayList<String> getUnits() {\r
+        ArrayList<String> units = new ArrayList<String>();\r
+        if(variable != null) {\r
+            try {\r
+                units = SimanticsUI.getSession().syncRequest(new Read<ArrayList<String>>() {\r
+\r
+                    @Override\r
+                    public ArrayList<String> perform(ReadGraph graph)\r
+                    throws DatabaseException {\r
+                        ArrayList<String> units = new ArrayList<String>();\r
+\r
+                        SysdynResource sr = SysdynResource.getInstance(graph);\r
+                        Builtins b = graph.getBuiltins();\r
+                        Resource model = graph.getPossibleObject(variable, b.PartOf);\r
+                        if (model != null) {\r
+                            Collection<Resource> variables = graph.getObjects(model, b.ConsistsOf);\r
+                            for(Resource v : variables) {\r
+                                Object unit = graph.getPossibleRelatedValue(v, sr.HasUnit);\r
+                                if (unit != null && !units.contains((String) unit)) {\r
+                                    units.add((String)unit);\r
+                                }\r
+                            }\r
+                        }\r
+\r
+                        return units;\r
+\r
+                    }\r
+\r
+\r
+                });\r
+            } catch (DatabaseException ee) {\r
+                ExceptionUtils.logAndShowError(ee);\r
+            }\r
+        }\r
+        return units;\r
+    }\r
+\r
 }\r