From 0f57e526a15ecdb138854485692e14605c75f14c Mon Sep 17 00:00:00 2001 From: lempinen Date: Fri, 27 Nov 2009 15:23:16 +0000 Subject: [PATCH] Dependencies detected and double click adds variable to cursor location in the editor git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13198 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/equation/EquationView.java | 240 +++++++++++++----- 1 file changed, 181 insertions(+), 59 deletions(-) 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 84e67309..713d39ee 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,6 +1,9 @@ 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.jface.viewers.ISelection; @@ -8,8 +11,11 @@ import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.swt.SWT; import org.eclipse.swt.events.FocusAdapter; 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.layout.RowLayout; import org.eclipse.swt.widgets.Combo; @@ -27,25 +33,32 @@ 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.ReadRequest; import org.simantics.db.common.request.WriteRequest; 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 { static final Font FONT = new Font(null, "Courier New", 12, SWT.NORMAL); + Resource variable; + ArrayList dependencies; + boolean disposed = false; + Text nameText; Composite equationComposite; Label equationLabel; Text text; - Resource variable; + String originalText; TabFolder tabFolder; TabItem variables; + Table variableTable; TabItem functions; Composite unitsAndRange; Composite variableType; @@ -67,26 +80,68 @@ public class EquationView extends ViewPart implements ISelectionListener { GridDataFactory.fillDefaults().span(1, 4).applyTo(tabFolder); variables = new TabItem(tabFolder, SWT.NULL); variables.setText("Variables"); - Table table = new Table (tabFolder, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION); - TableItem item = new TableItem(table, SWT.NONE); - item.setText("auxiliary21"); - item = new TableItem(table, SWT.NONE); - item.setText("stock5"); - item = new TableItem(table, SWT.NONE); - item.setText("valve2"); - variables.setControl(table); + variableTable = new Table (tabFolder, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION); + variableTable.addMouseListener(new MouseListener(){ + + @Override + public void mouseDoubleClick(MouseEvent e) { + TableItem[] selection = variableTable.getSelection(); + final String var = selection[0].getText(); + text.getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + int pos = text.getCaretPosition(); + String middle = var; + String start = text.getText(0, pos -1); + String end = text.getText(pos, text.getCharCount()-1); + if (pos != 0 && !text.getText(pos-1, pos).equals(" ")) + middle = " " + middle; + if (pos != text.getCharCount() && !text.getText(pos, pos+1).equals(" ")) + middle = middle + " "; + text.setText(start + middle + end); + text.forceFocus(); + text.setSelection(pos + middle.length()); + } + }); + } + + @Override + public void mouseDown(MouseEvent e) { + + } + + @Override + public void mouseUp(MouseEvent e) { + + } + + }); + 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 = new Table (tabFolder, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION); - item = new TableItem(table, SWT.NONE); + 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); - + // Right side of the view: Details, comments Composite emptyComposite = new Composite(parent, SWT.NONE); GridDataFactory.fillDefaults().span(1,4).grab(true, true).applyTo(emptyComposite); @@ -104,7 +159,7 @@ public class EquationView extends ViewPart implements ISelectionListener { 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); @@ -113,7 +168,7 @@ public class EquationView extends ViewPart implements ISelectionListener { comboDropDown.add("m"); comboDropDown.add("V"); comboDropDown.add("euro"); - + // Below variable type: Equation equationComposite = new Composite(parent, SWT.NONE); GridDataFactory.fillDefaults().span(2, 1).grab(true, true).applyTo(equationComposite); @@ -164,7 +219,7 @@ public class EquationView extends ViewPart implements ISelectionListener { } }); - + // Lower part of the view: Units and Range unitsAndRange = new Composite(parent, SWT.NONE); GridDataFactory.fillDefaults().span(2, 1).applyTo(unitsAndRange); @@ -186,7 +241,12 @@ public class EquationView extends ViewPart implements ISelectionListener { text1.setToolTipText("Step"); } - class UpdateViewRequest extends ReadRequest { + static class Auxiliary { + ArrayList dependencies; + String eqLabel, name, value; + } + + class UpdateViewRequest implements Read { Resource resource; @@ -195,49 +255,23 @@ public class EquationView extends ViewPart implements ISelectionListener { } @Override - public void run(ReadGraph g) throws DatabaseException { - SysdynResource sr = SysdynResource.getInstance(g); - Builtins b = g.getBuiltins(); - final String eqLabel; - if (g.isInstanceOf(resource, sr.Auxiliary) || g.isInstanceOf(resource, sr.Valve)) - eqLabel = "="; - else if (g.isInstanceOf(resource, sr.Stock)) - eqLabel = "initial ="; + public Auxiliary perform(ReadGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Builtins b = graph.getBuiltins(); + Auxiliary a = new Auxiliary(); + if (graph.isInstanceOf(resource, sr.Auxiliary) || graph.isInstanceOf(resource, sr.Valve)) + a.eqLabel = "="; + else if (graph.isInstanceOf(resource, sr.Stock)) + a.eqLabel = "initial ="; else - return; + return null; variable = resource; - final String name = g.getRelatedValue(resource, b.HasName); - Resource expression = g.getPossibleObject(resource, sr.HasExpression); - final String value = expression == null ? null : - (String)g.getPossibleRelatedValue(expression, sr.HasText); - text.getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - if(value == null) - originalText = ""; - else - originalText = value; - text.setText(originalText); - } - - }); - nameText.getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - nameText.setText(name); - } - }); - equationComposite.getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - equationLabel.setText(eqLabel); - equationComposite.layout(); - } - }); - + a.name = graph.getRelatedValue(resource, b.HasName); + Resource expression = graph.getPossibleObject(resource, sr.HasExpression); + a.value = expression == null ? null : + (String)graph.getPossibleRelatedValue(expression, sr.HasText); + a.dependencies = getDependencies(resource); + return a; } @@ -252,7 +286,52 @@ public class EquationView extends ViewPart implements ISelectionListener { if(structuredSelection.size() == 1) { Object element = structuredSelection.getFirstElement(); if(element instanceof Resource) - SimanticsUI.getSession().asyncRequest(new UpdateViewRequest((Resource)element)); + SimanticsUI.getSession().asyncRequest( + new UpdateViewRequest((Resource)element), + new Listener() { + + @Override + public void exception(Throwable t) { + // TODO Auto-generated method stub + + } + + @Override + public void execute(final Auxiliary result) { + text.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); + } + + nameText.setText(result.name); + equationLabel.setText(result.eqLabel); + equationComposite.layout(); + if(result.value == null) + originalText = ""; + else + originalText = result.value; + text.setText(originalText); + + + } + }); + + } + + @Override + public boolean isDisposed() { + return disposed; + } + + } + ); } } } @@ -262,4 +341,47 @@ public class EquationView extends ViewPart implements ISelectionListener { text.setFocus(); } + // 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) { + 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; + } + + @Override + public void dispose() { + disposed = true; + super.dispose(); + } + } \ No newline at end of file -- 2.47.1