]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
* Add the name of the item itself to the text feed assistant and the variable list...
authormiettinen <miettinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 30 Jan 2013 08:17:32 +0000 (08:17 +0000)
committermiettinen <miettinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 30 Jan 2013 08:17:32 +0000 (08:17 +0000)
* Reference to self in equation does not get the yellow underlining anymore ("No link to this variable"). (refs #4024)
* Sort items in shortcut tab
* Color time and self gray

git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@26707 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ShortcutTabWidget.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ExpressionUtils.java

index ee1e9d94e895311be63478953a3524b7ad2bdfe8..6b7d45956a11e923db548f159b721d4ff062af59 100644 (file)
  *******************************************************************************/\r
 package org.simantics.sysdyn.ui.properties.widgets;\r
 \r
+import java.text.Collator;\r
 import java.util.ArrayList;\r
 import java.util.Collection;\r
 import java.util.Collections;\r
 import java.util.HashSet;\r
+import java.util.Locale;\r
 import java.util.concurrent.CopyOnWriteArrayList;\r
 \r
 import org.eclipse.jface.layout.GridDataFactory;\r
@@ -194,10 +196,21 @@ public class ShortcutTabWidget implements Widget {
                                                        }\r
                                                }\r
                                                \r
-                                               // Add the time variable\r
-                                               item = new TableItem(variableTable, SWT.NONE);\r
-                                item.setText("time");\r
-                                item.setData("time");\r
+                                               sort();\r
+                                               \r
+                                               String selfName = getName();\r
+\r
+                                               // Time and self are not added if selfName (we have an error or a stock).\r
+                                               if (selfName != null)\r
+                                               {\r
+                                    item = new TableItem(variableTable, SWT.NONE);\r
+                                    item.setText(selfName);\r
+                                    item.setData(selfName);\r
+                                    \r
+                                    item = new TableItem(variableTable, SWT.NONE);\r
+                                    item.setText("time");\r
+                                    item.setData("time");\r
+                                               }\r
                                                \r
                                                                synchronized(dependencyListeners) {\r
                                                                        for(Runnable listener : dependencyListeners)\r
@@ -206,6 +219,60 @@ public class ShortcutTabWidget implements Widget {
                                                        }\r
                                                });\r
                                        }\r
+                                       \r
+                                       /**\r
+                                        * Sort items to alphabetical order.\r
+                                        */\r
+                                       private void sort() {\r
+                                           // First sort all items\r
+                                           TableItem[] connectedVariables = variableTable.getItems();\r
+                                           Collator collator = Collator.getInstance(Locale.getDefault());\r
+                                           for (int i = 1; i < connectedVariables.length; i++) {\r
+                                               String value1 = connectedVariables[i].getText(0);\r
+                                               for (int j = 0; j < i; j++) {\r
+                                                   String value2 = connectedVariables[j].getText(0);\r
+                                                   if (collator.compare(value1, value2) < 0) {\r
+                                                       String[] values = { connectedVariables[i].getText(0),\r
+                                                               connectedVariables[i].getText(1) };\r
+                                                       connectedVariables[i].dispose();\r
+                                                       TableItem item2 = new TableItem(variableTable, SWT.NONE, j);\r
+                                                       item2.setText(values);\r
+                                                       connectedVariables = variableTable.getItems();\r
+                                                       break;\r
+                                                   }\r
+                                               }\r
+                                           }\r
+                                       }\r
+                                       \r
+                                       /**\r
+                                        * Get the name of the respective variable.\r
+                                        */\r
+                                       private String getName() {\r
+                                           String selfName = null;\r
+                        try {\r
+                                               selfName = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
+                                @Override\r
+                                public String perform(ReadGraph graph)\r
+                                        throws DatabaseException {\r
+                                    Layer0 l0 = Layer0.getInstance(graph);\r
+                                    SysdynResource sr = SysdynResource.getInstance(graph);\r
+                                    \r
+                                    // Check if we have a stock or some other mystic error.\r
+                                    if (!graph.isInstanceOf(variable, sr.Stock)) {\r
+                                        Object selfName = graph.getPossibleRelatedValue(variable, l0.HasName);\r
+                                        if (selfName instanceof String) {\r
+                                            return (String)selfName;\r
+                                        }\r
+                                    }\r
+                                    return null;\r
+                                }\r
+                            });\r
+                        }\r
+                        catch (DatabaseException e) {\r
+                            e.printStackTrace();\r
+                        }\r
+                                           return selfName;\r
+                                       }\r
 \r
                                        @Override\r
                                        public void exception(AsyncReadGraph graph,\r
index ac22c1a68656271a62954539257cdf94f0640eac..1fa72bfb965cce51d6368bfc3cb165b087f28e8d 100644 (file)
@@ -145,6 +145,14 @@ public class ExpressionUtils {
                 parsingSucceeded = true;\r
             } catch (ParseException e1) {\r
                 ef.setSyntaxError(new SyntaxError(e1.currentToken, "Syntax Error"));\r
+                \r
+                /* \r
+                 * If the equation is empty, set the parsingSucceeded = true\r
+                 * to allow the coloring of the variables succeed. \r
+                 */\r
+                if (textString.equals("")) {\r
+                    parsingSucceeded = true;\r
+                }\r
             } catch (TokenMgrError err) {\r
                 ef.setSyntaxError(new SyntaxError(0, textString.length(), ExpressionField.SYNTAX_ERROR, "Expression contains unsupported characters"));\r
             }\r
@@ -231,15 +239,37 @@ public class ExpressionUtils {
         // Check that the variables that exist have connections and the connected variables have references in the expressions\r
         if(!(expression instanceof StockExpression)) { \r
 \r
+            String selfName = null;\r
+            \r
             // If there are syntax errors, keep the previous coloring.\r
-            if(parsingSucceeded && variableTable != null && !variableTable.isDisposed()) {\r
+            if(variableTable != null && !variableTable.isDisposed()) {\r
+                \r
+                // Get the name of the variable itself\r
+                try {\r
+                    selfName = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
+\r
+                        @Override\r
+                        public String perform(ReadGraph graph) throws DatabaseException {\r
+                            Layer0 l0 = Layer0.getInstance(graph);\r
+                            Object selfName = graph.getPossibleRelatedValue(variable, l0.HasName);\r
+                            if(selfName != null) {\r
+                                return (String)selfName;\r
+                            }\r
+                            return null;\r
+                        }\r
+                    });\r
+                } catch (DatabaseException e) {\r
+                    e.printStackTrace();\r
+                }\r
+      \r
+                // Color the items in the table\r
                 TableItem[] connectedVariables = variableTable.getItems();\r
                 for(TableItem ti : connectedVariables) {\r
-                    if (ti.getText().equals("time")) {\r
+                    if (ti.getText().equals("time") || ti.getText().equals(selfName)) {\r
                         ti.setForeground(new Color(ti.getDisplay(), 127,127,127));\r
-                    } else if (!variables.contains(ti.getText())) {\r
+                    } else if (parsingSucceeded && !variables.contains(ti.getText())) {\r
                         ti.setForeground(new Color(ti.getDisplay(), 255,125,0));\r
-                    } else {\r
+                    } else if (parsingSucceeded) {\r
                         ti.setForeground(new Color(ti.getDisplay(), 0, 0, 0));\r
                         variables.remove(ti.getText());\r
                     }\r
@@ -249,6 +279,9 @@ public class ExpressionUtils {
             // Remove all enumerations and sheets, they cannot have connections\r
             variables.removeAll(ignoreVariables);\r
 \r
+            // Always remove self\r
+            variables.remove(selfName);\r
+\r
             if(!variables.isEmpty()) {\r
                 HashMap<ExpressionField ,ArrayList<Position>> positions = getPositionsForVariables(references, variables);\r
                 for(ExpressionField ef : positions.keySet()) {\r