From 6b4428b03fa75588e95c08df61bcd9989aa66dbb Mon Sep 17 00:00:00 2001 From: lempinen Date: Mon, 11 Jan 2010 13:14:12 +0000 Subject: [PATCH] Different expression types, some examples git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13473 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/elements/TextElement.java | 5 - .../sysdyn/ui/equation/EquationView.java | 88 ++++--- .../ui/equation/ExpressionController.java | 229 ++++++++++++++++++ .../sysdyn/ui/equation/TypeSelector.java | 41 ---- .../AuxiliaryExpressionViewFactor.java | 139 +++++++++++ ...java => ConstantExpressionViewFactor.java} | 123 +++++----- .../DelayExpressionViewFactor.java | 40 +++ .../equation/expressions/ExpressionView.java | 21 -- .../expressions/IExpressionViewFactor.java | 47 ++++ .../LookupExpressionViewFactor.java | 40 +++ .../ParameterExpressionViewFactor.java | 142 +++++++++++ ...ew.java => StockExpressionViewFactor.java} | 130 +++++----- .../WithLookupExpressionViewFactor.java | 186 ++++++++++++++ .../org/simantics/sysdyn/SysdynResource.java | 15 ++ sysdyn_ontologies/sysdyn.graph | 16 ++ 15 files changed, 1034 insertions(+), 228 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionController.java delete mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/TypeSelector.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/AuxiliaryExpressionViewFactor.java rename org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/{NormalExpressionView.java => ConstantExpressionViewFactor.java} (57%) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/DelayExpressionViewFactor.java delete mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionView.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/IExpressionViewFactor.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/LookupExpressionViewFactor.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ParameterExpressionViewFactor.java rename org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/{StockExpressionView.java => StockExpressionViewFactor.java} (61%) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/WithLookupExpressionViewFactor.java diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/TextElement.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/TextElement.java index 23e93b93..0d6b6e73 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/TextElement.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/TextElement.java @@ -17,10 +17,8 @@ import org.simantics.h2d.event.handler.IEventHandler; import org.simantics.h2d.node.ITextListener; import org.simantics.h2d.node.TextNode; import org.simantics.objmap.annotations.GraphType; -import org.simantics.objmap.annotations.RelatedElement; import org.simantics.objmap.annotations.RelatedValue; import org.simantics.scenegraph.g2d.G2DParentNode; -import org.simantics.sysdyn.ui.equation.expressions.ExpressionView; @GraphType("http://www.simantics.org/Sysdyn#Auxiliary") public class TextElement extends RectangularElement implements Movable, Connectable, IEventHandler { @@ -40,9 +38,6 @@ public class TextElement extends RectangularElement implements Movable, Connecta @RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#HasType") public String type = "Real"; - @RelatedElement("http://www.simantics.org/Sysdyn#HasExpression") - public ExpressionView expression; - // Auxiliary fields double textX; double textY; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java index 785deb0b..f3ac8a38 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java @@ -17,9 +17,7 @@ import org.eclipse.swt.events.MouseListener; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.layout.RowLayout; -import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.TabFolder; import org.eclipse.swt.widgets.TabItem; @@ -37,9 +35,6 @@ import org.simantics.db.exception.DatabaseException; import org.simantics.db.procedure.Listener; import org.simantics.db.request.Read; import org.simantics.sysdyn.SysdynResource; -import org.simantics.sysdyn.ui.equation.expressions.ExpressionView; -import org.simantics.sysdyn.ui.equation.expressions.NormalExpressionView; -import org.simantics.sysdyn.ui.equation.expressions.StockExpressionView; import org.simantics.ui.SimanticsUI; import org.simantics.utils.ui.ExceptionUtils; @@ -59,20 +54,25 @@ public class EquationView extends ViewPart implements ISelectionListener { Text nameText; - Composite equationComposite; + + Composite emptyComposite; String originalText; TabFolder tabFolder; TabItem variables; Table variableTable; TabItem functions; + Composite unitsAndRange; - Composite variableType; - Composite emptyComposite; - ExpressionView expressionView; + + Composite typeComposite; + ExpressionController expressionController; + Composite unitComposite; UnitSelector unitSelector; - + + Composite equationComposite; + org.eclipse.ui.IPartListener2 focusLostListener; @Override @@ -91,7 +91,11 @@ public class EquationView extends ViewPart implements ISelectionListener { { if(partRef.getPart(false) instanceof EquationView) { // Save expressions - if(expressionView != null) expressionView.save(); + if(expressionController != null) { + expressionController.save(); + } + + // Save unit if(unitSelector != null) { String unitNew = unitSelector.save(); @@ -119,16 +123,15 @@ public class EquationView extends ViewPart implements ISelectionListener { GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText); // After the variable name: Variable type - variableType = new Composite(parent, SWT.NONE); - GridLayoutFactory.fillDefaults().numColumns(3).spacing(3, 0).applyTo(variableType); + Composite variableType = new Composite(parent, SWT.NONE); + GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 0).applyTo(variableType); GridDataFactory.fillDefaults().applyTo(variableType); - Label label = new Label(variableType, SWT.SINGLE); - label.setText("Type:"); - Combo comboDropDown = new Combo(variableType, SWT.DROP_DOWN | SWT.BORDER); - comboDropDown.add("Auxiliary"); - comboDropDown.add("Parameter"); - comboDropDown.add("Constant"); - + + typeComposite = new Composite(variableType, SWT.NONE); + GridDataFactory.fillDefaults().applyTo(typeComposite); + GridLayoutFactory.fillDefaults().applyTo(typeComposite); + expressionController = new ExpressionController(typeComposite, SWT.NONE, variable, equationComposite); + unitComposite = new Composite(variableType, SWT.NONE); GridDataFactory.fillDefaults().applyTo(unitComposite); GridLayoutFactory.fillDefaults().applyTo(unitComposite); @@ -148,11 +151,11 @@ public class EquationView extends ViewPart implements ISelectionListener { TableItem item = variableTable.getItem(new Point(e.x, e.y)); if(item != null) { final String var = item.getText(); - expressionView.getDisplay().asyncExec(new Runnable() { + equationComposite.getDisplay().asyncExec(new Runnable() { @Override public void run() { - expressionView.replaceSelection(var); + expressionController.getExpressionViewFactor().replaceSelection(var); } }); } @@ -160,12 +163,12 @@ public class EquationView extends ViewPart implements ISelectionListener { @Override public void mouseDown(MouseEvent e) { - expressionView.focus(); + expressionController.getExpressionViewFactor().focus(); } @Override public void mouseUp(MouseEvent e) { - expressionView.focus(); + expressionController.getExpressionViewFactor().focus(); } }); @@ -201,10 +204,10 @@ public class EquationView extends ViewPart implements ISelectionListener { GridLayoutFactory.fillDefaults().applyTo(emptyComposite); // Below variable name: Equation + // equationComposite content is controlled by ExpressionController equationComposite = new Composite(parent, SWT.NONE); GridDataFactory.fillDefaults().span(2, 1).grab(true, true).applyTo(equationComposite); GridLayoutFactory.fillDefaults().applyTo(equationComposite); - expressionView = new NormalExpressionView(equationComposite, SWT.NONE, variable); // Lower part of the view: Units and Range unitsAndRange = new Composite(parent, SWT.NONE); @@ -212,7 +215,7 @@ public class EquationView extends ViewPart implements ISelectionListener { RowLayout layout = new RowLayout(); layout.spacing = 3; unitsAndRange.setLayout(layout); - label = new Label(unitsAndRange, SWT.SINGLE); + Label label = new Label(unitsAndRange, SWT.SINGLE); label.setText("Range:"); label.setAlignment(SWT.RIGHT); Composite c = new Composite(unitsAndRange, SWT.NONE); @@ -300,19 +303,12 @@ public class EquationView extends ViewPart implements ISelectionListener { unitSelector = new UnitSelector(unitComposite, SWT.NONE, variable, units); unitComposite.layout(); - nameText.setText(result.name); + if(expressionController!= null) expressionController.dispose(); + expressionController = new ExpressionController(typeComposite, SWT.NONE, variable, equationComposite); + typeComposite.layout(); - for(Control c : equationComposite.getChildren()) { - c.dispose(); - } + nameText.setText(result.name); - ExpressionView ev = null; - if (result.type.equalsIgnoreCase("NORMAL")) - ev = new NormalExpressionView(equationComposite, SWT.NONE, variable); - else if (result.type.equalsIgnoreCase("STOCK")) - ev = new StockExpressionView(equationComposite, SWT.NONE, variable); - expressionView = ev; - equationComposite.layout(); } }); @@ -331,8 +327,15 @@ public class EquationView extends ViewPart implements ISelectionListener { @Override public void setFocus() { - if(expressionView != null) - expressionView.focus(); + if(expressionController.getExpressionViewFactor() != null) + expressionController.getExpressionViewFactor().focus(); + } + + @Override + public void dispose() { + disposed = true; + getViewSite().getPage().removePartListener(focusLostListener); + super.dispose(); } // Returns all the used units in the model @@ -414,11 +417,4 @@ public class EquationView extends ViewPart implements ISelectionListener { return variables; } - @Override - public void dispose() { - disposed = true; - getViewSite().getPage().removePartListener(focusLostListener); - super.dispose(); - } - } \ No newline at end of file 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 new file mode 100644 index 00000000..1f60cd03 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionController.java @@ -0,0 +1,229 @@ +package org.simantics.sysdyn.ui.equation; + +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; +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.Control; +import org.eclipse.swt.widgets.Label; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.ServiceException; +import org.simantics.db.request.Read; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.equation.expressions.AuxiliaryExpressionViewFactor; +import org.simantics.sysdyn.ui.equation.expressions.ConstantExpressionViewFactor; +import org.simantics.sysdyn.ui.equation.expressions.DelayExpressionViewFactor; +import org.simantics.sysdyn.ui.equation.expressions.IExpressionViewFactor; +import org.simantics.sysdyn.ui.equation.expressions.LookupExpressionViewFactor; +import org.simantics.sysdyn.ui.equation.expressions.ParameterExpressionViewFactor; +import org.simantics.sysdyn.ui.equation.expressions.StockExpressionViewFactor; +import org.simantics.sysdyn.ui.equation.expressions.WithLookupExpressionViewFactor; +import org.simantics.ui.SimanticsUI; + +public class ExpressionController extends Composite { + + Combo typeCombo; + Resource variable; + Map data; + Map origData; + Composite equationComposite; + IExpressionViewFactor expressionViewFactor; + + //private enum VariableClass {Auxiliary, Valve, Stock}; + private enum ExpressionType {Auxiliary, Parameter, Constant, Lookup, WithLookup, Stock, Delay}; + + ExpressionType[] auxiliaryExpressions = new ExpressionType[] { + ExpressionType.Parameter, + ExpressionType.Auxiliary, + ExpressionType.Constant, + ExpressionType.Lookup, + ExpressionType.WithLookup}; + + ExpressionType[] valveExpressions = new ExpressionType[] { + ExpressionType.Auxiliary, + ExpressionType.Parameter, + ExpressionType.Constant, + ExpressionType.WithLookup}; + + ExpressionType[] stockExpressions = new ExpressionType[] { + ExpressionType.Stock, + ExpressionType.Delay}; + + ExpressionType[] expressionTypes; + ExpressionType originalExpressionType; + + HashMap> expressions = new HashMap>(); + + public ExpressionController(final Composite parent, int style, final Resource variable, Composite equationComposite) { + super(parent, style); + this.variable = variable; + this.equationComposite = equationComposite; + this.expressionViewFactor = null; + this.data = new HashMap(); + this.origData = new HashMap(); + GridDataFactory.fillDefaults().applyTo(this); + GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(this); + + if(variable != null) { + try { + expressionTypes = SimanticsUI.getSession().syncRequest(new Read< ExpressionType[]>() { + + @Override + public ExpressionType[] perform(ReadGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + ExpressionType[] expressionTypes; + if(graph.isInstanceOf(variable, sr.Auxiliary)) { + expressionTypes = auxiliaryExpressions; + originalExpressionType = ExpressionType.Auxiliary; + } + else if(graph.isInstanceOf(variable, sr.Valve)) { + expressionTypes = valveExpressions; + originalExpressionType = ExpressionType.Auxiliary; + } + else if(graph.isInstanceOf(variable, sr.Stock)) { + expressionTypes = stockExpressions; + originalExpressionType = ExpressionType.Stock; + } + else + expressionTypes = null; + + ExpressionType oet = getOriginalExpressionType(graph, graph.getPossibleObject(variable, sr.HasExpression)); + if (oet != null) originalExpressionType = oet; + return expressionTypes; + } + }); + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + } + + Label label = new Label(this, SWT.SINGLE ); + label.setFont(EquationView.FONT); + label.setText("Type:"); + GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label); + + typeCombo = new Combo(this, SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY); + typeCombo.setFont(EquationView.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); + + typeCombo.removeAll(); + if(this.expressionTypes != null) { + for(ExpressionType et : this.expressionTypes) { + typeCombo.add(et.toString()); + } + } + + if(originalExpressionType != null) { + displayExpressionView(variable, originalExpressionType, true); + } + + typeCombo.addSelectionListener(new SelectionListener() { + + @Override + public void widgetSelected(SelectionEvent e) { + displayExpressionView(variable, ExpressionType.valueOf(typeCombo.getItem(typeCombo.getSelectionIndex())), false); + + } + + @Override + public void widgetDefaultSelected(SelectionEvent e) { + } + }); + } + + private ExpressionType getOriginalExpressionType(ReadGraph graph, Resource expression) { + if(expression == null) + return null; + SysdynResource sr = SysdynResource.getInstance(graph); + ExpressionType et = null; + try { + 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.Auxiliary; + } + } catch (ServiceException e) { + e.printStackTrace(); + } + + return et; + } + + + private void displayExpressionView(Resource variable, ExpressionType et, boolean original) { + int index = typeCombo.indexOf(et.toString()); + if (index > -1) { + typeCombo.select(index); + } + + IExpressionViewFactor evf = null; + switch (et) { + case Auxiliary: + evf = new AuxiliaryExpressionViewFactor(); break; + case Parameter: + evf = new ParameterExpressionViewFactor(); break; + case Constant: + evf = new ConstantExpressionViewFactor(); break; + case Lookup: + evf = new LookupExpressionViewFactor(); break; + case WithLookup: + evf = new WithLookupExpressionViewFactor(); break; + case Stock: + evf = new StockExpressionViewFactor(); break; + case Delay: + evf = new DelayExpressionViewFactor(); break; + default: ; + } + if (evf != null) { + for(Control c : equationComposite.getChildren()) { + c.dispose(); + } + if(original) { + evf.readData(variable, data); + for(String key : data.keySet()) { + origData.put(key, data.get(key)); + } + } + evf.createView(equationComposite, data); + expressionViewFactor = evf; + equationComposite.layout(); + } + } + + public IExpressionViewFactor getExpressionViewFactor() { + return this.expressionViewFactor; + } + + public void save() { + this.expressionViewFactor.writeData(variable, data); + } +} 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 deleted file mode 100644 index 277b94c8..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/TypeSelector.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.simantics.sysdyn.ui.equation; - -import org.eclipse.jface.layout.GridDataFactory; -import org.eclipse.jface.layout.GridLayoutFactory; -import org.eclipse.swt.widgets.Combo; -import org.eclipse.swt.widgets.Composite; -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.ui.SimanticsUI; - -public class TypeSelector extends Composite { - - Combo typeCombo; - Resource variable; - - public TypeSelector(Composite parent, int style, final Resource variable) { - super(parent, style); - - this.variable = variable; - GridDataFactory.fillDefaults().applyTo(this); - GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(this); - - if(variable != null) { - try { - SimanticsUI.getSession().syncRequest(new Read() { - - @Override - public String perform(ReadGraph graph) throws DatabaseException { - - - return null; - } - }); - } catch (DatabaseException e1) { - e1.printStackTrace(); - } - } - } -} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/AuxiliaryExpressionViewFactor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/AuxiliaryExpressionViewFactor.java new file mode 100644 index 00000000..6828ef1d --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/AuxiliaryExpressionViewFactor.java @@ -0,0 +1,139 @@ +package org.simantics.sysdyn.ui.equation.expressions; + +import java.util.Map; + +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.FocusAdapter; +import org.eclipse.swt.events.FocusEvent; +import org.eclipse.swt.events.KeyAdapter; +import org.eclipse.swt.events.KeyEvent; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Text; +import org.simantics.db.Builtins; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.request.Read; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.ui.SimanticsUI; + +public class AuxiliaryExpressionViewFactor implements IExpressionViewFactor { + + private Label equationLabel; + private Text expression; + private Point lastSelection = new Point(0,0); + + @Override + public void createView(Composite parent, Map data) { + + final String equation = data.get("equation") != null ? (String)data.get("equation") : ""; + + GridDataFactory.fillDefaults().grab(true, true).applyTo(parent); + GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent); + equationLabel = new Label(parent, SWT.NONE); + equationLabel.setFont(FONT); + equationLabel.setText("="); + GridDataFactory.fillDefaults().applyTo(equationLabel); + + + expression = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER); + expression.setFont(FONT); + expression.setText(equation); + GridDataFactory.fillDefaults().grab(true, true).applyTo(expression); + + expression.addFocusListener(new FocusAdapter() { + + @Override + public void focusLost(FocusEvent e) { + lastSelection = expression.getSelection(); + } + }); + + expression.addKeyListener(new KeyAdapter() { + + @Override + public void keyPressed(KeyEvent e) { + if(e.keyCode == SWT.ESC) { + expression.setText(equation); + } + } + + }); + + + } + + @Override + public void readData(final Resource variable, Map data) { + String equation = null; + if (variable != null && data.get("equation") == null) { + try { + equation = SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public String perform(ReadGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Resource expression = graph.getPossibleObject(variable, sr.HasExpression); + if (expression != null && graph.isInstanceOf(expression, sr.NormalExpression)) { + return graph.getRelatedValue(expression, sr.HasEquation); + } else { + return ""; + } + } + + }); + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + data.put("equation", equation); + } + } + + @Override + public void writeData(final Resource variable, Map data) { + final String currentText = expression.getText(); + if(currentText != null) { + data.put("equation", currentText); + SimanticsUI.getSession().asyncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph g) + throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(g); + Resource expression = g.getPossibleObject(variable, sr.HasExpression); + Builtins b = g.getBuiltins(); + if(expression != null) { + g.deny(variable, sr.HasExpression); + } + expression = g.newResource(); + g.claim(expression, b.InstanceOf, sr.NormalExpression); + g.claim(variable, sr.HasExpression, expression); + g.claimValue(expression, sr.HasEquation, currentText); + } + + }); + } + } + + @Override + public void focus() { + if(this.expression != null) this.expression.forceFocus(); + } + + @Override + public void replaceSelection(String var) { + if(this.expression != null) { + String oldExpression = expression.getText(); + if(lastSelection == null) + lastSelection = new Point(0,0); + String newExpression = oldExpression.substring(0, lastSelection.x) + var + oldExpression.substring(lastSelection.y); + expression.setText(newExpression); + expression.setSelection(lastSelection.x + var.length()); + } + } +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/NormalExpressionView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ConstantExpressionViewFactor.java similarity index 57% rename from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/NormalExpressionView.java rename to org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ConstantExpressionViewFactor.java index 73144c12..708ef86c 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/NormalExpressionView.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ConstantExpressionViewFactor.java @@ -1,5 +1,7 @@ package org.simantics.sysdyn.ui.equation.expressions; +import java.util.Map; + import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.swt.SWT; @@ -18,57 +20,30 @@ import org.simantics.db.WriteGraph; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.db.request.Read; -import org.simantics.objmap.annotations.GraphType; -import org.simantics.objmap.annotations.RelatedValue; import org.simantics.sysdyn.SysdynResource; import org.simantics.ui.SimanticsUI; -@GraphType("http://www.simantics.org/Sysdyn#NormalExpression") -public class NormalExpressionView extends ExpressionView { +public class ConstantExpressionViewFactor implements IExpressionViewFactor { - Label equationLabel; - Text expression; - Point lastSelection = new Point(0,0); + private Label equationLabel; + private Text expression; + private Point lastSelection = new Point(0,0); - @RelatedValue("http://www.simantics.org/Sysdyn#HasEquation") - private String equation; + @Override + public void createView(Composite parent, Map data) { - public NormalExpressionView(Composite parent, int style, final Resource variable) { - super(parent, style, variable); + final String equation = data.get("equation") != null ? (String)data.get("equation") : ""; - if (variable != null) { - try { - SimanticsUI.getSession().syncRequest(new Read() { - @Override - public String perform(ReadGraph graph) throws DatabaseException { - SysdynResource sr = SysdynResource.getInstance(graph); - Resource expression = graph.getPossibleObject(variable, sr.HasExpression); - if (expression != null && graph.isInstanceOf(expression, sr.NormalExpression)) { - equation = graph.getRelatedValue(expression, sr.HasEquation); - } else { - equation = ""; - } - return null; - } - - }); - } catch (DatabaseException e1) { - e1.printStackTrace(); - } - } else { - equation = ""; - } - - GridDataFactory.fillDefaults().grab(true, true).applyTo(this); - GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(this); - equationLabel = new Label(this, SWT.NONE); + GridDataFactory.fillDefaults().grab(true, true).applyTo(parent); + GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent); + equationLabel = new Label(parent, SWT.NONE); equationLabel.setFont(FONT); equationLabel.setText("="); GridDataFactory.fillDefaults().applyTo(equationLabel); - expression = new Text(this, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER); + expression = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER); expression.setFont(FONT); expression.setText(equation); GridDataFactory.fillDefaults().grab(true, true).applyTo(expression); @@ -87,17 +62,61 @@ public class NormalExpressionView extends ExpressionView { public void keyPressed(KeyEvent e) { if(e.keyCode == SWT.ESC) { expression.setText(equation); - } + } } }); + + } + + @Override + public void focus() { + if(this.expression != null) this.expression.forceFocus(); + } + + @Override + public void readData(final Resource variable, Map data) { + String equation = null; + if (variable != null && data.get("equation") == null) { + try { + equation = SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public String perform(ReadGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Resource expression = graph.getPossibleObject(variable, sr.HasExpression); + if (expression != null && graph.isInstanceOf(expression, sr.ConstantExpression)) { + return graph.getRelatedValue(expression, sr.HasEquation); + } else { + return ""; + } + } + + }); + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + data.put("equation", equation); + } + } + + @Override + public void replaceSelection(String var) { + if(this.expression != null) { + String oldExpression = expression.getText(); + if(lastSelection == null) + lastSelection = new Point(0,0); + String newExpression = oldExpression.substring(0, lastSelection.x) + var + oldExpression.substring(lastSelection.y); + expression.setText(newExpression); + expression.setSelection(lastSelection.x + var.length()); + } } @Override - public void save() { + public void writeData(final Resource variable, Map data) { final String currentText = expression.getText(); - if(equation != null && !equation.equals(currentText)) { + if(currentText != null) { SimanticsUI.getSession().asyncRequest(new WriteRequest() { @Override @@ -106,27 +125,17 @@ public class NormalExpressionView extends ExpressionView { SysdynResource sr = SysdynResource.getInstance(g); Resource expression = g.getPossibleObject(variable, sr.HasExpression); Builtins b = g.getBuiltins(); - if(expression == null) { - expression = g.newResource(); - g.claim(expression, b.InstanceOf, sr.NormalExpression); - g.claim(variable, sr.HasExpression, expression); + if(expression != null) { + g.deny(variable, sr.HasExpression); } + expression = g.newResource(); + g.claim(expression, b.InstanceOf, sr.ConstantExpression); + g.claim(variable, sr.HasExpression, expression); g.claimValue(expression, sr.HasEquation, currentText); } }); - } - - } - - public void replaceSelection(String text) { - String oldExpression = expression.getText(); - String newExpression = oldExpression.substring(0, lastSelection.x) + text + oldExpression.substring(lastSelection.y); - expression.setText(newExpression); - expression.setSelection(lastSelection.x + text.length()); + } } - public void focus() { - expression.forceFocus(); - } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/DelayExpressionViewFactor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/DelayExpressionViewFactor.java new file mode 100644 index 00000000..a2545511 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/DelayExpressionViewFactor.java @@ -0,0 +1,40 @@ +package org.simantics.sysdyn.ui.equation.expressions; + +import java.util.Map; + +import org.eclipse.swt.widgets.Composite; +import org.simantics.db.Resource; + +public class DelayExpressionViewFactor implements IExpressionViewFactor { + + @Override + public void createView(Composite parent, Map data) { + // TODO Auto-generated method stub + + } + + @Override + public void focus() { + // TODO Auto-generated method stub + + } + + @Override + public void readData(Resource variable, Map data) { + // TODO Auto-generated method stub + + } + + @Override + public void replaceSelection(String var) { + // TODO Auto-generated method stub + + } + + @Override + public void writeData(Resource variable, Map data) { + // TODO Auto-generated method stub + + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionView.java deleted file mode 100644 index df47203c..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionView.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.simantics.sysdyn.ui.equation.expressions; - -import org.eclipse.swt.SWT; -import org.eclipse.swt.graphics.Font; -import org.eclipse.swt.widgets.Composite; -import org.simantics.db.Resource; - -abstract public class ExpressionView extends Composite { - - static final Font FONT = new Font(null, "Courier New", 12, SWT.NORMAL); - protected Resource variable; - - public ExpressionView(Composite parent, int style, Resource variable) { - super(parent, style); - this.variable = variable; - } - - abstract public void save(); - abstract public void replaceSelection(String text); - abstract public void focus(); -} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/IExpressionViewFactor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/IExpressionViewFactor.java new file mode 100644 index 00000000..0cee3b46 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/IExpressionViewFactor.java @@ -0,0 +1,47 @@ +package org.simantics.sysdyn.ui.equation.expressions; + +import java.util.Map; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.widgets.Composite; +import org.simantics.db.Resource; + +public interface IExpressionViewFactor { + + static final Font FONT = new Font(null, "Courier New", 12, SWT.NORMAL); + + /** + * Creates expression type specific user interface and initializes + * the components by the given data. The given map is linked + * to the ui components and updated. + * @param parent + * @param data + */ + void createView(Composite parent, Map data); + /** + * Reads expression type specific data from database. + * @param variable + * @return + */ + void readData(Resource variable, Map data); + + /** + * Writes expression type specific data to database. + * @param variable + * @return + */ + void writeData(Resource variable, Map data); + + /** + * Focuses to some part of the expression view. + */ + void focus(); + + /** + * Replaces the current selection with a variable name. + * @param var + */ + void replaceSelection(String var); + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/LookupExpressionViewFactor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/LookupExpressionViewFactor.java new file mode 100644 index 00000000..129c64ce --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/LookupExpressionViewFactor.java @@ -0,0 +1,40 @@ +package org.simantics.sysdyn.ui.equation.expressions; + +import java.util.Map; + +import org.eclipse.swt.widgets.Composite; +import org.simantics.db.Resource; + +public class LookupExpressionViewFactor implements IExpressionViewFactor { + + @Override + public void createView(Composite parent, Map data) { + // TODO Auto-generated method stub + + } + + @Override + public void focus() { + // TODO Auto-generated method stub + + } + + @Override + public void readData(Resource variable, Map data) { + // TODO Auto-generated method stub + + } + + @Override + public void replaceSelection(String var) { + // TODO Auto-generated method stub + + } + + @Override + public void writeData(Resource variable, Map data) { + // TODO Auto-generated method stub + + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ParameterExpressionViewFactor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ParameterExpressionViewFactor.java new file mode 100644 index 00000000..a5c02fbe --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ParameterExpressionViewFactor.java @@ -0,0 +1,142 @@ +package org.simantics.sysdyn.ui.equation.expressions; + +import java.util.Map; + +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.FocusAdapter; +import org.eclipse.swt.events.FocusEvent; +import org.eclipse.swt.events.KeyAdapter; +import org.eclipse.swt.events.KeyEvent; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Text; +import org.simantics.db.Builtins; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.request.Read; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.ui.SimanticsUI; + +public class ParameterExpressionViewFactor implements IExpressionViewFactor { + + private Label equationLabel; + private Text expression; + private Point lastSelection = new Point(0,0); + + @Override + public void createView(Composite parent, Map data) { + + final String equation = data.get("equation") != null ? (String)data.get("equation") : ""; + + + GridDataFactory.fillDefaults().grab(true, true).applyTo(parent); + GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent); + equationLabel = new Label(parent, SWT.NONE); + equationLabel.setFont(FONT); + equationLabel.setText("="); + GridDataFactory.fillDefaults().applyTo(equationLabel); + + + expression = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER); + expression.setFont(FONT); + expression.setText(equation); + GridDataFactory.fillDefaults().grab(true, true).applyTo(expression); + + expression.addFocusListener(new FocusAdapter() { + + @Override + public void focusLost(FocusEvent e) { + lastSelection = expression.getSelection(); + } + }); + + expression.addKeyListener(new KeyAdapter() { + + @Override + public void keyPressed(KeyEvent e) { + if(e.keyCode == SWT.ESC) { + expression.setText(equation); + } + } + + }); + + + } + + @Override + public void focus() { + if(this.expression != null) this.expression.forceFocus(); + } + + @Override + public void readData(final Resource variable, Map data) { + String equation = null; + if (variable != null && data.get("equation") == null) { + try { + equation = SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public String perform(ReadGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Resource expression = graph.getPossibleObject(variable, sr.HasExpression); + if (expression != null && graph.isInstanceOf(expression, sr.ParameterExpression)) { + return graph.getRelatedValue(expression, sr.HasEquation); + } else { + return ""; + } + } + + }); + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + data.put("equation", equation); + } + } + + @Override + public void replaceSelection(String var) { + if(this.expression != null) { + String oldExpression = expression.getText(); + if(lastSelection == null) + lastSelection = new Point(0,0); + String newExpression = oldExpression.substring(0, lastSelection.x) + var + oldExpression.substring(lastSelection.y); + expression.setText(newExpression); + expression.setSelection(lastSelection.x + var.length()); + } + } + + @Override + public void writeData(final Resource variable, Map data) { + final String currentText = expression.getText(); + if(currentText != null) { + SimanticsUI.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph g) + throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(g); + Resource expression = g.getPossibleObject(variable, sr.HasExpression); + Builtins b = g.getBuiltins(); + if(expression != null) { + g.deny(variable, sr.HasExpression); + } + expression = g.newResource(); + g.claim(expression, b.InstanceOf, sr.ParameterExpression); + g.claim(variable, sr.HasExpression, expression); + g.claimValue(expression, sr.HasEquation, currentText); + } + + }); + } + } + +} + diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/StockExpressionView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/StockExpressionViewFactor.java similarity index 61% rename from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/StockExpressionView.java rename to org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/StockExpressionViewFactor.java index d7d24ac7..f10447b9 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/StockExpressionView.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/StockExpressionViewFactor.java @@ -1,6 +1,7 @@ package org.simantics.sysdyn.ui.equation.expressions; import java.util.Collection; +import java.util.Map; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; @@ -20,13 +21,10 @@ import org.simantics.db.WriteGraph; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.db.request.Read; -import org.simantics.objmap.annotations.GraphType; -import org.simantics.objmap.annotations.RelatedValue; import org.simantics.sysdyn.SysdynResource; import org.simantics.ui.SimanticsUI; -@GraphType("http://www.simantics.org/Sysdyn#StockExpression") -public class StockExpressionView extends ExpressionView { +public class StockExpressionViewFactor implements IExpressionViewFactor { Label integralLabel; Text integral; @@ -34,57 +32,37 @@ public class StockExpressionView extends ExpressionView { Text expression; Point lastSelection = new Point(0,0); - @RelatedValue("http://www.simantics.org/Sysdyn#HasInitialEquation") - private String initialEquation; - - public StockExpressionView(Composite parent, int style, final Resource variable) { - super(parent, style, variable); - - try { - SimanticsUI.getSession().syncRequest(new Read() { + @Override + public void createView(Composite parent, Map data) { - @Override - public String perform(ReadGraph graph) throws DatabaseException { - SysdynResource sr = SysdynResource.getInstance(graph); - Resource expression = graph.getPossibleObject(variable, sr.HasExpression); - if (expression != null && graph.isInstanceOf(expression, sr.StockExpression)) { - initialEquation = graph.getRelatedValue(expression, sr.HasInitialEquation); - } else { - initialEquation = ""; - } - return null; - } + final String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : ""; + final String integralEquation = data.get("integral") != null ? (String)data.get("integral") : ""; - }); - } catch (DatabaseException e1) { - e1.printStackTrace(); - } - - GridDataFactory.fillDefaults().grab(true, true).applyTo(this); - GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(this); + GridDataFactory.fillDefaults().grab(true, true).applyTo(parent); + GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent); - integralLabel = new Label(this, SWT.NONE); + integralLabel = new Label(parent, SWT.NONE); integralLabel.setFont(FONT); integralLabel.setText("Integral"); GridDataFactory.fillDefaults().applyTo(integralLabel); - - integral = new Text(this, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER); + + integral = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER); integral.setEditable(false); integral.setFont(FONT); - integral.setText(getIntegral()); + integral.setText(integralEquation); GridDataFactory.fillDefaults().grab(true, true).applyTo(integral); - - equationLabel = new Label(this, SWT.NONE); + + equationLabel = new Label(parent, SWT.NONE); equationLabel.setFont(FONT); equationLabel.setText("Initial\nValue"); GridDataFactory.fillDefaults().applyTo(equationLabel); - expression = new Text(this, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER); + expression = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER); expression.setFont(FONT); expression.setText(initialEquation); GridDataFactory.fillDefaults().grab(true, true).applyTo(expression); - + expression.addFocusListener(new FocusAdapter() { @Override @@ -106,19 +84,39 @@ public class StockExpressionView extends ExpressionView { } - @Override - public void replaceSelection(String text) { - String oldExpression = expression.getText(); - String newExpression = oldExpression.substring(0, lastSelection.x) + text + oldExpression.substring(lastSelection.y); - expression.setText(newExpression); - expression.setSelection(lastSelection.x + text.length()); + public void readData(final Resource variable, Map data) { + String initialEquation = null; + + if (variable != null && data.get("initialEquation") == null) { + try { + initialEquation = SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public String perform(ReadGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Resource expression = graph.getPossibleObject(variable, sr.HasExpression); + if (expression != null && graph.isInstanceOf(expression, sr.StockExpression)) { + return graph.getRelatedValue(expression, sr.HasInitialEquation); + } else { + return ""; + } + } + + }); + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + data.put("initialEquation", initialEquation); + } + + data.put("integral", getIntegral(variable)); } @Override - public void save() { + public void writeData(final Resource variable, Map data) { final String currentText = expression.getText(); - if(initialEquation != null && !initialEquation.equals(currentText)) { + if(currentText != null) { SimanticsUI.getSession().asyncRequest(new WriteRequest() { @Override @@ -127,23 +125,20 @@ public class StockExpressionView extends ExpressionView { SysdynResource sr = SysdynResource.getInstance(g); Resource expression = g.getPossibleObject(variable, sr.HasExpression); Builtins b = g.getBuiltins(); - if(expression == null) { - expression = g.newResource(); - g.claim(expression, b.InstanceOf, sr.StockExpression); - g.claim(variable, sr.HasExpression, expression); + if(expression != null) { + g.deny(variable, sr.HasExpression); } + expression = g.newResource(); + g.claim(expression, b.InstanceOf, sr.StockExpression); + g.claim(variable, sr.HasExpression, expression); g.claimValue(expression, sr.HasInitialEquation, currentText); } }); } } - - public void focus() { - expression.forceFocus(); - } - - private String getIntegral() { + + private String getIntegral(final Resource variable) { String integral = ""; try { integral = SimanticsUI.getSession().syncRequest(new Read() { @@ -154,7 +149,7 @@ public class StockExpressionView extends ExpressionView { Builtins b = graph.getBuiltins(); Collection heads = graph.getObjects(variable, sr.IsHeadOf); Collection tails = graph.getObjects(variable, sr.IsTailOf); - + StringBuilder builder = new StringBuilder(); builder.append(""); for (Resource r : heads) { @@ -181,7 +176,7 @@ public class StockExpressionView extends ExpressionView { builder.delete(0, 3); return builder.toString().trim(); } - + }); } catch (DatabaseException e) { e.printStackTrace(); @@ -189,4 +184,23 @@ public class StockExpressionView extends ExpressionView { return integral; } + @Override + public void focus() { + if(this.expression != null) this.expression.forceFocus(); + } + + @Override + public void replaceSelection(String var) { + if(this.expression != null) { + String oldExpression = expression.getText(); + if(lastSelection == null) + lastSelection = new Point(0,0); + String newExpression = oldExpression.substring(0, lastSelection.x) + var + oldExpression.substring(lastSelection.y); + expression.setText(newExpression); + expression.setSelection(lastSelection.x + var.length()); + } + + } + + } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/WithLookupExpressionViewFactor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/WithLookupExpressionViewFactor.java new file mode 100644 index 00000000..78a5d5a8 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/WithLookupExpressionViewFactor.java @@ -0,0 +1,186 @@ +package org.simantics.sysdyn.ui.equation.expressions; + +import java.util.Map; + +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.FocusAdapter; +import org.eclipse.swt.events.FocusEvent; +import org.eclipse.swt.events.KeyAdapter; +import org.eclipse.swt.events.KeyEvent; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Text; +import org.simantics.db.Builtins; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.request.Read; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.ui.SimanticsUI; + +public class WithLookupExpressionViewFactor implements IExpressionViewFactor { + + + Label expressionLabel; + Text expression; + Label lookupLabel; + Text lookup; + + private Point lastSelection = new Point(0,0); + private Text lastSelectedText = expression; + + @Override + public void createView(Composite parent, Map data) { + + final String equation = data.get("equation") != null ? (String)data.get("equation") : ""; + final String lookupTable = data.get("lookup") != null ? (String)data.get("lookup") : ""; + + GridDataFactory.fillDefaults().grab(true, true).applyTo(parent); + GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent); + + expressionLabel = new Label(parent, SWT.NONE); + expressionLabel.setFont(FONT); + expressionLabel.setText("With\nLookup"); + GridDataFactory.fillDefaults().applyTo(expressionLabel); + + expression = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER); + expression.setFont(FONT); + expression.setText(equation); + GridDataFactory.fillDefaults().grab(true, true).applyTo(expression); + + + expression.addFocusListener(new FocusAdapter() { + + @Override + public void focusLost(FocusEvent e) { + lastSelection = expression.getSelection(); + lastSelectedText = expression; + } + }); + + expression.addKeyListener(new KeyAdapter() { + + @Override + public void keyPressed(KeyEvent e) { + if(e.keyCode == SWT.ESC) { + expression.setText(equation); + } + } + + }); + + lookupLabel = new Label(parent, SWT.NONE); + lookupLabel.setFont(FONT); + lookupLabel.setText("Lookup\ntable"); + GridDataFactory.fillDefaults().applyTo(lookupLabel); + + + lookup = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER); + lookup.setFont(FONT); + lookup.setText(lookupTable); + GridDataFactory.fillDefaults().grab(true, true).applyTo(lookup); + + lookup.addFocusListener(new FocusAdapter() { + + @Override + public void focusLost(FocusEvent e) { + lastSelection = lookup.getSelection(); + lastSelectedText = lookup; + } + }); + + lookup.addKeyListener(new KeyAdapter() { + + @Override + public void keyPressed(KeyEvent e) { + if(e.keyCode == SWT.ESC) { + lookup.setText(lookupTable); + } + } + + }); + + } + + @Override + public void readData(final Resource variable, Map data) { + String[] results = null; + if (variable != null && data.get("equation") == null) { + try { + results = SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public String[] perform(ReadGraph graph) throws DatabaseException { + String[] results = new String[2]; + SysdynResource sr = SysdynResource.getInstance(graph); + Resource expression = graph.getPossibleObject(variable, sr.HasExpression); + if (expression != null && graph.isInstanceOf(expression, sr.WithLookupExpression)) { + results[0] = graph.getRelatedValue(expression, sr.HasEquation); + results[1] = graph.getRelatedValue(expression, sr.HasLookup); + } else { + results[0] = ""; + results[1] = ""; + } + return results; + } + + }); + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + data.put("equation", results[0]); + data.put("lookup", results[1]); + } + } + + @Override + public void writeData(final Resource variable, Map data) { + final String currentExpression = expression.getText(); + final String currentLookupTable = lookup.getText(); + + if(currentExpression != null && currentLookupTable != null) { + data.put("equation", currentExpression); + data.put("lookup", currentLookupTable); + SimanticsUI.getSession().asyncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph g) + throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(g); + Resource expression = g.getPossibleObject(variable, sr.HasExpression); + Builtins b = g.getBuiltins(); + if(expression != null) { + g.deny(variable, sr.HasExpression); + } + expression = g.newResource(); + g.claim(expression, b.InstanceOf, sr.WithLookupExpression); + g.claim(variable, sr.HasExpression, expression); + g.claimValue(expression, sr.HasEquation, currentExpression); + g.claimValue(expression, sr.HasLookup, currentLookupTable); + } + + }); + } + } + + @Override + public void focus() { + if(this.lastSelectedText != null) this.lastSelectedText.forceFocus(); + } + + @Override + public void replaceSelection(String var) { + if(this.lastSelectedText != null) { + String oldExpression = lastSelectedText.getText(); + if(lastSelection == null) + lastSelection = new Point(0,0); + String newExpression = oldExpression.substring(0, lastSelection.x) + var + oldExpression.substring(lastSelection.y); + lastSelectedText.setText(newExpression); + lastSelectedText.setSelection(lastSelection.x + var.length()); + } + } +} \ No newline at end of file diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java index 4987d1a9..e05b06da 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java @@ -21,6 +21,8 @@ public class SysdynResource { public final Resource Auxiliary; public final Resource Cloud; public final Resource Configuration; + public final Resource ConstantExpression; + public final Resource DelayExpression; public final Resource Dependency; public final Resource Expression; public final Resource Flow; @@ -29,6 +31,7 @@ public class SysdynResource { public final Resource HasExpression; public final Resource HasHead; public final Resource HasInitialEquation; + public final Resource HasLookup; public final Resource HasTail; public final Resource HasType; public final Resource HasUnit; @@ -37,6 +40,7 @@ public class SysdynResource { public final Resource IndependentVariable; public final Resource IsHeadOf; public final Resource IsTailOf; + public final Resource LookupExpression; public final Resource NormalExpression; public final Resource ParameterExpression; public final Resource Stock; @@ -44,11 +48,14 @@ public class SysdynResource { public final Resource SysdynProject; public final Resource Valve; public final Resource Variable; + public final Resource WithLookupExpression; public static class URIs { public static final String Auxiliary = "http://www.simantics.org/Sysdyn#Auxiliary"; public static final String Cloud = "http://www.simantics.org/Sysdyn#Cloud"; public static final String Configuration = "http://www.simantics.org/Sysdyn#Configuration"; + public static final String ConstantExpression = "http://www.simantics.org/Sysdyn#ConstantExpression"; + public static final String DelayExpression = "http://www.simantics.org/Sysdyn#DelayExpression"; public static final String Dependency = "http://www.simantics.org/Sysdyn#Dependency"; public static final String Expression = "http://www.simantics.org/Sysdyn#Expression"; public static final String Flow = "http://www.simantics.org/Sysdyn#Flow"; @@ -57,6 +64,7 @@ public class SysdynResource { public static final String HasExpression = "http://www.simantics.org/Sysdyn#HasExpression"; public static final String HasHead = "http://www.simantics.org/Sysdyn#HasHead"; public static final String HasInitialEquation = "http://www.simantics.org/Sysdyn#HasInitialEquation"; + public static final String HasLookup = "http://www.simantics.org/Sysdyn#HasLookup"; public static final String HasTail = "http://www.simantics.org/Sysdyn#HasTail"; public static final String HasType = "http://www.simantics.org/Sysdyn#HasType"; public static final String HasUnit = "http://www.simantics.org/Sysdyn#HasUnit"; @@ -65,6 +73,7 @@ public class SysdynResource { public static final String IndependentVariable = "http://www.simantics.org/Sysdyn#IndependentVariable"; public static final String IsHeadOf = "http://www.simantics.org/Sysdyn#IsHeadOf"; public static final String IsTailOf = "http://www.simantics.org/Sysdyn#IsTailOf"; + public static final String LookupExpression = "http://www.simantics.org/Sysdyn#LookupExpression"; public static final String NormalExpression = "http://www.simantics.org/Sysdyn#NormalExpression"; public static final String ParameterExpression = "http://www.simantics.org/Sysdyn#ParameterExpression"; public static final String Stock = "http://www.simantics.org/Sysdyn#Stock"; @@ -72,6 +81,7 @@ public class SysdynResource { public static final String SysdynProject = "http://www.simantics.org/Sysdyn#SysdynProject"; public static final String Valve = "http://www.simantics.org/Sysdyn#Valve"; public static final String Variable = "http://www.simantics.org/Sysdyn#Variable"; + public static final String WithLookupExpression = "http://www.simantics.org/Sysdyn#WithLookupExpression"; } public static Resource getResourceOrNull(ReadGraph graph, String uri) { @@ -87,6 +97,8 @@ public class SysdynResource { Auxiliary = getResourceOrNull(graph, URIs.Auxiliary); Cloud = getResourceOrNull(graph, URIs.Cloud); Configuration = getResourceOrNull(graph, URIs.Configuration); + ConstantExpression = getResourceOrNull(graph, URIs.ConstantExpression); + DelayExpression = getResourceOrNull(graph, URIs.DelayExpression); Dependency = getResourceOrNull(graph, URIs.Dependency); Expression = getResourceOrNull(graph, URIs.Expression); Flow = getResourceOrNull(graph, URIs.Flow); @@ -95,6 +107,7 @@ public class SysdynResource { HasExpression = getResourceOrNull(graph, URIs.HasExpression); HasHead = getResourceOrNull(graph, URIs.HasHead); HasInitialEquation = getResourceOrNull(graph, URIs.HasInitialEquation); + HasLookup = getResourceOrNull(graph, URIs.HasLookup); HasTail = getResourceOrNull(graph, URIs.HasTail); HasType = getResourceOrNull(graph, URIs.HasType); HasUnit = getResourceOrNull(graph, URIs.HasUnit); @@ -103,6 +116,7 @@ public class SysdynResource { IndependentVariable = getResourceOrNull(graph, URIs.IndependentVariable); IsHeadOf = getResourceOrNull(graph, URIs.IsHeadOf); IsTailOf = getResourceOrNull(graph, URIs.IsTailOf); + LookupExpression = getResourceOrNull(graph, URIs.LookupExpression); NormalExpression = getResourceOrNull(graph, URIs.NormalExpression); ParameterExpression = getResourceOrNull(graph, URIs.ParameterExpression); Stock = getResourceOrNull(graph, URIs.Stock); @@ -110,6 +124,7 @@ public class SysdynResource { SysdynProject = getResourceOrNull(graph, URIs.SysdynProject); Valve = getResourceOrNull(graph, URIs.Valve); Variable = getResourceOrNull(graph, URIs.Variable); + WithLookupExpression = getResourceOrNull(graph, URIs.WithLookupExpression); } public static SysdynResource getInstance(ReadGraph graph) { diff --git a/sysdyn_ontologies/sysdyn.graph b/sysdyn_ontologies/sysdyn.graph index d25d867e..4ead2e75 100644 --- a/sysdyn_ontologies/sysdyn.graph +++ b/sysdyn_ontologies/sysdyn.graph @@ -102,6 +102,9 @@ HasEquation