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