]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Unit selector. When equation view opened, it collects all units in the model. After...
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 7 Jan 2010 12:22:15 +0000 (12:22 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 7 Jan 2010 12:22:15 +0000 (12:22 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13420 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/TypeSelector.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/UnitSelector.java

index 4fbd7345e391058ad01433cd46a2ea2a8fb5bbe6..785deb0b3346294177449d8fdcdeabc27e025337 100644 (file)
@@ -3,6 +3,7 @@ package org.simantics.sysdyn.ui.equation;
 \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
@@ -11,8 +12,6 @@ import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.swt.SWT;\r
 import org.eclipse.swt.events.FocusEvent;\r
 import org.eclipse.swt.events.FocusListener;\r
-import org.eclipse.swt.events.KeyAdapter;\r
-import org.eclipse.swt.events.KeyEvent;\r
 import org.eclipse.swt.events.MouseEvent;\r
 import org.eclipse.swt.events.MouseListener;\r
 import org.eclipse.swt.graphics.Font;\r
@@ -34,11 +33,7 @@ import org.eclipse.ui.part.ViewPart;
 import org.simantics.db.Builtins;\r
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.request.WriteRequest;\r
 import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;\r
-import org.simantics.db.exception.ServiceException;\r
 import org.simantics.db.procedure.Listener;\r
 import org.simantics.db.request.Read;\r
 import org.simantics.sysdyn.SysdynResource;\r
@@ -52,8 +47,12 @@ public class EquationView extends ViewPart implements ISelectionListener {
 \r
     static final Font FONT = new Font(null, "Courier New", 12, SWT.NORMAL);\r
 \r
+    //Resources\r
     Resource variable;\r
-    ArrayList<String> dependencies;\r
+    Resource configuration;\r
+    \r
+    ArrayList<String> units;\r
+    \r
     boolean disposed = false;\r
     \r
     Composite parent;\r
@@ -79,6 +78,7 @@ public class EquationView extends ViewPart implements ISelectionListener {
     @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
@@ -93,7 +93,13 @@ public class EquationView extends ViewPart implements ISelectionListener {
                     // Save expressions\r
                     if(expressionView != null) expressionView.save();\r
                     // Save unit\r
-                    if(unitSelector != null) unitSelector.save();\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
+                    }\r
                 }\r
 \r
             }\r
@@ -126,7 +132,8 @@ public class EquationView extends ViewPart implements ISelectionListener {
         unitComposite = new Composite(variableType, SWT.NONE);\r
         GridDataFactory.fillDefaults().applyTo(unitComposite);\r
         GridLayoutFactory.fillDefaults().applyTo(unitComposite);\r
-        unitSelector = new UnitSelector(unitComposite, SWT.NONE, variable);\r
+        units = getUnits();\r
+        unitSelector = new UnitSelector(unitComposite, SWT.NONE, variable, units);\r
  \r
         // Center side of the view: (related) variables and possible functions\r
         TabFolder tabFolder = new TabFolder (parent, SWT.NONE);\r
@@ -224,6 +231,7 @@ public class EquationView extends ViewPart implements ISelectionListener {
         ArrayList<String> dependencies;\r
         String name;\r
         String type;\r
+        Resource configuration;\r
     }\r
 \r
     class UpdateViewRequest implements Read<Auxiliary> {\r
@@ -239,6 +247,7 @@ public class EquationView extends ViewPart implements ISelectionListener {
             Builtins b = graph.getBuiltins();\r
             Auxiliary a = new Auxiliary();\r
             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
@@ -282,12 +291,17 @@ public class EquationView extends ViewPart implements ISelectionListener {
                                                 item = new TableItem(variableTable, SWT.NONE);\r
                                                 item.setText(d);\r
                                             }\r
-                                            nameText.setText(result.name);\r
-\r
+                                            \r
                                             if(unitSelector!= null) unitSelector.dispose();\r
-                                            unitSelector = new UnitSelector(unitComposite, SWT.NONE, variable);\r
+                                            if(configuration == null || !configuration.equals(result.configuration)) {\r
+                                                configuration = result.configuration;\r
+                                                units = getUnits();\r
+                                            }\r
+                                            unitSelector = new UnitSelector(unitComposite, SWT.NONE, variable, units);\r
                                             unitComposite.layout();\r
                                             \r
+                                            nameText.setText(result.name);\r
+                                            \r
                                             for(Control c : equationComposite.getChildren()) {\r
                                                 c.dispose();\r
                                             }   \r
@@ -320,6 +334,46 @@ public class EquationView extends ViewPart implements ISelectionListener {
         if(expressionView != null)\r
             expressionView.focus();\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
+\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
+\r
+                    return units;\r
+\r
+                }\r
+\r
+\r
+            });\r
+        } catch (DatabaseException ee) {\r
+            ExceptionUtils.logAndShowError(ee);\r
+        }\r
+        return units;\r
+    }\r
 \r
     // Returns the names of the related variables (dependencies)\r
     private ArrayList<String> getDependencies(final Resource r){\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/TypeSelector.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/TypeSelector.java
new file mode 100644 (file)
index 0000000..277b94c
--- /dev/null
@@ -0,0 +1,41 @@
+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.widgets.Combo;\r
+import org.eclipse.swt.widgets.Composite;\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.ui.SimanticsUI;\r
+\r
+public class TypeSelector extends Composite {\r
+    \r
+    Combo typeCombo;\r
+    Resource variable;\r
+\r
+    public TypeSelector(Composite parent, int style, final Resource variable) {\r
+        super(parent, style);\r
+        \r
+        this.variable = variable;\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
+\r
+\r
+                        return null;\r
+                    }\r
+                });\r
+            } catch (DatabaseException e1) {\r
+                e1.printStackTrace();\r
+            }\r
+        }\r
+    }\r
+}\r
index 238f98119f55916ab5efab45fca62576f09f5be2..26e282f4856ef19631aec6c32f06450f0822ea1f 100644 (file)
@@ -1,5 +1,7 @@
 package org.simantics.sysdyn.ui.equation;\r
 \r
+import java.util.ArrayList;\r
+\r
 import org.eclipse.jface.layout.GridDataFactory;\r
 import org.eclipse.jface.layout.GridLayoutFactory;\r
 import org.eclipse.swt.SWT;\r
@@ -21,11 +23,13 @@ public class UnitSelector extends Composite {
 \r
     Combo unitCombo;\r
     Resource variable;\r
-    String unit = "";\r
+    String unit = null;\r
+    ArrayList<String> units;\r
 \r
-    public UnitSelector(Composite parent, int style, final Resource variable) {\r
+    public UnitSelector(Composite parent, int style, final Resource variable, ArrayList<String> units) {\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
@@ -55,7 +59,7 @@ public class UnitSelector extends Composite {
 \r
         unitCombo = new Combo(this, SWT.DROP_DOWN | SWT.BORDER);\r
         unitCombo.setFont(EquationView.FONT);\r
-        \r
+\r
         int columns = 6;\r
         GC gc = new GC (unitCombo);\r
         FontMetrics fm = gc.getFontMetrics ();\r
@@ -63,10 +67,12 @@ public class UnitSelector extends Composite {
         int height = fm.getHeight ();\r
         gc.dispose ();        \r
         GridDataFactory.fillDefaults().hint(unitCombo.computeSize(width, height)).applyTo(unitCombo);\r
-        \r
-        unitCombo.add("m");\r
-        unitCombo.add("V");\r
-        unitCombo.add("euro");\r
+\r
+        unitCombo.removeAll();\r
+        for(String unit : this.units) {\r
+            unitCombo.add(unit);\r
+        }\r
+\r
         if (unit != null)\r
             if(unitCombo.indexOf(unit) >= 0)\r
                 unitCombo.select(unitCombo.indexOf(unit));\r
@@ -74,23 +80,28 @@ public class UnitSelector extends Composite {
                 unitCombo.setText(unit);\r
     }\r
 \r
-    public void save(){\r
+    public String save(){\r
         final String unitNew = unitCombo.getText();\r
         if(unitNew != null && !unitNew.equals(unit)) {\r
-            SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
-\r
-                @Override\r
-                public void perform(WriteGraph g)\r
-                throws DatabaseException {\r
-                    SysdynResource sr = SysdynResource.getInstance(g);\r
-                    if (!unitNew.equals(""))\r
-                        g.claimValue(variable, sr.HasUnit, unitNew);\r
-                    else if (g.getPossibleRelatedValue(variable, sr.HasUnit) != null)\r
-                        g.deny(variable, sr.HasUnit);\r
-                }\r
-            });\r
-        }\r
+            try {\r
+                SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
 \r
+                    @Override\r
+                    public void perform(WriteGraph g)\r
+                    throws DatabaseException {\r
+                        SysdynResource sr = SysdynResource.getInstance(g);\r
+                        if (!unitNew.equals("")) {\r
+                            g.claimValue(variable, sr.HasUnit, unitNew);\r
+                        }\r
+                        else if (g.getPossibleRelatedValue(variable, sr.HasUnit) != null)\r
+                            g.deny(variable, sr.HasUnit);\r
+                    }\r
+                });\r
+            } catch (DatabaseException e) {\r
+                e.printStackTrace();\r
+            }\r
+            return unitNew;\r
+        }\r
+        return null;\r
     }\r
-\r
 }\r