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
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
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
--- /dev/null
+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
--- /dev/null
+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
\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
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