*******************************************************************************/\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
}\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
}\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
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
// 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
// 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