]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Dependencies detected and double click adds variable to cursor location in the editor
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 27 Nov 2009 15:23:16 +0000 (15:23 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 27 Nov 2009 15:23:16 +0000 (15:23 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13198 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java

index 84e67309f65b213023e0ee7cb9ebacb0bfdf4e4f..713d39ee1a8ec1fea3c0f3b211957384acebd237 100644 (file)
@@ -1,6 +1,9 @@
 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
@@ -8,8 +11,11 @@ import org.eclipse.jface.viewers.IStructuredSelection;
 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
@@ -27,25 +33,32 @@ import org.simantics.db.Builtins;
 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
@@ -67,26 +80,68 @@ public class EquationView extends ViewPart implements ISelectionListener {
         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
@@ -104,7 +159,7 @@ public class EquationView extends ViewPart implements ISelectionListener {
         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
@@ -113,7 +168,7 @@ public class EquationView extends ViewPart implements ISelectionListener {
         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
@@ -164,7 +219,7 @@ public class EquationView extends ViewPart implements ISelectionListener {
             }\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
@@ -186,7 +241,12 @@ public class EquationView extends ViewPart implements ISelectionListener {
         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
@@ -195,49 +255,23 @@ public class EquationView extends ViewPart implements ISelectionListener {
         }\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
@@ -252,7 +286,52 @@ public class EquationView extends ViewPart implements ISelectionListener {
             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
@@ -262,4 +341,47 @@ public class EquationView extends ViewPart implements ISelectionListener {
         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