]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
af72b798daf4afd218e946ddefd25722b2195c85
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.properties.widgets.arrays;\r
2 \r
3 import java.util.LinkedHashMap;\r
4 import java.util.StringTokenizer;\r
5 \r
6 import org.eclipse.jface.viewers.ISelection;\r
7 import org.eclipse.swt.widgets.Combo;\r
8 import org.simantics.Simantics;\r
9 import org.simantics.browsing.ui.swt.widgets.impl.ComboModifyListenerImpl;\r
10 import org.simantics.browsing.ui.swt.widgets.impl.TrackedModifyEvent;\r
11 import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
12 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
13 import org.simantics.db.ReadGraph;\r
14 import org.simantics.db.Resource;\r
15 import org.simantics.db.Session;\r
16 import org.simantics.db.VirtualGraph;\r
17 import org.simantics.db.WriteGraph;\r
18 import org.simantics.db.common.request.WriteRequest;\r
19 import org.simantics.db.exception.DatabaseException;\r
20 import org.simantics.db.management.ISessionContext;\r
21 import org.simantics.db.request.Read;\r
22 import org.simantics.db.service.VirtualGraphSupport;\r
23 import org.simantics.layer0.Layer0;\r
24 import org.simantics.sysdyn.SysdynResource;\r
25 import org.simantics.sysdyn.ui.properties.widgets.ArrayExpressionCombo;\r
26 import org.simantics.sysdyn.ui.properties.widgets.ExpressionWidget;\r
27 import org.simantics.sysdyn.ui.utils.VariableNameUtils;\r
28 import org.simantics.utils.ui.ISelectionUtils;\r
29 \r
30 /**\r
31  * Modification listener ONLY for ArrayExpressionCombos in EquationTabs.\r
32  * @author Teemu Lempinen\r
33  *\r
34  */\r
35 public class NameAndArrayRangeModifyListener extends ComboModifyListenerImpl<Resource> implements Widget {\r
36 \r
37         Resource lastExpression; \r
38         ExpressionWidget expressionWidget;\r
39         ArrayExpressionCombo arrayExpressionCombo;\r
40         Object lastInput;\r
41 \r
42         public NameAndArrayRangeModifyListener(WidgetSupport support, ExpressionWidget expressionWidget, ArrayExpressionCombo arrayExpressionCombo) {\r
43                 support.register(this);\r
44                 this.expressionWidget = expressionWidget;\r
45                 this.arrayExpressionCombo = arrayExpressionCombo;\r
46         }\r
47 \r
48         @Override\r
49         public void setInput(ISessionContext context, Object input) {\r
50                 super.setInput(context, input);\r
51                 this.lastInput = input;\r
52         } \r
53                 \r
54         public void modifyText(TrackedModifyEvent e) {          \r
55                 Combo combo = (Combo)e.getWidget();\r
56                 LinkedHashMap<?, ?> data = (LinkedHashMap<?, ?>) combo.getData();\r
57                 \r
58                 Resource activeExpression = null;\r
59                 try {\r
60                 final Object input = lastInput;\r
61                         activeExpression = Simantics.getSession().syncRequest(new Read<Resource>() {\r
62                                 @Override\r
63                                 public Resource perform(ReadGraph graph)\r
64                                                 throws DatabaseException {\r
65                                         Resource variable = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class);\r
66                                         return graph.getPossibleObject(variable, SysdynResource.getInstance(graph).IndependentVariable_activeExpression);\r
67                                 }\r
68                                 \r
69                         });\r
70                 } catch (DatabaseException e1) {\r
71                         e1.printStackTrace();\r
72                 }\r
73                 \r
74 \r
75                 Resource expression = (Resource) data.get(combo.getText());\r
76                 if(expression != null) {\r
77                         lastExpression = expression;\r
78                         arrayExpressionCombo.setLastSelectedIndex(combo.getSelectionIndex());\r
79                 } else {\r
80                         for(Object key : data.keySet()) {\r
81                                 int index = arrayExpressionCombo.getLastSelectedIndex() < 0 ? 0 : arrayExpressionCombo.getLastSelectedIndex();\r
82                                 if((Integer)combo.getData((String)key) == index) {\r
83                                         lastExpression =  (Resource) data.get((String)key);\r
84                                         break;\r
85                                 }\r
86                         }\r
87                 }\r
88                 \r
89                 // If expression has changed (i.e. user actually selects a different item in the combo), save the previous \r
90                 if(lastExpression != null && !lastExpression.equals(activeExpression)) {\r
91                         expressionWidget.save();\r
92                 }\r
93 \r
94                 super.modifyText(e);\r
95         }\r
96 \r
97         @Override\r
98         public void applyText(WriteGraph graph, final Resource variable, String text)\r
99                         throws DatabaseException {\r
100                 StringTokenizer st = new StringTokenizer(text, "[]");\r
101                 final String newName = st.nextToken();\r
102                 String range = null;\r
103                 if(st.hasMoreTokens()) {\r
104                         range = st.nextToken();\r
105                 }\r
106                 String originalName = graph.getRelatedValue(variable, Layer0.getInstance(graph).HasName);\r
107                 if(!originalName.equals(newName)) {\r
108                     Resource configuration = graph.getPossibleObject(variable, Layer0.getInstance(graph).PartOf);\r
109                         VariableNameUtils.renameInAllEquations(graph, configuration, originalName, newName);\r
110                         graph.claimLiteral(variable, Layer0.getInstance(graph).HasName, newName);\r
111                 }\r
112 \r
113                 SysdynResource sr = SysdynResource.getInstance(graph);\r
114 \r
115                 if(range != null && lastExpression != null) {\r
116                         String oldRange = graph.getPossibleRelatedValue(lastExpression, sr.Expression_arrayRange);\r
117                         if(oldRange == null || !range.equals(oldRange)) {\r
118                                 graph.claimLiteral(lastExpression, sr.Expression_arrayRange, "[" + range + "]");\r
119                         }\r
120                 } else if (range == null && lastExpression != null && graph.hasStatement(lastExpression, sr.Expression_arrayRange)) {\r
121                         graph.deny(lastExpression, sr.Expression_arrayRange);\r
122                 }\r
123 \r
124                 Resource activeExpression = graph.getPossibleObject(variable, sr.IndependentVariable_activeExpression);\r
125 \r
126                 if(lastExpression != null && !lastExpression.equals(activeExpression)) {\r
127                         VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class);\r
128                         final Session session = graph.getSession();\r
129                         session.asyncRequest(new WriteRequest(support.getWorkspacePersistent("expressions")) {\r
130                                 @Override\r
131                                 public void perform(WriteGraph graph) throws DatabaseException {\r
132                                         VirtualGraph runtime = graph.getService(VirtualGraph.class);\r
133                                         session.asyncRequest(new WriteRequest(runtime) {\r
134                                                 @Override\r
135                                                 public void perform(WriteGraph graph) throws DatabaseException {\r
136                                                         SysdynResource sr = SysdynResource.getInstance(graph);\r
137                                                         if(graph.hasStatement(variable, sr.IndependentVariable_activeExpression))\r
138                                                                 graph.deny(variable, sr.IndependentVariable_activeExpression);\r
139                                                         graph.claim(variable, sr.IndependentVariable_activeExpression, lastExpression);\r
140                                                 }\r
141                                         });\r
142                                 }\r
143                         });\r
144                 }\r
145         }\r
146 \r
147 }\r