]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
More properties
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 14 Jun 2010 15:09:56 +0000 (15:09 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 14 Jun 2010 15:09:56 +0000 (15:09 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16157 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypeWidget.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypes.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ShortcutTabWidget.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/UnitWidget.java

index 8c2a47c0b0449de5facf35d9ea2bccfbe55a0740..2c628aa7f160a7f6f7c989c948fa21964eb32b9e 100644 (file)
@@ -12,7 +12,9 @@ import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
 import org.simantics.db.Builtins;\r
 import org.simantics.db.management.ISessionContext;\r
 import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.ui.properties.widgets.ExpressionTypeWidget;\r
 import org.simantics.sysdyn.ui.properties.widgets.NameWidget;\r
+import org.simantics.sysdyn.ui.properties.widgets.ShortcutTabWidget;\r
 import org.simantics.sysdyn.ui.properties.widgets.UnitWidget;\r
 \r
 public class EquationTab extends PropertyTabContributorImpl {\r
@@ -27,10 +29,14 @@ public class EquationTab extends PropertyTabContributorImpl {
         NameWidget nameWidget = new NameWidget(composite, support, SWT.BORDER);\r
         nameWidget.setTextFactory(new StringPropertyFactory(context, Builtins.URIs.HasName));\r
         \r
+        @SuppressWarnings("unused")\r
+        ExpressionTypeWidget expressionTypeWidget = new ExpressionTypeWidget(composite, support, SWT.NONE);\r
+        \r
         UnitWidget unitWidget = new UnitWidget(composite, support, SWT.NONE);\r
         unitWidget.setTextFactory(new StringPropertyFactory(context, SysdynResource.URIs.HasUnit));\r
+        \r
+        @SuppressWarnings("unused")\r
+        ShortcutTabWidget shortcutTabWidget = new ShortcutTabWidget(composite, support, SWT.NONE);\r
+        \r
     }\r
-\r
-  \r
-\r
 }\r
index 05bc7de4617036bdf04e3b23db0b9851d4fe92e1..fb299a7d8da214c1845bb7524985719797f1b170 100644 (file)
@@ -1,5 +1,138 @@
 package org.simantics.sysdyn.ui.properties.widgets;\r
 \r
-public class ExpressionTypeWidget {\r
 \r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.jface.layout.GridLayoutFactory;\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.swt.SWT;\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
+import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.ui.properties.EquationTab;\r
+import org.simantics.sysdyn.ui.properties.widgets.ExpressionTypes.ExpressionType;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+\r
+public class ExpressionTypeWidget implements Widget {\r
+\r
+    Combo typeCombo;\r
+\r
+    public ExpressionTypeWidget(Composite parent, WidgetSupport support, int style) {\r
+        support.register(this);\r
+        \r
+        Composite composite = new Composite(parent, style);\r
+        GridDataFactory.fillDefaults().applyTo(composite);\r
+        GridLayoutFactory.fillDefaults().numColumns(2).applyTo(composite);\r
+\r
+        Label label = new Label(composite, SWT.SINGLE );\r
+        label.setFont(EquationTab.FONT);\r
+        label.setText("Type:");\r
+        GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
+\r
+        typeCombo = new Combo(composite, SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY);\r
+        typeCombo.setFont(EquationTab.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
+    @Override\r
+    public void setInput(Object input) {\r
+        if(input instanceof IStructuredSelection) {\r
+            final Resource variable = ISelectionUtils.filterSingleSelection((IStructuredSelection)input, Resource.class);\r
+            if(variable != null) {\r
+                SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
+                    @Override\r
+                    public void run(ReadGraph graph) throws DatabaseException {\r
+                        SysdynResource sr = SysdynResource.getInstance(graph);\r
+                        \r
+                        final ExpressionType[] expressionTypes;\r
+                        if(graph.isInstanceOf(variable, sr.Auxiliary))\r
+                            expressionTypes = ExpressionTypes.auxiliaryExpressions;\r
+                        else if(graph.isInstanceOf(variable, sr.Stock))\r
+                            expressionTypes = ExpressionTypes.stockExpressions;\r
+                        else if(graph.isInstanceOf(variable, sr.Valve))\r
+                            expressionTypes = ExpressionTypes.valveExpressions;\r
+                        else\r
+                            expressionTypes = new ExpressionType[] {};\r
+                        \r
+                        \r
+                        final ExpressionType et;\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
+                            } else\r
+                                et = ExpressionType.Empty;\r
+                        } else {\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.Empty;\r
+                            }\r
+                        }\r
+                        \r
+                        typeCombo.getDisplay().asyncExec(new Runnable() {\r
+                            \r
+                            @Override\r
+                            public void run() {\r
+                                setExpressionTypes(expressionTypes);\r
+                                setExpressionType(et);                                \r
+                            }\r
+                        });\r
+\r
+                    }\r
+                   \r
+                });\r
\r
+            }\r
+        }\r
+    }\r
+    \r
+    private void setExpressionTypes(ExpressionType[] expressionTypes) {\r
+        if(expressionTypes != null) {\r
+            for(ExpressionType et : expressionTypes) {\r
+                typeCombo.add(et.toString());\r
+            }\r
+        }\r
+    }\r
+\r
+    public void setExpressionType(ExpressionType expressionType) {\r
+        int index = typeCombo.indexOf(expressionType.toString());\r
+        if (index > -1) {\r
+            typeCombo.select(index);\r
+        }\r
+    }\r
 }\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypes.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypes.java
new file mode 100644 (file)
index 0000000..4cebd8a
--- /dev/null
@@ -0,0 +1,23 @@
+package org.simantics.sysdyn.ui.properties.widgets;\r
+\r
+public class ExpressionTypes {\r
+    \r
+    public static enum ExpressionType {Auxiliary, Parameter, Constant, Lookup, WithLookup, Stock, Delay, Empty};\r
+\r
+    public static ExpressionType[] auxiliaryExpressions = new ExpressionType[] {\r
+            ExpressionType.Parameter, \r
+            ExpressionType.Auxiliary, \r
+            ExpressionType.Constant, \r
+            ExpressionType.Lookup, \r
+            ExpressionType.WithLookup};\r
+\r
+    public static ExpressionType[] valveExpressions = new ExpressionType[] {\r
+            ExpressionType.Auxiliary, \r
+            ExpressionType.Parameter, \r
+            ExpressionType.Constant, \r
+            ExpressionType.WithLookup};\r
+\r
+    public static ExpressionType[] stockExpressions = new ExpressionType[] {\r
+            ExpressionType.Stock};\r
+\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ShortcutTabWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ShortcutTabWidget.java
new file mode 100644 (file)
index 0000000..1de6fb4
--- /dev/null
@@ -0,0 +1,126 @@
+package org.simantics.sysdyn.ui.properties.widgets;\r
+\r
+import java.util.Collection;\r
+import java.util.HashSet;\r
+\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.jface.layout.GridLayoutFactory;\r
+import org.eclipse.jface.viewers.IStructuredSelection;\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.browsing.ui.swt.widgets.impl.Widget;\r
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\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
+import org.simantics.utils.ui.ISelectionUtils;\r
+\r
+public class ShortcutTabWidget implements Widget {\r
+\r
+    TabFolder tabFolder;\r
+    TabItem variables;\r
+    TabItem functions;\r
+    Table variableTable;\r
+    Table functionTable;\r
+\r
+    public ShortcutTabWidget(Composite parent, WidgetSupport support, int style) {\r
+        support.register(this);\r
+\r
+        Composite composite = new Composite(parent, style);\r
+        GridDataFactory.fillDefaults().span(1, 2).grab(false, true).applyTo(composite);\r
+        GridLayoutFactory.fillDefaults().numColumns(2).applyTo(composite);\r
+\r
+        tabFolder = new TabFolder (composite, 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
+        functionTable = new Table (tabFolder, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);\r
+        TableItem item = new TableItem(functionTable, SWT.NONE);\r
+        item.setText("min()");\r
+        item.setData("min({ })");\r
+        item = new TableItem(functionTable, SWT.NONE);\r
+        item.setText("max()");\r
+        item.setData("max({ })");\r
+        item = new TableItem(functionTable, SWT.NONE);\r
+        item.setText("abs()");\r
+        item.setData("abs({ })");\r
+        item = new TableItem(functionTable, SWT.NONE);\r
+        item.setText("if then else");\r
+        item.setData("if then else");\r
+        functions.setControl(functionTable);\r
+    }\r
+\r
+    @Override\r
+    public void setInput(Object input) {\r
+        if(input instanceof IStructuredSelection) {\r
+            final Resource variable = ISelectionUtils.filterSingleSelection((IStructuredSelection)input, Resource.class);\r
+            if(variable != null) {\r
+                HashSet<String> dependencies = getDependencies(variable);\r
+                TableItem item;\r
+                for(String d : dependencies) {\r
+                    item = new TableItem(variableTable, SWT.NONE);\r
+                    item.setText(d);\r
+                    item.setData(d);\r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
+    // Returns the names of the related variables (dependencies)\r
+    private HashSet<String> getDependencies(final Resource r){\r
+        HashSet<String> variables = null;\r
+        if(r != null)\r
+            try {\r
+                variables = SimanticsUI.getSession().syncRequest(new Read<HashSet<String>>() {\r
+\r
+                    @Override\r
+                    public HashSet<String> perform(ReadGraph graph)\r
+                    throws DatabaseException {\r
+                        HashSet<String> variables = new HashSet<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
+\r
+}\r
index 658b8c31f1f933c6c9368feecef05b60f2ad9e96..c93676fdb3a3e649f6f177fa24d1bd7b9e9f73a9 100644 (file)
@@ -75,9 +75,7 @@ public class UnitWidget implements Widget {
 \r
                 @Override\r
                 public void execute(final String text) {\r
-\r
-                    if(text == null) return;\r
-\r
+                    \r
                     unitCombo.getDisplay().asyncExec(new Runnable() {\r
 \r
                         @Override\r
@@ -92,6 +90,9 @@ public class UnitWidget implements Widget {
                                     unitCombo.add(unit);\r
                                 }\r
                             }\r
+                            \r
+                            if(text == null) return;\r
+                            \r
                             if(unitCombo.indexOf(text) >= 0)\r
                                 unitCombo.select(unitCombo.indexOf(text));\r
                             else\r