package org.simantics.sysdyn.ui.equation;\r
\r
\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+\r
import org.eclipse.jface.layout.GridDataFactory;\r
import org.eclipse.jface.layout.GridLayoutFactory;\r
import org.eclipse.jface.viewers.ISelection;\r
import org.eclipse.swt.SWT;\r
import org.eclipse.swt.events.FocusAdapter;\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.eclipse.swt.layout.RowLayout;\r
import org.eclipse.swt.widgets.Combo;\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.ReadRequest;\r
import org.simantics.db.common.request.WriteRequest;\r
import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.procedure.Listener;\r
+import org.simantics.db.request.Read;\r
import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.ui.ExceptionUtils;\r
\r
public class EquationView extends ViewPart implements ISelectionListener {\r
\r
static final Font FONT = new Font(null, "Courier New", 12, SWT.NORMAL);\r
\r
+ Resource variable;\r
+ ArrayList<String> dependencies;\r
+ boolean disposed = false;\r
+\r
Text nameText;\r
\r
Composite equationComposite;\r
Label equationLabel;\r
Text text;\r
- Resource variable;\r
+\r
String originalText;\r
TabFolder tabFolder;\r
TabItem variables;\r
+ Table variableTable;\r
TabItem functions;\r
Composite unitsAndRange;\r
Composite variableType;\r
GridDataFactory.fillDefaults().span(1, 4).applyTo(tabFolder);\r
variables = new TabItem(tabFolder, SWT.NULL);\r
variables.setText("Variables");\r
- Table table = new Table (tabFolder, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);\r
- TableItem item = new TableItem(table, SWT.NONE);\r
- item.setText("auxiliary21");\r
- item = new TableItem(table, SWT.NONE);\r
- item.setText("stock5");\r
- item = new TableItem(table, SWT.NONE);\r
- item.setText("valve2");\r
- variables.setControl(table);\r
+ variableTable = new Table (tabFolder, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);\r
+ variableTable.addMouseListener(new MouseListener(){\r
+ \r
+ @Override\r
+ public void mouseDoubleClick(MouseEvent e) {\r
+ TableItem[] selection = variableTable.getSelection();\r
+ final String var = selection[0].getText();\r
+ text.getDisplay().asyncExec(new Runnable() {\r
+ \r
+ @Override\r
+ public void run() {\r
+ int pos = text.getCaretPosition();\r
+ String middle = var;\r
+ String start = text.getText(0, pos -1);\r
+ String end = text.getText(pos, text.getCharCount()-1);\r
+ if (pos != 0 && !text.getText(pos-1, pos).equals(" "))\r
+ middle = " " + middle;\r
+ if (pos != text.getCharCount() && !text.getText(pos, pos+1).equals(" "))\r
+ middle = middle + " ";\r
+ text.setText(start + middle + end);\r
+ text.forceFocus();\r
+ text.setSelection(pos + middle.length());\r
+ }\r
+ });\r
+ }\r
+\r
+ @Override\r
+ public void mouseDown(MouseEvent e) {\r
+\r
+ }\r
+\r
+ @Override\r
+ public void mouseUp(MouseEvent e) {\r
+\r
+ }\r
+\r
+ });\r
+ variableTable.addFocusListener(new FocusListener() {\r
+\r
+ @Override\r
+ public void focusGained(FocusEvent e) { \r
+ }\r
+\r
+ @Override\r
+ public void focusLost(FocusEvent e) {\r
+ variableTable.deselectAll();\r
+ }\r
+ \r
+ });\r
+ variables.setControl(variableTable);\r
\r
functions = new TabItem(tabFolder, SWT.NULL);\r
functions.setText("Functions"); \r
- table = new Table (tabFolder, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);\r
- item = new TableItem(table, SWT.NONE);\r
+ Table table = new Table (tabFolder, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);\r
+ TableItem item = new TableItem(table, SWT.NONE);\r
item.setText("min");\r
item = new TableItem(table, SWT.NONE);\r
item.setText("max");\r
item = new TableItem(table, SWT.NONE);\r
item.setText("abs");\r
functions.setControl(table);\r
- \r
+\r
// Right side of the view: Details, comments\r
Composite emptyComposite = new Composite(parent, SWT.NONE);\r
GridDataFactory.fillDefaults().span(1,4).grab(true, true).applyTo(emptyComposite);\r
comboDropDown = new Combo(variableType, SWT.DROP_DOWN | SWT.BORDER);\r
comboDropDown.add("Normal");\r
comboDropDown.add("With Lookup");\r
- \r
+\r
label = new Label(variableType, SWT.SINGLE );\r
label.setText("Unit:");\r
label.setAlignment(SWT.RIGHT);\r
comboDropDown.add("m");\r
comboDropDown.add("V");\r
comboDropDown.add("euro");\r
- \r
+\r
// Below variable type: Equation\r
equationComposite = new Composite(parent, SWT.NONE);\r
GridDataFactory.fillDefaults().span(2, 1).grab(true, true).applyTo(equationComposite);\r
}\r
\r
});\r
- \r
+\r
// Lower part of the view: Units and Range\r
unitsAndRange = new Composite(parent, SWT.NONE); \r
GridDataFactory.fillDefaults().span(2, 1).applyTo(unitsAndRange);\r
text1.setToolTipText("Step");\r
} \r
\r
- class UpdateViewRequest extends ReadRequest {\r
+ static class Auxiliary {\r
+ ArrayList<String> dependencies;\r
+ String eqLabel, name, value;\r
+ }\r
+ \r
+ class UpdateViewRequest implements Read<Auxiliary> {\r
\r
Resource resource;\r
\r
}\r
\r
@Override\r
- public void run(ReadGraph g) throws DatabaseException {\r
- SysdynResource sr = SysdynResource.getInstance(g);\r
- Builtins b = g.getBuiltins();\r
- final String eqLabel; \r
- if (g.isInstanceOf(resource, sr.Auxiliary) || g.isInstanceOf(resource, sr.Valve))\r
- eqLabel = "=";\r
- else if (g.isInstanceOf(resource, sr.Stock))\r
- eqLabel = "initial =";\r
+ public Auxiliary perform(ReadGraph graph) throws DatabaseException {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ Builtins b = graph.getBuiltins();\r
+ Auxiliary a = new Auxiliary();\r
+ if (graph.isInstanceOf(resource, sr.Auxiliary) || graph.isInstanceOf(resource, sr.Valve))\r
+ a.eqLabel = "=";\r
+ else if (graph.isInstanceOf(resource, sr.Stock))\r
+ a.eqLabel = "initial =";\r
else \r
- return;\r
+ return null;\r
variable = resource;\r
- final String name = g.getRelatedValue(resource, b.HasName);\r
- Resource expression = g.getPossibleObject(resource, sr.HasExpression); \r
- final String value = expression == null ? null :\r
- (String)g.getPossibleRelatedValue(expression, sr.HasText);\r
- text.getDisplay().asyncExec(new Runnable() {\r
-\r
- @Override\r
- public void run() {\r
- if(value == null)\r
- originalText = "";\r
- else\r
- originalText = value;\r
- text.setText(originalText);\r
- }\r
-\r
- });\r
- nameText.getDisplay().asyncExec(new Runnable() {\r
-\r
- @Override\r
- public void run() {\r
- nameText.setText(name);\r
- }\r
- });\r
- equationComposite.getDisplay().asyncExec(new Runnable() {\r
-\r
- @Override\r
- public void run() {\r
- equationLabel.setText(eqLabel);\r
- equationComposite.layout();\r
- }\r
- });\r
-\r
+ a.name = graph.getRelatedValue(resource, b.HasName);\r
+ Resource expression = graph.getPossibleObject(resource, sr.HasExpression); \r
+ a.value = expression == null ? null :\r
+ (String)graph.getPossibleRelatedValue(expression, sr.HasText);\r
+ a.dependencies = getDependencies(resource);\r
+ return a;\r
}\r
\r
\r
if(structuredSelection.size() == 1) {\r
Object element = structuredSelection.getFirstElement();\r
if(element instanceof Resource)\r
- SimanticsUI.getSession().asyncRequest(new UpdateViewRequest((Resource)element));\r
+ SimanticsUI.getSession().asyncRequest(\r
+ new UpdateViewRequest((Resource)element),\r
+ new Listener<Auxiliary>() {\r
+\r
+ @Override\r
+ public void exception(Throwable t) {\r
+ // TODO Auto-generated method stub\r
+ \r
+ }\r
+\r
+ @Override\r
+ public void execute(final Auxiliary result) {\r
+ text.getDisplay().asyncExec(new Runnable() {\r
+\r
+ @Override\r
+ public void run(){\r
+ \r
+ variableTable.removeAll();\r
+ TableItem item;\r
+ for(String d : result.dependencies) {\r
+ item = new TableItem(variableTable, SWT.NONE);\r
+ item.setText(d);\r
+ }\r
+ \r
+ nameText.setText(result.name);\r
+ equationLabel.setText(result.eqLabel);\r
+ equationComposite.layout();\r
+ if(result.value == null)\r
+ originalText = "";\r
+ else\r
+ originalText = result.value;\r
+ text.setText(originalText);\r
+ \r
+ \r
+ }\r
+ }); \r
+ \r
+ }\r
+\r
+ @Override\r
+ public boolean isDisposed() {\r
+ return disposed;\r
+ }\r
+ \r
+ }\r
+ );\r
}\r
}\r
}\r
text.setFocus();\r
}\r
\r
+ // Returns the names of the related variables (dependencies)\r
+ private ArrayList<String> getDependencies(final Resource r){\r
+ ArrayList<String> variables = null;\r
+ try {\r
+ variables = SimanticsUI.getSession().syncRequest(new Read<ArrayList<String>>() {\r
+\r
+ @Override\r
+ public ArrayList<String> perform(ReadGraph graph)\r
+ throws DatabaseException {\r
+ ArrayList<String> variables = new ArrayList<String>();\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ Builtins b = graph.getBuiltins();\r
+\r
+ Collection<Resource> dependencies = graph.getObjects(r, sr.IsHeadOf);\r
+\r
+ for(Resource d : dependencies) {\r
+ Resource tail = graph.getPossibleObject(d, sr.HasTail);\r
+ if(tail != null) {\r
+ Object name = graph.getPossibleRelatedValue(tail, b.HasName);\r
+ if(name != null)\r
+ variables.add((String)name);\r
+ }\r
+ }\r
+\r
+ return variables;\r
+\r
+ }\r
+\r
+\r
+ });\r
+ } catch (DatabaseException ee) {\r
+ ExceptionUtils.logAndShowError(ee);\r
+ }\r
+\r
+ return variables;\r
+ }\r
+ \r
+ @Override\r
+ public void dispose() {\r
+ disposed = true;\r
+ super.dispose();\r
+ }\r
+\r
}
\ No newline at end of file