From b1a0224d949215981171b74f4719139cb10d981e Mon Sep 17 00:00:00 2001 From: lempinen Date: Thu, 7 Jan 2010 12:22:15 +0000 Subject: [PATCH] Unit selector. When equation view opened, it collects all units in the model. After that, it updates the unit list with new units added by the modeler git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13420 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/equation/EquationView.java | 78 ++++++++++++++++--- .../sysdyn/ui/equation/TypeSelector.java | 41 ++++++++++ .../sysdyn/ui/equation/UnitSelector.java | 55 +++++++------ 3 files changed, 140 insertions(+), 34 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/TypeSelector.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 4fbd7345..785deb0b 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 @@ -3,6 +3,7 @@ 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; @@ -11,8 +12,6 @@ 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; @@ -34,11 +33,7 @@ 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; @@ -52,8 +47,12 @@ public class EquationView extends ViewPart implements ISelectionListener { static final Font FONT = new Font(null, "Courier New", 12, SWT.NORMAL); + //Resources Resource variable; - ArrayList dependencies; + Resource configuration; + + ArrayList units; + boolean disposed = false; Composite parent; @@ -79,6 +78,7 @@ public class EquationView extends ViewPart implements ISelectionListener { @Override public void createPartControl(Composite parent) { this.parent = parent; + this.units = new ArrayList(); // Listeners getSite().getPage().addPostSelectionListener(this); focusLostListener = new org.eclipse.ui.IPartListener2() @@ -93,7 +93,13 @@ public class EquationView extends ViewPart implements ISelectionListener { // Save expressions if(expressionView != null) expressionView.save(); // Save unit - if(unitSelector != null) unitSelector.save(); + if(unitSelector != null) { + String unitNew = unitSelector.save(); + if(unitNew != null && !unitNew.equals("") && !units.contains(unitNew)) { + units.add(unitNew); + Collections.sort(units); + } + } } } @@ -126,7 +132,8 @@ public class EquationView extends ViewPart implements ISelectionListener { unitComposite = new Composite(variableType, SWT.NONE); GridDataFactory.fillDefaults().applyTo(unitComposite); GridLayoutFactory.fillDefaults().applyTo(unitComposite); - unitSelector = new UnitSelector(unitComposite, SWT.NONE, variable); + units = getUnits(); + unitSelector = new UnitSelector(unitComposite, SWT.NONE, variable, units); // Center side of the view: (related) variables and possible functions TabFolder tabFolder = new TabFolder (parent, SWT.NONE); @@ -224,6 +231,7 @@ public class EquationView extends ViewPart implements ISelectionListener { ArrayList dependencies; String name; String type; + Resource configuration; } class UpdateViewRequest implements Read { @@ -239,6 +247,7 @@ public class EquationView extends ViewPart implements ISelectionListener { Builtins b = graph.getBuiltins(); Auxiliary a = new Auxiliary(); 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); @@ -282,12 +291,17 @@ public class EquationView extends ViewPart implements ISelectionListener { item = new TableItem(variableTable, SWT.NONE); item.setText(d); } - nameText.setText(result.name); - + if(unitSelector!= null) unitSelector.dispose(); - unitSelector = new UnitSelector(unitComposite, SWT.NONE, variable); + if(configuration == null || !configuration.equals(result.configuration)) { + configuration = result.configuration; + units = getUnits(); + } + unitSelector = new UnitSelector(unitComposite, SWT.NONE, variable, units); unitComposite.layout(); + nameText.setText(result.name); + for(Control c : equationComposite.getChildren()) { c.dispose(); } @@ -320,6 +334,46 @@ public class EquationView extends ViewPart implements ISelectionListener { if(expressionView != null) expressionView.focus(); } + + // 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>() { + + @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); + } + } + } + + return units; + + } + + + }); + } catch (DatabaseException ee) { + ExceptionUtils.logAndShowError(ee); + } + return units; + } // Returns the names of the related variables (dependencies) private ArrayList getDependencies(final Resource r){ 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 new file mode 100644 index 00000000..277b94c8 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/TypeSelector.java @@ -0,0 +1,41 @@ +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/UnitSelector.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/UnitSelector.java index 238f9811..26e282f4 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,5 +1,7 @@ package org.simantics.sysdyn.ui.equation; +import java.util.ArrayList; + import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.swt.SWT; @@ -21,11 +23,13 @@ public class UnitSelector extends Composite { Combo unitCombo; Resource variable; - String unit = ""; + String unit = null; + ArrayList units; - public UnitSelector(Composite parent, int style, final Resource variable) { + public UnitSelector(Composite parent, int style, final Resource variable, ArrayList units) { super(parent, style); this.variable = variable; + this.units = units; GridDataFactory.fillDefaults().applyTo(this); GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(this); @@ -55,7 +59,7 @@ public class UnitSelector extends Composite { 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 (); @@ -63,10 +67,12 @@ public class UnitSelector extends Composite { int height = fm.getHeight (); gc.dispose (); GridDataFactory.fillDefaults().hint(unitCombo.computeSize(width, height)).applyTo(unitCombo); - - unitCombo.add("m"); - unitCombo.add("V"); - unitCombo.add("euro"); + + unitCombo.removeAll(); + for(String unit : this.units) { + unitCombo.add(unit); + } + if (unit != null) if(unitCombo.indexOf(unit) >= 0) unitCombo.select(unitCombo.indexOf(unit)); @@ -74,23 +80,28 @@ public class UnitSelector extends Composite { unitCombo.setText(unit); } - public void save(){ + public String 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); - } - }); - } + try { + SimanticsUI.getSession().syncRequest(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); + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + return unitNew; + } + return null; } - } -- 2.47.1