]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Save modifications to expressions before displaying another expression for same varia...
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 30 May 2012 11:24:54 +0000 (11:24 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 30 May 2012 11:24:54 +0000 (11:24 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@25074 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ArrayExpressionCombo.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/NameAndArrayRangeModifyListener.java [new file with mode: 0644]

index e27c80de6878b4cf2c901fa6df5d63029ac42e37..58fb0eb0e21d7c6a10175c767a256f17808d8069 100644 (file)
@@ -72,6 +72,7 @@ import org.simantics.sysdyn.ui.properties.widgets.ExpressionTypes.ExpressionType
 import org.simantics.sysdyn.ui.properties.widgets.ExpressionWidget;\r
 import org.simantics.sysdyn.ui.properties.widgets.IsOutputWidget;\r
 import org.simantics.sysdyn.ui.properties.widgets.ShortcutTabWidget;\r
+import org.simantics.sysdyn.ui.properties.widgets.arrays.NameAndArrayRangeModifyListener;\r
 import org.simantics.sysdyn.ui.properties.widgets.expressions.ExpressionWidgetInput;\r
 import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNameValidator;\r
 import org.simantics.ui.SimanticsUI;\r
@@ -182,6 +183,8 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget {
                expressionWidget = new ExpressionWidget(expressionComposite, expressionSupport, SWT.NONE);\r
                expressionWidget.setVariableTable(shortcutTabWidget.getVariableTable());\r
 \r
+               arrayEquationCombo.addModifyListener(new NameAndArrayRangeModifyListener(support, expressionWidget, (ArrayExpressionCombo)arrayEquationCombo));\r
+\r
                addListeners(context);\r
        }\r
 \r
index 5994a0c5e8578a11175ecbf4c643f9c376b232f7..bab2c3952765f5bec04d3352ac1884766d8745b7 100644 (file)
@@ -2,33 +2,23 @@ package org.simantics.sysdyn.ui.properties.widgets;
 \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
@@ -110,7 +100,6 @@ public class ArrayExpressionCombo extends TrackedCombo {
                        }\r
                });\r
 \r
-               this.addModifyListener(new NameAndArrayRangeModifyListener(support));\r
        }\r
 \r
 \r
@@ -185,92 +174,18 @@ public class ArrayExpressionCombo extends TrackedCombo {
                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
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/NameAndArrayRangeModifyListener.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/NameAndArrayRangeModifyListener.java
new file mode 100644 (file)
index 0000000..2d81400
--- /dev/null
@@ -0,0 +1,146 @@
+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