\r
import java.util.ArrayList;\r
import java.util.Iterator;\r
-import java.util.Map;\r
-import java.util.StringTokenizer;\r
import java.util.LinkedHashMap;\r
+import java.util.Map;\r
\r
import org.eclipse.swt.widgets.Combo;\r
import org.eclipse.swt.widgets.Composite;\r
import org.simantics.browsing.ui.swt.widgets.TrackedCombo;\r
-import org.simantics.browsing.ui.swt.widgets.impl.ComboModifyListenerImpl;\r
import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;\r
-import org.simantics.browsing.ui.swt.widgets.impl.TrackedModifyEvent;\r
-import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
-import org.simantics.db.Session;\r
-import org.simantics.db.VirtualGraph;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.request.WriteRequest;\r
import org.simantics.db.common.utils.NameUtils;\r
import org.simantics.db.common.utils.OrderedSetUtils;\r
import org.simantics.db.exception.DatabaseException;\r
import org.simantics.db.management.ISessionContext;\r
import org.simantics.db.procedure.Listener;\r
-import org.simantics.db.service.VirtualGraphSupport;\r
import org.simantics.layer0.Layer0;\r
import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.sysdyn.ui.utils.VariableNameUtils;\r
\r
public class ArrayExpressionCombo extends TrackedCombo {\r
\r
}\r
});\r
\r
- this.addModifyListener(new NameAndArrayRangeModifyListener(support));\r
}\r
\r
\r
sb.append("]");\r
return sb.toString();\r
}\r
-\r
- private class NameAndArrayRangeModifyListener extends ComboModifyListenerImpl<Resource> implements Widget {\r
-\r
- Resource lastExpression; \r
-\r
- public NameAndArrayRangeModifyListener(WidgetSupport support) {\r
- support.register(this);\r
- }\r
-\r
- @Override\r
- public void setInput(ISessionContext context, Object input) {\r
- super.setInput(context, input);\r
- }\r
-\r
- @Override\r
- public void modifyText(TrackedModifyEvent e) {\r
-\r
- Combo combo = (Combo)e.getWidget();\r
- LinkedHashMap<?, ?> data = (LinkedHashMap<?, ?>) combo.getData();\r
-\r
- Resource expression = (Resource) data.get(combo.getText());\r
- if(expression != null) {\r
- lastExpression = expression;\r
- lastSelectedIndex = combo.getSelectionIndex();\r
- } else {\r
- for(Object key : data.keySet()) {\r
- int index = lastSelectedIndex < 0 ? 0 : lastSelectedIndex;\r
- if((Integer)combo.getData((String)key) == index) {\r
- lastExpression = (Resource) data.get((String)key);\r
- break;\r
- }\r
- }\r
- }\r
-\r
- super.modifyText(e);\r
- }\r
-\r
- @Override\r
- public void applyText(WriteGraph graph, final Resource variable, String text)\r
- throws DatabaseException {\r
- StringTokenizer st = new StringTokenizer(text, "[]");\r
- final String newName = st.nextToken();\r
- String range = null;\r
- if(st.hasMoreTokens()) {\r
- range = st.nextToken();\r
- }\r
- String originalName = graph.getRelatedValue(variable, Layer0.getInstance(graph).HasName);\r
- if(!originalName.equals(newName)) {\r
- VariableNameUtils.renameInEquations(graph, variable, originalName, newName);\r
- graph.claimLiteral(variable, Layer0.getInstance(graph).HasName, newName);\r
- }\r
-\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
-\r
- if(range != null && lastExpression != null) {\r
- String oldRange = graph.getPossibleRelatedValue(lastExpression, sr.Expression_arrayRange);\r
- if(oldRange == null || !range.equals(oldRange)) {\r
- graph.claimLiteral(lastExpression, sr.Expression_arrayRange, "[" + range + "]");\r
- }\r
- } else if (range == null && lastExpression != null && graph.hasStatement(lastExpression, sr.Expression_arrayRange)) {\r
- graph.deny(lastExpression, sr.Expression_arrayRange);\r
- }\r
-\r
- Resource activeExpression = graph.getPossibleObject(variable, sr.IndependentVariable_activeExpression);\r
-\r
- if(lastExpression != null && !lastExpression.equals(activeExpression)) {\r
- VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class);\r
- final Session session = graph.getSession();\r
- session.asyncRequest(new WriteRequest(support.getWorkspacePersistent("expressions")) {\r
- @Override\r
- public void perform(WriteGraph graph) throws DatabaseException {\r
- VirtualGraph runtime = graph.getService(VirtualGraph.class);\r
- session.asyncRequest(new WriteRequest(runtime) {\r
- @Override\r
- public void perform(WriteGraph graph) throws DatabaseException {\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- if(graph.hasStatement(variable, sr.IndependentVariable_activeExpression))\r
- graph.deny(variable, sr.IndependentVariable_activeExpression);\r
- graph.claim(variable, sr.IndependentVariable_activeExpression, lastExpression);\r
- }\r
- }\r
- );\r
- }\r
- });\r
- }\r
- }\r
+ \r
+ /**\r
+ * Get the index that has previously been selected\r
+ * @return\r
+ */\r
+ public int getLastSelectedIndex() {\r
+ return lastSelectedIndex;\r
+ }\r
+ \r
+ public void setLastSelectedIndex(int lastSelectedIndex) {\r
+ this.lastSelectedIndex = lastSelectedIndex;\r
}\r
+\r
}\r
\r
--- /dev/null
+package org.simantics.sysdyn.ui.properties.widgets.arrays;\r
+\r
+import java.util.LinkedHashMap;\r
+import java.util.StringTokenizer;\r
+\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.swt.widgets.Combo;\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.swt.widgets.impl.ComboModifyListenerImpl;\r
+import org.simantics.browsing.ui.swt.widgets.impl.TrackedModifyEvent;\r
+import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.VirtualGraph;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.db.service.VirtualGraphSupport;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.ui.properties.widgets.ArrayExpressionCombo;\r
+import org.simantics.sysdyn.ui.properties.widgets.ExpressionWidget;\r
+import org.simantics.sysdyn.ui.utils.VariableNameUtils;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+\r
+/**\r
+ * Modification listener ONLY for ArrayExpressionCombos in EquationTabs.\r
+ * @author Teemu Lempinen\r
+ *\r
+ */\r
+public class NameAndArrayRangeModifyListener extends ComboModifyListenerImpl<Resource> implements Widget {\r
+\r
+ Resource lastExpression; \r
+ ExpressionWidget expressionWidget;\r
+ ArrayExpressionCombo arrayExpressionCombo;\r
+ Object lastInput;\r
+\r
+ public NameAndArrayRangeModifyListener(WidgetSupport support, ExpressionWidget expressionWidget, ArrayExpressionCombo arrayExpressionCombo) {\r
+ support.register(this);\r
+ this.expressionWidget = expressionWidget;\r
+ this.arrayExpressionCombo = arrayExpressionCombo;\r
+ }\r
+\r
+ @Override\r
+ public void setInput(ISessionContext context, Object input) {\r
+ super.setInput(context, input);\r
+ this.lastInput = input;\r
+ } \r
+ \r
+ public void modifyText(TrackedModifyEvent e) { \r
+ Combo combo = (Combo)e.getWidget();\r
+ LinkedHashMap<?, ?> data = (LinkedHashMap<?, ?>) combo.getData();\r
+ \r
+ Resource activeExpression = null;\r
+ try {\r
+ final Object input = lastInput;\r
+ activeExpression = Simantics.getSession().syncRequest(new Read<Resource>() {\r
+ @Override\r
+ public Resource perform(ReadGraph graph)\r
+ throws DatabaseException {\r
+ Resource variable = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class);\r
+ return graph.getPossibleObject(variable, SysdynResource.getInstance(graph).IndependentVariable_activeExpression);\r
+ }\r
+ \r
+ });\r
+ } catch (DatabaseException e1) {\r
+ e1.printStackTrace();\r
+ }\r
+ \r
+\r
+ Resource expression = (Resource) data.get(combo.getText());\r
+ if(expression != null) {\r
+ lastExpression = expression;\r
+ arrayExpressionCombo.setLastSelectedIndex(combo.getSelectionIndex());\r
+ } else {\r
+ for(Object key : data.keySet()) {\r
+ int index = arrayExpressionCombo.getLastSelectedIndex() < 0 ? 0 : arrayExpressionCombo.getLastSelectedIndex();\r
+ if((Integer)combo.getData((String)key) == index) {\r
+ lastExpression = (Resource) data.get((String)key);\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ \r
+ // If expression has changed (i.e. user actually selects a different item in the combo), save the previous \r
+ if(lastExpression != null && !lastExpression.equals(activeExpression)) {\r
+ expressionWidget.save();\r
+ }\r
+\r
+ super.modifyText(e);\r
+ }\r
+\r
+ @Override\r
+ public void applyText(WriteGraph graph, final Resource variable, String text)\r
+ throws DatabaseException {\r
+ StringTokenizer st = new StringTokenizer(text, "[]");\r
+ final String newName = st.nextToken();\r
+ String range = null;\r
+ if(st.hasMoreTokens()) {\r
+ range = st.nextToken();\r
+ }\r
+ String originalName = graph.getRelatedValue(variable, Layer0.getInstance(graph).HasName);\r
+ if(!originalName.equals(newName)) {\r
+ VariableNameUtils.renameInEquations(graph, variable, originalName, newName);\r
+ graph.claimLiteral(variable, Layer0.getInstance(graph).HasName, newName);\r
+ }\r
+\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+\r
+ if(range != null && lastExpression != null) {\r
+ String oldRange = graph.getPossibleRelatedValue(lastExpression, sr.Expression_arrayRange);\r
+ if(oldRange == null || !range.equals(oldRange)) {\r
+ graph.claimLiteral(lastExpression, sr.Expression_arrayRange, "[" + range + "]");\r
+ }\r
+ } else if (range == null && lastExpression != null && graph.hasStatement(lastExpression, sr.Expression_arrayRange)) {\r
+ graph.deny(lastExpression, sr.Expression_arrayRange);\r
+ }\r
+\r
+ Resource activeExpression = graph.getPossibleObject(variable, sr.IndependentVariable_activeExpression);\r
+\r
+ if(lastExpression != null && !lastExpression.equals(activeExpression)) {\r
+ VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class);\r
+ final Session session = graph.getSession();\r
+ session.asyncRequest(new WriteRequest(support.getWorkspacePersistent("expressions")) {\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ VirtualGraph runtime = graph.getService(VirtualGraph.class);\r
+ session.asyncRequest(new WriteRequest(runtime) {\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ if(graph.hasStatement(variable, sr.IndependentVariable_activeExpression))\r
+ graph.deny(variable, sr.IndependentVariable_activeExpression);\r
+ graph.claim(variable, sr.IndependentVariable_activeExpression, lastExpression);\r
+ }\r
+ });\r
+ }\r
+ });\r
+ }\r
+ }\r
+\r
+}\r