1 package org.simantics.sysdyn.ui.properties.widgets.arrays;
\r
3 import java.util.LinkedHashMap;
\r
4 import java.util.StringTokenizer;
\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
31 * Modification listener ONLY for ArrayExpressionCombos in EquationTabs.
\r
32 * @author Teemu Lempinen
\r
35 public class NameAndArrayRangeModifyListener extends ComboModifyListenerImpl<Resource> implements Widget {
\r
37 Resource lastExpression;
\r
38 ExpressionWidget expressionWidget;
\r
39 ArrayExpressionCombo arrayExpressionCombo;
\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
49 public void setInput(ISessionContext context, Object input) {
\r
50 super.setInput(context, input);
\r
51 this.lastInput = input;
\r
54 public void modifyText(TrackedModifyEvent e) {
\r
55 Combo combo = (Combo)e.getWidget();
\r
56 LinkedHashMap<?, ?> data = (LinkedHashMap<?, ?>) combo.getData();
\r
58 Resource activeExpression = null;
\r
60 final Object input = lastInput;
\r
61 activeExpression = Simantics.getSession().syncRequest(new Read<Resource>() {
\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
70 } catch (DatabaseException e1) {
\r
71 e1.printStackTrace();
\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
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
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
94 super.modifyText(e);
\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
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
113 SysdynResource sr = SysdynResource.getInstance(graph);
\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
120 } else if (range == null && lastExpression != null && graph.hasStatement(lastExpression, sr.Expression_arrayRange)) {
\r
121 graph.deny(lastExpression, sr.Expression_arrayRange);
\r
124 Resource activeExpression = graph.getPossibleObject(variable, sr.IndependentVariable_activeExpression);
\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
131 public void perform(WriteGraph graph) throws DatabaseException {
\r
132 VirtualGraph runtime = graph.getService(VirtualGraph.class);
\r
133 session.asyncRequest(new WriteRequest(runtime) {
\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