\r
import java.util.ArrayList;\r
import java.util.Collection;\r
+import java.util.Collections;\r
\r
import org.eclipse.jface.layout.GridDataFactory;\r
import org.eclipse.jface.layout.GridLayoutFactory;\r
import org.eclipse.swt.SWT;\r
import org.eclipse.swt.events.FocusEvent;\r
import org.eclipse.swt.events.FocusListener;\r
-import org.eclipse.swt.events.KeyAdapter;\r
-import org.eclipse.swt.events.KeyEvent;\r
import org.eclipse.swt.events.MouseEvent;\r
import org.eclipse.swt.events.MouseListener;\r
import org.eclipse.swt.graphics.Font;\r
import org.simantics.db.Builtins;\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.request.WriteRequest;\r
import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;\r
-import org.simantics.db.exception.ServiceException;\r
import org.simantics.db.procedure.Listener;\r
import org.simantics.db.request.Read;\r
import org.simantics.sysdyn.SysdynResource;\r
\r
static final Font FONT = new Font(null, "Courier New", 12, SWT.NORMAL);\r
\r
+ //Resources\r
Resource variable;\r
- ArrayList<String> dependencies;\r
+ Resource configuration;\r
+ \r
+ ArrayList<String> units;\r
+ \r
boolean disposed = false;\r
\r
Composite parent;\r
@Override \r
public void createPartControl(Composite parent) {\r
this.parent = parent;\r
+ this.units = new ArrayList<String>();\r
// Listeners\r
getSite().getPage().addPostSelectionListener(this);\r
focusLostListener = new org.eclipse.ui.IPartListener2()\r
// Save expressions\r
if(expressionView != null) expressionView.save();\r
// Save unit\r
- if(unitSelector != null) unitSelector.save();\r
+ if(unitSelector != null) {\r
+ String unitNew = unitSelector.save(); \r
+ if(unitNew != null && !unitNew.equals("") && !units.contains(unitNew)) {\r
+ units.add(unitNew);\r
+ Collections.sort(units);\r
+ }\r
+ }\r
}\r
\r
}\r
unitComposite = new Composite(variableType, SWT.NONE);\r
GridDataFactory.fillDefaults().applyTo(unitComposite);\r
GridLayoutFactory.fillDefaults().applyTo(unitComposite);\r
- unitSelector = new UnitSelector(unitComposite, SWT.NONE, variable);\r
+ units = getUnits();\r
+ unitSelector = new UnitSelector(unitComposite, SWT.NONE, variable, units);\r
\r
// Center side of the view: (related) variables and possible functions\r
TabFolder tabFolder = new TabFolder (parent, SWT.NONE);\r
ArrayList<String> dependencies;\r
String name;\r
String type;\r
+ Resource configuration;\r
}\r
\r
class UpdateViewRequest implements Read<Auxiliary> {\r
Builtins b = graph.getBuiltins();\r
Auxiliary a = new Auxiliary();\r
variable = resource;\r
+ a.configuration = graph.getPossibleObject(variable, b.PartOf);\r
a.name = graph.getRelatedValue(resource, b.HasName);\r
a.dependencies = getDependencies(resource);\r
SysdynResource sr = SysdynResource.getInstance(graph);\r
item = new TableItem(variableTable, SWT.NONE);\r
item.setText(d);\r
}\r
- nameText.setText(result.name);\r
-\r
+ \r
if(unitSelector!= null) unitSelector.dispose();\r
- unitSelector = new UnitSelector(unitComposite, SWT.NONE, variable);\r
+ if(configuration == null || !configuration.equals(result.configuration)) {\r
+ configuration = result.configuration;\r
+ units = getUnits();\r
+ }\r
+ unitSelector = new UnitSelector(unitComposite, SWT.NONE, variable, units);\r
unitComposite.layout();\r
\r
+ nameText.setText(result.name);\r
+ \r
for(Control c : equationComposite.getChildren()) {\r
c.dispose();\r
} \r
if(expressionView != null)\r
expressionView.focus();\r
}\r
+ \r
+ // Returns all the used units in the model\r
+ private ArrayList<String> getUnits() {\r
+ ArrayList<String> units = new ArrayList<String>();\r
+ if(configuration == null)\r
+ return units;\r
+ \r
+ try {\r
+ units = SimanticsUI.getSession().syncRequest(new Read<ArrayList<String>>() {\r
+\r
+ @Override\r
+ public ArrayList<String> perform(ReadGraph graph)\r
+ throws DatabaseException {\r
+ ArrayList<String> units = new ArrayList<String>();\r
+ \r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ Builtins b = graph.getBuiltins();\r
+ //Resource model = graph.getPossibleObject(variable, b.PartOf);\r
+ Resource model = configuration;\r
+ if (model != null) {\r
+ Collection<Resource> variables = graph.getObjects(model, b.ConsistsOf);\r
+ for(Resource v : variables) {\r
+ Object unit = graph.getPossibleRelatedValue(v, sr.HasUnit);\r
+ if (unit != null && !units.contains((String) unit)) {\r
+ units.add((String)unit);\r
+ }\r
+ }\r
+ }\r
+\r
+ return units;\r
+\r
+ }\r
+\r
+\r
+ });\r
+ } catch (DatabaseException ee) {\r
+ ExceptionUtils.logAndShowError(ee);\r
+ }\r
+ return units;\r
+ }\r
\r
// Returns the names of the related variables (dependencies)\r
private ArrayList<String> getDependencies(final Resource r){\r
--- /dev/null
+package org.simantics.sysdyn.ui.equation;\r
+\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.jface.layout.GridLayoutFactory;\r
+import org.eclipse.swt.widgets.Combo;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.ui.SimanticsUI;\r
+\r
+public class TypeSelector extends Composite {\r
+ \r
+ Combo typeCombo;\r
+ Resource variable;\r
+\r
+ public TypeSelector(Composite parent, int style, final Resource variable) {\r
+ super(parent, style);\r
+ \r
+ this.variable = variable;\r
+ GridDataFactory.fillDefaults().applyTo(this);\r
+ GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(this);\r
+\r
+ if(variable != null) {\r
+ try {\r
+ SimanticsUI.getSession().syncRequest(new Read<String>() {\r
+\r
+ @Override\r
+ public String perform(ReadGraph graph) throws DatabaseException {\r
+\r
+\r
+ return null;\r
+ }\r
+ });\r
+ } catch (DatabaseException e1) {\r
+ e1.printStackTrace();\r
+ }\r
+ }\r
+ }\r
+}\r
package org.simantics.sysdyn.ui.equation;\r
\r
+import java.util.ArrayList;\r
+\r
import org.eclipse.jface.layout.GridDataFactory;\r
import org.eclipse.jface.layout.GridLayoutFactory;\r
import org.eclipse.swt.SWT;\r
\r
Combo unitCombo;\r
Resource variable;\r
- String unit = "";\r
+ String unit = null;\r
+ ArrayList<String> units;\r
\r
- public UnitSelector(Composite parent, int style, final Resource variable) {\r
+ public UnitSelector(Composite parent, int style, final Resource variable, ArrayList<String> units) {\r
super(parent, style);\r
this.variable = variable;\r
+ this.units = units;\r
GridDataFactory.fillDefaults().applyTo(this);\r
GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(this);\r
\r
\r
unitCombo = new Combo(this, SWT.DROP_DOWN | SWT.BORDER);\r
unitCombo.setFont(EquationView.FONT);\r
- \r
+\r
int columns = 6;\r
GC gc = new GC (unitCombo);\r
FontMetrics fm = gc.getFontMetrics ();\r
int height = fm.getHeight ();\r
gc.dispose (); \r
GridDataFactory.fillDefaults().hint(unitCombo.computeSize(width, height)).applyTo(unitCombo);\r
- \r
- unitCombo.add("m");\r
- unitCombo.add("V");\r
- unitCombo.add("euro");\r
+\r
+ unitCombo.removeAll();\r
+ for(String unit : this.units) {\r
+ unitCombo.add(unit);\r
+ }\r
+\r
if (unit != null)\r
if(unitCombo.indexOf(unit) >= 0)\r
unitCombo.select(unitCombo.indexOf(unit));\r
unitCombo.setText(unit);\r
}\r
\r
- public void save(){\r
+ public String save(){\r
final String unitNew = unitCombo.getText();\r
if(unitNew != null && !unitNew.equals(unit)) {\r
- SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
-\r
- @Override\r
- public void perform(WriteGraph g)\r
- throws DatabaseException {\r
- SysdynResource sr = SysdynResource.getInstance(g);\r
- if (!unitNew.equals(""))\r
- g.claimValue(variable, sr.HasUnit, unitNew);\r
- else if (g.getPossibleRelatedValue(variable, sr.HasUnit) != null)\r
- g.deny(variable, sr.HasUnit);\r
- }\r
- });\r
- }\r
+ try {\r
+ SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
\r
+ @Override\r
+ public void perform(WriteGraph g)\r
+ throws DatabaseException {\r
+ SysdynResource sr = SysdynResource.getInstance(g);\r
+ if (!unitNew.equals("")) {\r
+ g.claimValue(variable, sr.HasUnit, unitNew);\r
+ }\r
+ else if (g.getPossibleRelatedValue(variable, sr.HasUnit) != null)\r
+ g.deny(variable, sr.HasUnit);\r
+ }\r
+ });\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ return unitNew;\r
+ }\r
+ return null;\r
}\r
-\r
}\r