From 16b823f816b828d77b7c9218636e49b1312d2553 Mon Sep 17 00:00:00 2001 From: lempinen Date: Mon, 14 Jun 2010 15:09:56 +0000 Subject: [PATCH] More properties git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16157 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/properties/EquationTab.java | 12 +- .../widgets/ExpressionTypeWidget.java | 135 +++++++++++++++++- .../properties/widgets/ExpressionTypes.java | 23 +++ .../properties/widgets/ShortcutTabWidget.java | 126 ++++++++++++++++ .../ui/properties/widgets/UnitWidget.java | 7 +- 5 files changed, 296 insertions(+), 7 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypes.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ShortcutTabWidget.java diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java index 8c2a47c0..2c628aa7 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java @@ -12,7 +12,9 @@ import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; import org.simantics.db.Builtins; import org.simantics.db.management.ISessionContext; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.properties.widgets.ExpressionTypeWidget; import org.simantics.sysdyn.ui.properties.widgets.NameWidget; +import org.simantics.sysdyn.ui.properties.widgets.ShortcutTabWidget; import org.simantics.sysdyn.ui.properties.widgets.UnitWidget; public class EquationTab extends PropertyTabContributorImpl { @@ -27,10 +29,14 @@ public class EquationTab extends PropertyTabContributorImpl { NameWidget nameWidget = new NameWidget(composite, support, SWT.BORDER); nameWidget.setTextFactory(new StringPropertyFactory(context, Builtins.URIs.HasName)); + @SuppressWarnings("unused") + ExpressionTypeWidget expressionTypeWidget = new ExpressionTypeWidget(composite, support, SWT.NONE); + UnitWidget unitWidget = new UnitWidget(composite, support, SWT.NONE); unitWidget.setTextFactory(new StringPropertyFactory(context, SysdynResource.URIs.HasUnit)); + + @SuppressWarnings("unused") + ShortcutTabWidget shortcutTabWidget = new ShortcutTabWidget(composite, support, SWT.NONE); + } - - - } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypeWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypeWidget.java index 05bc7de4..fb299a7d 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypeWidget.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypeWidget.java @@ -1,5 +1,138 @@ package org.simantics.sysdyn.ui.properties.widgets; -public class ExpressionTypeWidget { +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.FontMetrics; +import org.eclipse.swt.graphics.GC; +import org.eclipse.swt.widgets.Combo; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.simantics.browsing.ui.swt.widgets.impl.Widget; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.properties.EquationTab; +import org.simantics.sysdyn.ui.properties.widgets.ExpressionTypes.ExpressionType; +import org.simantics.ui.SimanticsUI; +import org.simantics.utils.ui.ISelectionUtils; + +public class ExpressionTypeWidget implements Widget { + + Combo typeCombo; + + public ExpressionTypeWidget(Composite parent, WidgetSupport support, int style) { + support.register(this); + + Composite composite = new Composite(parent, style); + GridDataFactory.fillDefaults().applyTo(composite); + GridLayoutFactory.fillDefaults().numColumns(2).applyTo(composite); + + Label label = new Label(composite, SWT.SINGLE ); + label.setFont(EquationTab.FONT); + label.setText("Type:"); + GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label); + + typeCombo = new Combo(composite, SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY); + typeCombo.setFont(EquationTab.FONT); + + int columns = 6; + GC gc = new GC (typeCombo); + FontMetrics fm = gc.getFontMetrics (); + int width = columns * fm.getAverageCharWidth (); + int height = fm.getHeight (); + gc.dispose (); + GridDataFactory.fillDefaults().hint(typeCombo.computeSize(width, height)).applyTo(typeCombo); + } + + @Override + public void setInput(Object input) { + if(input instanceof IStructuredSelection) { + final Resource variable = ISelectionUtils.filterSingleSelection((IStructuredSelection)input, Resource.class); + if(variable != null) { + SimanticsUI.getSession().asyncRequest(new ReadRequest() { + @Override + public void run(ReadGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + + final ExpressionType[] expressionTypes; + if(graph.isInstanceOf(variable, sr.Auxiliary)) + expressionTypes = ExpressionTypes.auxiliaryExpressions; + else if(graph.isInstanceOf(variable, sr.Stock)) + expressionTypes = ExpressionTypes.stockExpressions; + else if(graph.isInstanceOf(variable, sr.Valve)) + expressionTypes = ExpressionTypes.valveExpressions; + else + expressionTypes = new ExpressionType[] {}; + + + final ExpressionType et; + Resource expression = graph.getPossibleObject(variable, sr.HasExpression); + if(expression == null) { + if(graph.isInstanceOf(variable, sr.Auxiliary)) { + et = ExpressionType.Auxiliary; + } + else if(graph.isInstanceOf(variable, sr.Valve)) { + et = ExpressionType.Auxiliary; + } + else if(graph.isInstanceOf(variable, sr.Stock)) { + et = ExpressionType.Stock; + } else + et = ExpressionType.Empty; + } else { + if(graph.isInstanceOf(expression, sr.NormalExpression)) { + et = ExpressionType.Auxiliary; + } else if (graph.isInstanceOf(expression, sr.StockExpression)) { + et = ExpressionType.Stock; + } else if (graph.isInstanceOf(expression, sr.ParameterExpression)) { + et = ExpressionType.Parameter; + } else if (graph.isInstanceOf(expression, sr.ConstantExpression)) { + et = ExpressionType.Constant; + } else if (graph.isInstanceOf(expression, sr.DelayExpression)) { + et = ExpressionType.Delay; + } else if (graph.isInstanceOf(expression, sr.LookupExpression)) { + et = ExpressionType.Lookup; + } else if (graph.isInstanceOf(expression, sr.WithLookupExpression)) { + et = ExpressionType.WithLookup; + } else { + et = ExpressionType.Empty; + } + } + + typeCombo.getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + setExpressionTypes(expressionTypes); + setExpressionType(et); + } + }); + + } + + }); + + } + } + } + + private void setExpressionTypes(ExpressionType[] expressionTypes) { + if(expressionTypes != null) { + for(ExpressionType et : expressionTypes) { + typeCombo.add(et.toString()); + } + } + } + + public void setExpressionType(ExpressionType expressionType) { + int index = typeCombo.indexOf(expressionType.toString()); + if (index > -1) { + typeCombo.select(index); + } + } } 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 index 00000000..4cebd8a7 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypes.java @@ -0,0 +1,23 @@ +package org.simantics.sysdyn.ui.properties.widgets; + +public class ExpressionTypes { + + public static enum ExpressionType {Auxiliary, Parameter, Constant, Lookup, WithLookup, Stock, Delay, Empty}; + + public static ExpressionType[] auxiliaryExpressions = new ExpressionType[] { + ExpressionType.Parameter, + ExpressionType.Auxiliary, + ExpressionType.Constant, + ExpressionType.Lookup, + ExpressionType.WithLookup}; + + public static ExpressionType[] valveExpressions = new ExpressionType[] { + ExpressionType.Auxiliary, + ExpressionType.Parameter, + ExpressionType.Constant, + ExpressionType.WithLookup}; + + public static ExpressionType[] stockExpressions = new ExpressionType[] { + ExpressionType.Stock}; + +} 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 index 00000000..1de6fb4f --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ShortcutTabWidget.java @@ -0,0 +1,126 @@ +package org.simantics.sysdyn.ui.properties.widgets; + +import java.util.Collection; +import java.util.HashSet; + +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.TabFolder; +import org.eclipse.swt.widgets.TabItem; +import org.eclipse.swt.widgets.Table; +import org.eclipse.swt.widgets.TableItem; +import org.simantics.browsing.ui.swt.widgets.impl.Widget; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.db.Builtins; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.request.Read; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.ui.SimanticsUI; +import org.simantics.utils.ui.ExceptionUtils; +import org.simantics.utils.ui.ISelectionUtils; + +public class ShortcutTabWidget implements Widget { + + TabFolder tabFolder; + TabItem variables; + TabItem functions; + Table variableTable; + Table functionTable; + + public ShortcutTabWidget(Composite parent, WidgetSupport support, int style) { + support.register(this); + + Composite composite = new Composite(parent, style); + GridDataFactory.fillDefaults().span(1, 2).grab(false, true).applyTo(composite); + GridLayoutFactory.fillDefaults().numColumns(2).applyTo(composite); + + tabFolder = new TabFolder (composite, SWT.NONE); + GridDataFactory.fillDefaults().grab(false, true).applyTo(tabFolder); + GridLayoutFactory.fillDefaults().applyTo(tabFolder); + variables = new TabItem(tabFolder, SWT.NULL); + variables.setText("Variables"); + variableTable = new Table (tabFolder, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION); + + variables.setControl(variableTable); + + functions = new TabItem(tabFolder, SWT.NULL); + functions.setText("Functions"); + functionTable = new Table (tabFolder, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION); + TableItem item = new TableItem(functionTable, SWT.NONE); + item.setText("min()"); + item.setData("min({ })"); + item = new TableItem(functionTable, SWT.NONE); + item.setText("max()"); + item.setData("max({ })"); + item = new TableItem(functionTable, SWT.NONE); + item.setText("abs()"); + item.setData("abs({ })"); + item = new TableItem(functionTable, SWT.NONE); + item.setText("if then else"); + item.setData("if then else"); + functions.setControl(functionTable); + } + + @Override + public void setInput(Object input) { + if(input instanceof IStructuredSelection) { + final Resource variable = ISelectionUtils.filterSingleSelection((IStructuredSelection)input, Resource.class); + if(variable != null) { + HashSet dependencies = getDependencies(variable); + TableItem item; + for(String d : dependencies) { + item = new TableItem(variableTable, SWT.NONE); + item.setText(d); + item.setData(d); + } + } + } + } + + // Returns the names of the related variables (dependencies) + private HashSet getDependencies(final Resource r){ + HashSet variables = null; + if(r != null) + try { + variables = SimanticsUI.getSession().syncRequest(new Read>() { + + @Override + public HashSet perform(ReadGraph graph) + throws DatabaseException { + HashSet variables = new HashSet(); + SysdynResource sr = SysdynResource.getInstance(graph); + Builtins b = graph.getBuiltins(); + + Collection dependencies = graph.getObjects(r, sr.IsHeadOf); + + for(Resource d : dependencies) { + if(graph.isInstanceOf(d, sr.Dependency)) { + Resource tail = graph.getPossibleObject(d, sr.HasTail); + if(tail != null) { + Object name = graph.getPossibleRelatedValue(tail, b.HasName); + if(name != null) + variables.add((String)name); + } + } + } + + return variables; + + } + + + }); + } catch (DatabaseException ee) { + ExceptionUtils.logAndShowError(ee); + } + + return variables; + } + + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/UnitWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/UnitWidget.java index 658b8c31..c93676fd 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/UnitWidget.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/UnitWidget.java @@ -75,9 +75,7 @@ public class UnitWidget implements Widget { @Override public void execute(final String text) { - - if(text == null) return; - + unitCombo.getDisplay().asyncExec(new Runnable() { @Override @@ -92,6 +90,9 @@ public class UnitWidget implements Widget { unitCombo.add(unit); } } + + if(text == null) return; + if(unitCombo.indexOf(text) >= 0) unitCombo.select(unitCombo.indexOf(text)); else -- 2.47.1