From 22881d7f0921e7322982ab483f1dcd81d0b408c4 Mon Sep 17 00:00:00 2001 From: lempinen Date: Wed, 13 Jan 2010 13:55:24 +0000 Subject: [PATCH] New architecture for equationview. All interactions between components are defined in equationView. git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13498 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/equation/EquationView.java | 304 ++++++------------ .../ui/equation/ExpressionComposite.java | 220 +++++++++++++ .../ui/equation/ExpressionController.java | 229 ------------- .../ui/equation/ExpressionTypeSelector.java | 63 ++++ .../sysdyn/ui/equation/ShortcutTabs.java | 112 +++++++ .../sysdyn/ui/equation/UnitSelector.java | 121 +++++-- 6 files changed, 581 insertions(+), 468 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionComposite.java delete mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionController.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionTypeSelector.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ShortcutTabs.java 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 f3ac8a38..1819e6ff 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 @@ -1,10 +1,6 @@ package org.simantics.sysdyn.ui.equation; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; - import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.jface.viewers.ISelection; @@ -14,13 +10,13 @@ import org.eclipse.swt.events.FocusEvent; import org.eclipse.swt.events.FocusListener; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseListener; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; -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.eclipse.swt.widgets.Text; @@ -34,9 +30,7 @@ import org.simantics.db.Resource; 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.ui.SimanticsUI; -import org.simantics.utils.ui.ExceptionUtils; public class EquationView extends ViewPart implements ISelectionListener { @@ -46,39 +40,23 @@ public class EquationView extends ViewPart implements ISelectionListener { Resource variable; Resource configuration; - ArrayList units; - boolean disposed = false; + org.eclipse.ui.IPartListener2 focusLostListener; - Composite parent; + ShortcutTabs shortcutTabs; + ExpressionTypeSelector expressionController; + Composite unitComposite; + UnitSelector unitSelector; + ExpressionComposite expressionComposite; + // Dummies Text nameText; - - - + Composite unitsAndRange; Composite emptyComposite; - String originalText; - TabFolder tabFolder; - TabItem variables; - Table variableTable; - TabItem functions; - - Composite unitsAndRange; - Composite typeComposite; - ExpressionController expressionController; - Composite unitComposite; - UnitSelector unitSelector; - - Composite equationComposite; - - org.eclipse.ui.IPartListener2 focusLostListener; - @Override public void createPartControl(Composite parent) { - this.parent = parent; - this.units = new ArrayList(); // Listeners getSite().getPage().addPostSelectionListener(this); focusLostListener = new org.eclipse.ui.IPartListener2() @@ -91,18 +69,14 @@ public class EquationView extends ViewPart implements ISelectionListener { { if(partRef.getPart(false) instanceof EquationView) { // Save expressions - if(expressionController != null) { - expressionController.save(); + if(expressionComposite != null) { + expressionComposite.save(); } // Save unit if(unitSelector != null) { - String unitNew = unitSelector.save(); - if(unitNew != null && !unitNew.equals("") && !units.contains(unitNew)) { - units.add(unitNew); - Collections.sort(units); - } + unitSelector.save(); } } @@ -113,7 +87,7 @@ public class EquationView extends ViewPart implements ISelectionListener { }; getViewSite().getPage().addPartListener(focusLostListener); - GridLayoutFactory.fillDefaults().numColumns(4).margins(5, 5).applyTo(parent); + GridLayoutFactory.fillDefaults().numColumns(5).margins(3, 3).applyTo(parent); @@ -122,81 +96,22 @@ public class EquationView extends ViewPart implements ISelectionListener { nameText.setFont(FONT); GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText); - // After the variable name: Variable type - Composite variableType = new Composite(parent, SWT.NONE); - GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 0).applyTo(variableType); - GridDataFactory.fillDefaults().applyTo(variableType); - - typeComposite = new Composite(variableType, SWT.NONE); - GridDataFactory.fillDefaults().applyTo(typeComposite); - GridLayoutFactory.fillDefaults().applyTo(typeComposite); - expressionController = new ExpressionController(typeComposite, SWT.NONE, variable, equationComposite); + + // Upper part of the view: Expression type + expressionController = new ExpressionTypeSelector(parent, SWT.NONE); + GridDataFactory.fillDefaults().applyTo(expressionController); + addVariableTypeSelectionListener(); - unitComposite = new Composite(variableType, SWT.NONE); + // Upper part of the view: Unit input/selection + unitComposite = new Composite(parent, SWT.NONE); GridDataFactory.fillDefaults().applyTo(unitComposite); GridLayoutFactory.fillDefaults().applyTo(unitComposite); - units = getUnits(); - unitSelector = new UnitSelector(unitComposite, SWT.NONE, variable, units); + unitSelector = new UnitSelector(unitComposite, SWT.NONE); - // Center side of the view: (related) variables and possible functions - TabFolder tabFolder = new TabFolder (parent, SWT.NONE); - GridDataFactory.fillDefaults().span(1, 4).applyTo(tabFolder); - variables = new TabItem(tabFolder, SWT.NULL); - variables.setText("Variables"); - variableTable = new Table (tabFolder, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION); - variableTable.addMouseListener(new MouseListener(){ - - @Override - public void mouseDoubleClick(MouseEvent e) { - TableItem item = variableTable.getItem(new Point(e.x, e.y)); - if(item != null) { - final String var = item.getText(); - equationComposite.getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - expressionController.getExpressionViewFactor().replaceSelection(var); - } - }); - } - } - - @Override - public void mouseDown(MouseEvent e) { - expressionController.getExpressionViewFactor().focus(); - } - - @Override - public void mouseUp(MouseEvent e) { - expressionController.getExpressionViewFactor().focus(); - } - - }); - - variableTable.addFocusListener(new FocusListener() { - - @Override - public void focusGained(FocusEvent e) { - } - - @Override - public void focusLost(FocusEvent e) { - variableTable.deselectAll(); - } - - }); - variables.setControl(variableTable); - - functions = new TabItem(tabFolder, SWT.NULL); - functions.setText("Functions"); - Table table = new Table (tabFolder, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION); - TableItem item = new TableItem(table, SWT.NONE); - item.setText("min"); - item = new TableItem(table, SWT.NONE); - item.setText("max"); - item = new TableItem(table, SWT.NONE); - item.setText("abs"); - functions.setControl(table); + // Center of the view: variables and possible functions + shortcutTabs = new ShortcutTabs(parent, SWT.NONE); + GridDataFactory.fillDefaults().span(1, 4).applyTo(shortcutTabs); + addVariableTableListeners(); // Right side of the view: Details, comments emptyComposite = new Composite(parent, SWT.NONE); @@ -204,14 +119,13 @@ 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); + expressionComposite = new ExpressionComposite(parent, SWT.NONE); + GridDataFactory.fillDefaults().span(3, 1).grab(true, true).applyTo(expressionComposite); + // Lower part of the view: Units and Range unitsAndRange = new Composite(parent, SWT.NONE); - GridDataFactory.fillDefaults().span(2, 1).applyTo(unitsAndRange); + GridDataFactory.fillDefaults().span(3, 1).applyTo(unitsAndRange); RowLayout layout = new RowLayout(); layout.spacing = 3; unitsAndRange.setLayout(layout); @@ -231,9 +145,7 @@ public class EquationView extends ViewPart implements ISelectionListener { } static class Auxiliary { - ArrayList dependencies; String name; - String type; Resource configuration; } @@ -252,13 +164,6 @@ public class EquationView extends ViewPart implements ISelectionListener { variable = resource; a.configuration = graph.getPossibleObject(variable, b.PartOf); a.name = graph.getRelatedValue(resource, b.HasName); - a.dependencies = getDependencies(resource); - SysdynResource sr = SysdynResource.getInstance(graph); - if (graph.isInstanceOf(variable, sr.Auxiliary) || graph.isInstanceOf(variable, sr.Valve)) - a.type = "NORMAL"; - else if (graph.isInstanceOf(variable, sr.Stock)) - a.type = "STOCK"; - return a; } } @@ -277,38 +182,35 @@ public class EquationView extends ViewPart implements ISelectionListener { @Override public void exception(Throwable t) { - // TODO Auto-generated method stub } @Override public void execute(final Auxiliary result) { - equationComposite.getDisplay().asyncExec(new Runnable() { + expressionComposite.getDisplay().asyncExec(new Runnable() { @Override public void run(){ - - variableTable.removeAll(); - TableItem item; - for(String d : result.dependencies) { - item = new TableItem(variableTable, SWT.NONE); - item.setText(d); - } - if(unitSelector!= null) unitSelector.dispose(); if(configuration == null || !configuration.equals(result.configuration)) { configuration = result.configuration; - units = getUnits(); + if(unitSelector!= null) { + unitSelector.dispose(); + unitSelector = new UnitSelector(unitComposite, SWT.NONE); + } + } else { + unitSelector.updateUnits(variable); } - unitSelector = new UnitSelector(unitComposite, SWT.NONE, variable, units); - unitComposite.layout(); - if(expressionController!= null) expressionController.dispose(); - expressionController = new ExpressionController(typeComposite, SWT.NONE, variable, equationComposite); - typeComposite.layout(); + String expressionType = expressionComposite.resetExpressionView(variable); + expressionController.setExpressionTypes(expressionComposite.getExpressionTypes()); + expressionController.select(expressionType); nameText.setText(result.name); + if(shortcutTabs != null) { + shortcutTabs.updateTables(variable); + } } }); @@ -327,8 +229,8 @@ public class EquationView extends ViewPart implements ISelectionListener { @Override public void setFocus() { - if(expressionController.getExpressionViewFactor() != null) - expressionController.getExpressionViewFactor().focus(); + if(expressionComposite != null) + expressionComposite.focus(); } @Override @@ -337,84 +239,68 @@ public class EquationView extends ViewPart implements ISelectionListener { getViewSite().getPage().removePartListener(focusLostListener); super.dispose(); } + - // Returns all the used units in the model - private ArrayList getUnits() { - ArrayList units = new ArrayList(); - if(configuration == null) - return units; - - try { - units = SimanticsUI.getSession().syncRequest(new Read>() { + private void addVariableTypeSelectionListener() { + expressionController.getTypeCombo().addSelectionListener(new SelectionListener() { - @Override - public ArrayList perform(ReadGraph graph) - throws DatabaseException { - ArrayList units = new ArrayList(); - - SysdynResource sr = SysdynResource.getInstance(graph); - Builtins b = graph.getBuiltins(); - //Resource model = graph.getPossibleObject(variable, b.PartOf); - Resource model = configuration; - if (model != null) { - Collection variables = graph.getObjects(model, b.ConsistsOf); - for(Resource v : variables) { - Object unit = graph.getPossibleRelatedValue(v, sr.HasUnit); - if (unit != null && !units.contains((String) unit)) { - units.add((String)unit); - } - } - } + @Override + public void widgetSelected(SelectionEvent e) { + expressionComposite.displayExpressionView(expressionController.getSelecetedType()); + } - return units; + @Override + public void widgetDefaultSelected(SelectionEvent e) { + } + }); + } + + private void addVariableTableListeners() { + final Table variableTable = this.shortcutTabs.getVariableTable(); + variableTable.addMouseListener(new MouseListener(){ - } + @Override + public void mouseDoubleClick(MouseEvent e) { + TableItem item = variableTable.getItem(new Point(e.x, e.y)); + if(item != null) { + final String var = item.getText(); + variableTable.getDisplay().asyncExec(new Runnable() { + @Override + public void run() { + expressionComposite.replaceSelection(var); + } + }); + } + } - }); - } catch (DatabaseException ee) { - ExceptionUtils.logAndShowError(ee); - } - return units; - } + @Override + public void mouseDown(MouseEvent e) { + expressionComposite.focus(); + } - // Returns the names of the related variables (dependencies) - private ArrayList getDependencies(final Resource r){ - ArrayList variables = null; - try { - variables = SimanticsUI.getSession().syncRequest(new Read>() { - - @Override - public ArrayList perform(ReadGraph graph) - throws DatabaseException { - ArrayList variables = new ArrayList(); - 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); - } - } - } + @Override + public void mouseUp(MouseEvent e) { + expressionComposite.focus(); + } - return variables; + }); - } + variableTable.addFocusListener(new FocusListener() { + @Override + public void focusGained(FocusEvent e) { + } - }); - } catch (DatabaseException ee) { - ExceptionUtils.logAndShowError(ee); - } + @Override + public void focusLost(FocusEvent e) { + variableTable.deselectAll(); + } - return variables; + }); } + + + } \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionComposite.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionComposite.java new file mode 100644 index 00000000..bb5bc09e --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionComposite.java @@ -0,0 +1,220 @@ +package org.simantics.sysdyn.ui.equation; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +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.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 ExpressionComposite extends Composite { + + static 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}; + + + IExpressionViewFactor expressionViewFactor; + Map data; + Map origData; + Resource variable; + ArrayList dependencies; + + public ExpressionComposite(Composite parent, int style) { + super(parent, style); + + GridLayoutFactory.fillDefaults().applyTo(this); + + this.data = new HashMap(); + this.origData = new HashMap(); + } + + public void save() { + if(variable != null && data != null) + this.expressionViewFactor.writeData(variable, data); + } + + public void displayExpressionView(String expressionType) { + displayExpressionView(expressionType, false); + } + + public String resetExpressionView(Resource variable) { + data.clear(); + origData.clear(); + this.variable = variable; + this.expressionViewFactor = null; + String origExpressionType = getOriginalExpressionType().toString(); + displayExpressionView(origExpressionType, true); + return origExpressionType; + } + + + public void displayExpressionView(String expressionType, boolean original) { + if(expressionType == null) + return; + + if(this.expressionViewFactor != null) expressionViewFactor.updateData(data); + ExpressionType et = ExpressionType.valueOf(expressionType); + 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 : this.getChildren()) { + c.dispose(); + } + + if(original) { + evf.readData(variable, data); + for(String key : data.keySet()) { + origData.put(key, data.get(key)); + } + } + + evf.createView(this, data); + this.expressionViewFactor = evf; + this.layout(); + } + } + + + public ExpressionType[] getExpressionTypes() { + ExpressionType[] expressionTypes = null; + + 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; + } + else if(graph.isInstanceOf(variable, sr.Valve)) { + expressionTypes = valveExpressions; + } + else if(graph.isInstanceOf(variable, sr.Stock)) { + expressionTypes = stockExpressions; + } + else + expressionTypes = null; + + return expressionTypes; + } + }); + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + } + + return expressionTypes; + } + + public void focus() { + if(this.expressionViewFactor != null) + this.expressionViewFactor.focus(); + } + + public void replaceSelection(String var) { + this.expressionViewFactor.replaceSelection(var); + } + + private ExpressionType getOriginalExpressionType() { + + ExpressionType et = null; + + try { + et = SimanticsUI.getSession().syncRequest(new Read< ExpressionType>() { + + @Override + public ExpressionType perform(ReadGraph graph) throws DatabaseException { + + ExpressionType et = null; + SysdynResource sr = SysdynResource.getInstance(graph); + 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 { + + 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; + } + } + return et; + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + return et; + } +} 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 deleted file mode 100644 index d331d23d..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionController.java +++ /dev/null @@ -1,229 +0,0 @@ -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) { - getExpressionViewFactor().updateData(data); - 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/ExpressionTypeSelector.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionTypeSelector.java new file mode 100644 index 00000000..8049c65f --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionTypeSelector.java @@ -0,0 +1,63 @@ +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; + +public class ExpressionTypeSelector extends Composite { + + Combo typeCombo; + + public ExpressionTypeSelector(final Composite parent, int style) { + super(parent, style); + GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(this); + + + 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); + + } + + public Combo getTypeCombo() { + return this.typeCombo; + } + + public String getSelecetedType() { + return this.typeCombo.getItem(this.typeCombo.getSelectionIndex()); + } + + public void setExpressionTypes(ExpressionComposite.ExpressionType[] expressionTypes) { + typeCombo.removeAll(); + if(expressionTypes != null) { + for(ExpressionComposite.ExpressionType et : expressionTypes) { + typeCombo.add(et.toString()); + } + } + } + + public void select(String expressionType) { + int index = typeCombo.indexOf(expressionType); + if (index > -1) { + typeCombo.select(index); + } + } +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ShortcutTabs.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ShortcutTabs.java new file mode 100644 index 00000000..533b6312 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ShortcutTabs.java @@ -0,0 +1,112 @@ +package org.simantics.sysdyn.ui.equation; + +import java.util.ArrayList; +import java.util.Collection; + +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +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.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; + +public class ShortcutTabs extends Composite { + + + TabItem variables; + TabItem functions; + Table variableTable; + + public ShortcutTabs(Composite parent, int style) { + super(parent, style); + + GridLayoutFactory.fillDefaults().applyTo(this); + + TabFolder tabFolder = new TabFolder (this, 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"); + Table table = new Table (tabFolder, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION); + TableItem item = new TableItem(table, SWT.NONE); + item.setText("min"); + item = new TableItem(table, SWT.NONE); + item.setText("max"); + item = new TableItem(table, SWT.NONE); + item.setText("abs"); + functions.setControl(table); + + this.layout(); + } + + public ArrayList updateTables(Resource variable) { + ArrayList dependencies = getDependencies(variable); + variableTable.removeAll(); + TableItem item; + for(String d : dependencies) { + item = new TableItem(variableTable, SWT.NONE); + item.setText(d); + } + return dependencies; + } + + // Returns the names of the related variables (dependencies) + private ArrayList getDependencies(final Resource r){ + ArrayList variables = null; + try { + variables = SimanticsUI.getSession().syncRequest(new Read>() { + + @Override + public ArrayList perform(ReadGraph graph) + throws DatabaseException { + ArrayList variables = new ArrayList(); + 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; + } + + public Table getVariableTable() { + return this.variableTable; + } + +} 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 index 26e282f4..925aadfa 100644 --- 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 @@ -1,6 +1,8 @@ package org.simantics.sysdyn.ui.equation; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; @@ -10,6 +12,7 @@ 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.Builtins; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; @@ -18,6 +21,7 @@ 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; public class UnitSelector extends Composite { @@ -26,29 +30,11 @@ public class UnitSelector extends Composite { String unit = null; ArrayList units; - public UnitSelector(Composite parent, int style, final Resource variable, ArrayList units) { + public UnitSelector(Composite parent, int style) { super(parent, style); - this.variable = variable; - this.units = units; - 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(); - } - } - + this.units = getUnits(); Label label = new Label(this, SWT.SINGLE ); label.setFont(EquationView.FONT); @@ -68,21 +54,19 @@ public class UnitSelector extends Composite { gc.dispose (); GridDataFactory.fillDefaults().hint(unitCombo.computeSize(width, height)).applyTo(unitCombo); - unitCombo.removeAll(); - for(String unit : this.units) { - unitCombo.add(unit); - } + parent.layout(); - if (unit != null) - if(unitCombo.indexOf(unit) >= 0) - unitCombo.select(unitCombo.indexOf(unit)); - else - unitCombo.setText(unit); } public String save(){ final String unitNew = unitCombo.getText(); - if(unitNew != null && !unitNew.equals(unit)) { + + if(unitNew != null && !unitNew.equals("") && !units.contains(unitNew)) { + units.add(unitNew); + Collections.sort(units); + } + + if(unitNew != null && !unitNew.equals(unit) && variable != null) { try { SimanticsUI.getSession().syncRequest(new WriteRequest() { @@ -104,4 +88,81 @@ public class UnitSelector extends Composite { } return null; } + + public void updateUnits(Resource variable) { + if(unitCombo != null) { + this.variable = variable; + unitCombo.removeAll(); + for(String unit : this.units) { + unitCombo.add(unit); + } + + setUnit(); + } + } + + private void setUnit() { + + 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(); + } + } + + if (unit != null) + if(unitCombo.indexOf(unit) >= 0) + unitCombo.select(unitCombo.indexOf(unit)); + else + unitCombo.setText(unit); + } + + + // Returns all the used units in the model + private ArrayList getUnits() { + ArrayList units = new ArrayList(); + if(variable != null) { + try { + units = SimanticsUI.getSession().syncRequest(new Read>() { + + @Override + public ArrayList perform(ReadGraph graph) + throws DatabaseException { + ArrayList units = new ArrayList(); + + SysdynResource sr = SysdynResource.getInstance(graph); + Builtins b = graph.getBuiltins(); + Resource model = graph.getPossibleObject(variable, b.PartOf); + if (model != null) { + Collection variables = graph.getObjects(model, b.ConsistsOf); + for(Resource v : variables) { + Object unit = graph.getPossibleRelatedValue(v, sr.HasUnit); + if (unit != null && !units.contains((String) unit)) { + units.add((String)unit); + } + } + } + + return units; + + } + + + }); + } catch (DatabaseException ee) { + ExceptionUtils.logAndShowError(ee); + } + } + return units; + } + } -- 2.47.1