From f9247391aff61f5bd525a017fe3e3ea4328279c4 Mon Sep 17 00:00:00 2001 From: lempinen Date: Wed, 23 Dec 2009 14:56:28 +0000 Subject: [PATCH] Unit selector, variable table focus fix git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13353 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/elements/TextElement.java | 5 + .../sysdyn/ui/equation/EquationView.java | 101 ++++++++++-------- .../sysdyn/ui/equation/UnitSelector.java | 96 +++++++++++++++++ .../equation/expressions/ExpressionView.java | 1 + .../expressions/NormalExpressionView.java | 45 ++++---- .../expressions/StockExpressionView.java | 71 +++++++++++- .../org/simantics/sysdyn/SysdynResource.java | 3 + sysdyn_ontologies/sysdyn.graph | 13 ++- 8 files changed, 268 insertions(+), 67 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/UnitSelector.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 474fbd7e..23e93b93 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,8 +17,10 @@ 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 { @@ -37,6 +39,9 @@ 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; 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 095bec4e..4fbd7345 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 @@ -11,9 +11,12 @@ import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.swt.SWT; import org.eclipse.swt.events.FocusEvent; import org.eclipse.swt.events.FocusListener; +import org.eclipse.swt.events.KeyAdapter; +import org.eclipse.swt.events.KeyEvent; import org.eclipse.swt.events.MouseEvent; 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; @@ -31,7 +34,11 @@ import org.eclipse.ui.part.ViewPart; 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.exception.ManyObjectsForFunctionalRelationException; +import org.simantics.db.exception.ServiceException; import org.simantics.db.procedure.Listener; import org.simantics.db.request.Read; import org.simantics.sysdyn.SysdynResource; @@ -48,6 +55,8 @@ public class EquationView extends ViewPart implements ISelectionListener { Resource variable; ArrayList dependencies; boolean disposed = false; + + Composite parent; Text nameText; @@ -62,11 +71,14 @@ public class EquationView extends ViewPart implements ISelectionListener { Composite variableType; Composite emptyComposite; ExpressionView expressionView; + Composite unitComposite; + UnitSelector unitSelector; org.eclipse.ui.IPartListener2 focusLostListener; @Override public void createPartControl(Composite parent) { + this.parent = parent; // Listeners getSite().getPage().addPostSelectionListener(this); focusLostListener = new org.eclipse.ui.IPartListener2() @@ -78,8 +90,12 @@ public class EquationView extends ViewPart implements ISelectionListener { public void partDeactivated(IWorkbenchPartReference partRef) { if(partRef.getPart(false) instanceof EquationView) { + // Save expressions if(expressionView != null) expressionView.save(); + // Save unit + if(unitSelector != null) unitSelector.save(); } + } public void partClosed(IWorkbenchPartReference partRef) {} public void partBroughtToTop(IWorkbenchPartReference partRef) {} @@ -94,8 +110,24 @@ public class EquationView extends ViewPart implements ISelectionListener { // Upper part of the view: Name (and renaming) nameText = new Text(parent, SWT.SINGLE | SWT.BORDER); nameText.setFont(FONT); - GridDataFactory.fillDefaults().span(2, 1).applyTo(nameText); + 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); + 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"); + + unitComposite = new Composite(variableType, SWT.NONE); + GridDataFactory.fillDefaults().applyTo(unitComposite); + GridLayoutFactory.fillDefaults().applyTo(unitComposite); + unitSelector = new UnitSelector(unitComposite, SWT.NONE, variable); + // Center side of the view: (related) variables and possible functions TabFolder tabFolder = new TabFolder (parent, SWT.NONE); GridDataFactory.fillDefaults().span(1, 4).applyTo(tabFolder); @@ -106,9 +138,9 @@ public class EquationView extends ViewPart implements ISelectionListener { @Override public void mouseDoubleClick(MouseEvent e) { - TableItem[] selection = variableTable.getSelection(); - if(selection.length > 0) { - final String var = selection[0].getText(); + TableItem item = variableTable.getItem(new Point(e.x, e.y)); + if(item != null) { + final String var = item.getText(); expressionView.getDisplay().asyncExec(new Runnable() { @Override @@ -120,10 +152,14 @@ public class EquationView extends ViewPart implements ISelectionListener { } @Override - public void mouseDown(MouseEvent e) {} + public void mouseDown(MouseEvent e) { + expressionView.focus(); + } @Override - public void mouseUp(MouseEvent e) {} + public void mouseUp(MouseEvent e) { + expressionView.focus(); + } }); @@ -157,36 +193,11 @@ public class EquationView extends ViewPart implements ISelectionListener { GridDataFactory.fillDefaults().span(1,4).grab(true, true).applyTo(emptyComposite); GridLayoutFactory.fillDefaults().applyTo(emptyComposite); - - // Below the variable name: Variable type - variableType = new Composite(parent, SWT.NONE); - GridLayoutFactory.fillDefaults().numColumns(5).spacing(3, 0).applyTo(variableType); - GridDataFactory.fillDefaults().span(2, 1).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("Lookup"); - comboDropDown.add("Delay"); - comboDropDown = new Combo(variableType, SWT.DROP_DOWN | SWT.BORDER); - comboDropDown.add("Normal"); - comboDropDown.add("With Lookup"); - - label = new Label(variableType, SWT.SINGLE ); - label.setText("Unit:"); - label.setAlignment(SWT.RIGHT); - GridDataFactory.fillDefaults().grab(true, false).align(SWT.END, SWT.CENTER).applyTo(label); - comboDropDown = new Combo(variableType, SWT.DROP_DOWN | SWT.BORDER); - comboDropDown.add("m"); - comboDropDown.add("V"); - comboDropDown.add("euro"); - - // Below variable type: Equation + // Below variable name: Equation 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); @@ -235,7 +246,7 @@ public class EquationView extends ViewPart implements ISelectionListener { a.type = "NORMAL"; else if (graph.isInstanceOf(variable, sr.Stock)) a.type = "STOCK"; - + return a; } } @@ -273,9 +284,13 @@ public class EquationView extends ViewPart implements ISelectionListener { } nameText.setText(result.name); + if(unitSelector!= null) unitSelector.dispose(); + unitSelector = new UnitSelector(unitComposite, SWT.NONE, variable); + unitComposite.layout(); + for(Control c : equationComposite.getChildren()) { c.dispose(); - } + } ExpressionView ev = null; if (result.type.equalsIgnoreCase("NORMAL")) @@ -284,7 +299,6 @@ public class EquationView extends ViewPart implements ISelectionListener { ev = new StockExpressionView(equationComposite, SWT.NONE, variable); expressionView = ev; equationComposite.layout(); - } }); @@ -303,7 +317,8 @@ public class EquationView extends ViewPart implements ISelectionListener { @Override public void setFocus() { - //text.setFocus(); + if(expressionView != null) + expressionView.focus(); } // Returns the names of the related variables (dependencies) @@ -322,11 +337,13 @@ public class EquationView extends ViewPart implements ISelectionListener { Collection dependencies = graph.getObjects(r, sr.IsHeadOf); for(Resource d : dependencies) { - Resource tail = graph.getPossibleObject(d, sr.HasTail); - if(tail != null) { - Object name = graph.getPossibleRelatedValue(tail, b.HasName); - if(name != null) - variables.add((String)name); + 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); + } } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/UnitSelector.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/UnitSelector.java new file mode 100644 index 00000000..238f9811 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/UnitSelector.java @@ -0,0 +1,96 @@ +package org.simantics.sysdyn.ui.equation; + +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +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.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 UnitSelector extends Composite { + + Combo unitCombo; + Resource variable; + String unit = ""; + + public UnitSelector(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 { + SysdynResource sr = SysdynResource.getInstance(graph); + unit = graph.getPossibleRelatedValue(variable, sr.HasUnit); + return null; + } + }); + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + } + + + Label label = new Label(this, SWT.SINGLE ); + label.setFont(EquationView.FONT); + label.setText("Unit:"); + //label.setAlignment(SWT.RIGHT); + GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label); + + + unitCombo = new Combo(this, SWT.DROP_DOWN | SWT.BORDER); + unitCombo.setFont(EquationView.FONT); + + int columns = 6; + GC gc = new GC (unitCombo); + FontMetrics fm = gc.getFontMetrics (); + int width = columns * fm.getAverageCharWidth (); + int height = fm.getHeight (); + gc.dispose (); + GridDataFactory.fillDefaults().hint(unitCombo.computeSize(width, height)).applyTo(unitCombo); + + unitCombo.add("m"); + unitCombo.add("V"); + unitCombo.add("euro"); + if (unit != null) + if(unitCombo.indexOf(unit) >= 0) + unitCombo.select(unitCombo.indexOf(unit)); + else + unitCombo.setText(unit); + } + + public void save(){ + final String unitNew = unitCombo.getText(); + if(unitNew != null && !unitNew.equals(unit)) { + SimanticsUI.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph g) + throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(g); + if (!unitNew.equals("")) + g.claimValue(variable, sr.HasUnit, unitNew); + else if (g.getPossibleRelatedValue(variable, sr.HasUnit) != null) + g.deny(variable, sr.HasUnit); + } + }); + } + + } + +} 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 index 8b55b095..df47203c 100644 --- 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 @@ -17,4 +17,5 @@ abstract public class ExpressionView extends Composite { 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/NormalExpressionView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/NormalExpressionView.java index 7fb0bc65..73144c12 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/NormalExpressionView.java @@ -36,24 +36,28 @@ public class NormalExpressionView extends ExpressionView { public NormalExpressionView(Composite parent, int style, final Resource variable) { super(parent, style, variable); - 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 = ""; + 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; } - return null; - } - }); - } catch (DatabaseException e1) { - e1.printStackTrace(); + }); + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + } else { + equation = ""; } GridDataFactory.fillDefaults().grab(true, true).applyTo(this); @@ -68,7 +72,7 @@ public class NormalExpressionView extends ExpressionView { expression.setFont(FONT); expression.setText(equation); GridDataFactory.fillDefaults().grab(true, true).applyTo(expression); - + expression.addFocusListener(new FocusAdapter() { @Override @@ -114,10 +118,15 @@ public class NormalExpressionView extends ExpressionView { } } - + 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/StockExpressionView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/StockExpressionView.java index e640eedf..d7d24ac7 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/StockExpressionView.java @@ -1,5 +1,7 @@ package org.simantics.sysdyn.ui.equation.expressions; +import java.util.Collection; + import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.swt.SWT; @@ -26,6 +28,8 @@ import org.simantics.ui.SimanticsUI; @GraphType("http://www.simantics.org/Sysdyn#StockExpression") public class StockExpressionView extends ExpressionView { + Label integralLabel; + Text integral; Label equationLabel; Text expression; Point lastSelection = new Point(0,0); @@ -58,6 +62,18 @@ public class StockExpressionView extends ExpressionView { GridDataFactory.fillDefaults().grab(true, true).applyTo(this); GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(this); + + integralLabel = new Label(this, SWT.NONE); + integralLabel.setFont(FONT); + integralLabel.setText("Integral"); + GridDataFactory.fillDefaults().applyTo(integralLabel); + + integral = new Text(this, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER); + integral.setEditable(false); + integral.setFont(FONT); + integral.setText(getIntegral()); + GridDataFactory.fillDefaults().grab(true, true).applyTo(integral); + equationLabel = new Label(this, SWT.NONE); equationLabel.setFont(FONT); equationLabel.setText("Initial\nValue"); @@ -95,7 +111,8 @@ public class StockExpressionView extends ExpressionView { 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.setText(newExpression); + expression.setSelection(lastSelection.x + text.length()); } @Override @@ -119,9 +136,57 @@ public class StockExpressionView extends ExpressionView { } }); - } + } + } + + public void focus() { + expression.forceFocus(); + } + + private String getIntegral() { + String integral = ""; + try { + integral = SimanticsUI.getSession().syncRequest(new Read() { - + @Override + public String perform(ReadGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + 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) { + if(graph.isInstanceOf(r, sr.Flow)) { + Resource tail = graph.getPossibleObject(r, sr.HasTail); + if(tail != null) { + Object name = graph.getPossibleRelatedValue(tail, b.HasName); + if (name != null) + builder.append(" + " + name); + } + } + } + for (Resource r : tails) { + if(graph.isInstanceOf(r, sr.Flow)) { + Resource head = graph.getPossibleObject(r, sr.HasHead); + if(head != null) { + Object name = graph.getPossibleRelatedValue(head, b.HasName); + if (name != null) + builder.append(" - " + name); + } + } + } + if (builder.indexOf(" + ") == 0) + builder.delete(0, 3); + return builder.toString().trim(); + } + + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + return integral; } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java index 3546148f..4987d1a9 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java @@ -31,6 +31,7 @@ public class SysdynResource { public final Resource HasInitialEquation; public final Resource HasTail; public final Resource HasType; + public final Resource HasUnit; public final Resource HasX; public final Resource HasY; public final Resource IndependentVariable; @@ -58,6 +59,7 @@ public class SysdynResource { public static final String HasInitialEquation = "http://www.simantics.org/Sysdyn#HasInitialEquation"; 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"; public static final String HasX = "http://www.simantics.org/Sysdyn#HasX"; public static final String HasY = "http://www.simantics.org/Sysdyn#HasY"; public static final String IndependentVariable = "http://www.simantics.org/Sysdyn#IndependentVariable"; @@ -95,6 +97,7 @@ public class SysdynResource { HasInitialEquation = getResourceOrNull(graph, URIs.HasInitialEquation); HasTail = getResourceOrNull(graph, URIs.HasTail); HasType = getResourceOrNull(graph, URIs.HasType); + HasUnit = getResourceOrNull(graph, URIs.HasUnit); HasX = getResourceOrNull(graph, URIs.HasX); HasY = getResourceOrNull(graph, URIs.HasY); IndependentVariable = getResourceOrNull(graph, URIs.IndependentVariable); diff --git a/sysdyn_ontologies/sysdyn.graph b/sysdyn_ontologies/sysdyn.graph index 97cae434..d25d867e 100644 --- a/sysdyn_ontologies/sysdyn.graph +++ b/sysdyn_ontologies/sysdyn.graph @@ -52,18 +52,23 @@ HasExpression