From: lempinen Date: Wed, 30 May 2012 11:24:54 +0000 (+0000) Subject: Save modifications to expressions before displaying another expression for same varia... X-Git-Tag: simantics-1.6~1^2~1 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=910a8be241c5fa25dcaaae42a33c91fcaa2b20e5;p=simantics%2Fsysdyn.git Save modifications to expressions before displaying another expression for same variable. Previously users needed to explicitly press enter key. (refs #3478) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@25074 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java index e27c80de..58fb0eb0 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java @@ -72,6 +72,7 @@ import org.simantics.sysdyn.ui.properties.widgets.ExpressionTypes.ExpressionType import org.simantics.sysdyn.ui.properties.widgets.ExpressionWidget; import org.simantics.sysdyn.ui.properties.widgets.IsOutputWidget; import org.simantics.sysdyn.ui.properties.widgets.ShortcutTabWidget; +import org.simantics.sysdyn.ui.properties.widgets.arrays.NameAndArrayRangeModifyListener; import org.simantics.sysdyn.ui.properties.widgets.expressions.ExpressionWidgetInput; import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNameValidator; import org.simantics.ui.SimanticsUI; @@ -182,6 +183,8 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { expressionWidget = new ExpressionWidget(expressionComposite, expressionSupport, SWT.NONE); expressionWidget.setVariableTable(shortcutTabWidget.getVariableTable()); + arrayEquationCombo.addModifyListener(new NameAndArrayRangeModifyListener(support, expressionWidget, (ArrayExpressionCombo)arrayEquationCombo)); + addListeners(context); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ArrayExpressionCombo.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ArrayExpressionCombo.java index 5994a0c5..bab2c395 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ArrayExpressionCombo.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ArrayExpressionCombo.java @@ -2,33 +2,23 @@ package org.simantics.sysdyn.ui.properties.widgets; import java.util.ArrayList; import java.util.Iterator; -import java.util.Map; -import java.util.StringTokenizer; import java.util.LinkedHashMap; +import java.util.Map; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Composite; import org.simantics.browsing.ui.swt.widgets.TrackedCombo; -import org.simantics.browsing.ui.swt.widgets.impl.ComboModifyListenerImpl; import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl; -import org.simantics.browsing.ui.swt.widgets.impl.TrackedModifyEvent; -import org.simantics.browsing.ui.swt.widgets.impl.Widget; import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; -import org.simantics.db.Session; -import org.simantics.db.VirtualGraph; -import org.simantics.db.WriteGraph; -import org.simantics.db.common.request.WriteRequest; import org.simantics.db.common.utils.NameUtils; import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.management.ISessionContext; import org.simantics.db.procedure.Listener; -import org.simantics.db.service.VirtualGraphSupport; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.SysdynResource; -import org.simantics.sysdyn.ui.utils.VariableNameUtils; public class ArrayExpressionCombo extends TrackedCombo { @@ -110,7 +100,6 @@ public class ArrayExpressionCombo extends TrackedCombo { } }); - this.addModifyListener(new NameAndArrayRangeModifyListener(support)); } @@ -185,92 +174,18 @@ public class ArrayExpressionCombo extends TrackedCombo { sb.append("]"); return sb.toString(); } - - private class NameAndArrayRangeModifyListener extends ComboModifyListenerImpl implements Widget { - - Resource lastExpression; - - public NameAndArrayRangeModifyListener(WidgetSupport support) { - support.register(this); - } - - @Override - public void setInput(ISessionContext context, Object input) { - super.setInput(context, input); - } - - @Override - public void modifyText(TrackedModifyEvent e) { - - Combo combo = (Combo)e.getWidget(); - LinkedHashMap data = (LinkedHashMap) combo.getData(); - - Resource expression = (Resource) data.get(combo.getText()); - if(expression != null) { - lastExpression = expression; - lastSelectedIndex = combo.getSelectionIndex(); - } else { - for(Object key : data.keySet()) { - int index = lastSelectedIndex < 0 ? 0 : lastSelectedIndex; - if((Integer)combo.getData((String)key) == index) { - lastExpression = (Resource) data.get((String)key); - break; - } - } - } - - super.modifyText(e); - } - - @Override - public void applyText(WriteGraph graph, final Resource variable, String text) - throws DatabaseException { - StringTokenizer st = new StringTokenizer(text, "[]"); - final String newName = st.nextToken(); - String range = null; - if(st.hasMoreTokens()) { - range = st.nextToken(); - } - String originalName = graph.getRelatedValue(variable, Layer0.getInstance(graph).HasName); - if(!originalName.equals(newName)) { - VariableNameUtils.renameInEquations(graph, variable, originalName, newName); - graph.claimLiteral(variable, Layer0.getInstance(graph).HasName, newName); - } - - SysdynResource sr = SysdynResource.getInstance(graph); - - if(range != null && lastExpression != null) { - String oldRange = graph.getPossibleRelatedValue(lastExpression, sr.Expression_arrayRange); - if(oldRange == null || !range.equals(oldRange)) { - graph.claimLiteral(lastExpression, sr.Expression_arrayRange, "[" + range + "]"); - } - } else if (range == null && lastExpression != null && graph.hasStatement(lastExpression, sr.Expression_arrayRange)) { - graph.deny(lastExpression, sr.Expression_arrayRange); - } - - Resource activeExpression = graph.getPossibleObject(variable, sr.IndependentVariable_activeExpression); - - if(lastExpression != null && !lastExpression.equals(activeExpression)) { - VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class); - final Session session = graph.getSession(); - session.asyncRequest(new WriteRequest(support.getWorkspacePersistent("expressions")) { - @Override - public void perform(WriteGraph graph) throws DatabaseException { - VirtualGraph runtime = graph.getService(VirtualGraph.class); - session.asyncRequest(new WriteRequest(runtime) { - @Override - public void perform(WriteGraph graph) throws DatabaseException { - SysdynResource sr = SysdynResource.getInstance(graph); - if(graph.hasStatement(variable, sr.IndependentVariable_activeExpression)) - graph.deny(variable, sr.IndependentVariable_activeExpression); - graph.claim(variable, sr.IndependentVariable_activeExpression, lastExpression); - } - } - ); - } - }); - } - } + + /** + * Get the index that has previously been selected + * @return + */ + public int getLastSelectedIndex() { + return lastSelectedIndex; + } + + public void setLastSelectedIndex(int lastSelectedIndex) { + this.lastSelectedIndex = lastSelectedIndex; } + } 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 index 00000000..2d814007 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/NameAndArrayRangeModifyListener.java @@ -0,0 +1,146 @@ +package org.simantics.sysdyn.ui.properties.widgets.arrays; + +import java.util.LinkedHashMap; +import java.util.StringTokenizer; + +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.swt.widgets.Combo; +import org.simantics.Simantics; +import org.simantics.browsing.ui.swt.widgets.impl.ComboModifyListenerImpl; +import org.simantics.browsing.ui.swt.widgets.impl.TrackedModifyEvent; +import org.simantics.browsing.ui.swt.widgets.impl.Widget; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.VirtualGraph; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.management.ISessionContext; +import org.simantics.db.request.Read; +import org.simantics.db.service.VirtualGraphSupport; +import org.simantics.layer0.Layer0; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.properties.widgets.ArrayExpressionCombo; +import org.simantics.sysdyn.ui.properties.widgets.ExpressionWidget; +import org.simantics.sysdyn.ui.utils.VariableNameUtils; +import org.simantics.utils.ui.ISelectionUtils; + +/** + * Modification listener ONLY for ArrayExpressionCombos in EquationTabs. + * @author Teemu Lempinen + * + */ +public class NameAndArrayRangeModifyListener extends ComboModifyListenerImpl implements Widget { + + Resource lastExpression; + ExpressionWidget expressionWidget; + ArrayExpressionCombo arrayExpressionCombo; + Object lastInput; + + public NameAndArrayRangeModifyListener(WidgetSupport support, ExpressionWidget expressionWidget, ArrayExpressionCombo arrayExpressionCombo) { + support.register(this); + this.expressionWidget = expressionWidget; + this.arrayExpressionCombo = arrayExpressionCombo; + } + + @Override + public void setInput(ISessionContext context, Object input) { + super.setInput(context, input); + this.lastInput = input; + } + + public void modifyText(TrackedModifyEvent e) { + Combo combo = (Combo)e.getWidget(); + LinkedHashMap data = (LinkedHashMap) combo.getData(); + + Resource activeExpression = null; + try { + final Object input = lastInput; + activeExpression = Simantics.getSession().syncRequest(new Read() { + @Override + public Resource perform(ReadGraph graph) + throws DatabaseException { + Resource variable = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class); + return graph.getPossibleObject(variable, SysdynResource.getInstance(graph).IndependentVariable_activeExpression); + } + + }); + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + + + Resource expression = (Resource) data.get(combo.getText()); + if(expression != null) { + lastExpression = expression; + arrayExpressionCombo.setLastSelectedIndex(combo.getSelectionIndex()); + } else { + for(Object key : data.keySet()) { + int index = arrayExpressionCombo.getLastSelectedIndex() < 0 ? 0 : arrayExpressionCombo.getLastSelectedIndex(); + if((Integer)combo.getData((String)key) == index) { + lastExpression = (Resource) data.get((String)key); + break; + } + } + } + + // If expression has changed (i.e. user actually selects a different item in the combo), save the previous + if(lastExpression != null && !lastExpression.equals(activeExpression)) { + expressionWidget.save(); + } + + super.modifyText(e); + } + + @Override + public void applyText(WriteGraph graph, final Resource variable, String text) + throws DatabaseException { + StringTokenizer st = new StringTokenizer(text, "[]"); + final String newName = st.nextToken(); + String range = null; + if(st.hasMoreTokens()) { + range = st.nextToken(); + } + String originalName = graph.getRelatedValue(variable, Layer0.getInstance(graph).HasName); + if(!originalName.equals(newName)) { + VariableNameUtils.renameInEquations(graph, variable, originalName, newName); + graph.claimLiteral(variable, Layer0.getInstance(graph).HasName, newName); + } + + SysdynResource sr = SysdynResource.getInstance(graph); + + if(range != null && lastExpression != null) { + String oldRange = graph.getPossibleRelatedValue(lastExpression, sr.Expression_arrayRange); + if(oldRange == null || !range.equals(oldRange)) { + graph.claimLiteral(lastExpression, sr.Expression_arrayRange, "[" + range + "]"); + } + } else if (range == null && lastExpression != null && graph.hasStatement(lastExpression, sr.Expression_arrayRange)) { + graph.deny(lastExpression, sr.Expression_arrayRange); + } + + Resource activeExpression = graph.getPossibleObject(variable, sr.IndependentVariable_activeExpression); + + if(lastExpression != null && !lastExpression.equals(activeExpression)) { + VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class); + final Session session = graph.getSession(); + session.asyncRequest(new WriteRequest(support.getWorkspacePersistent("expressions")) { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + VirtualGraph runtime = graph.getService(VirtualGraph.class); + session.asyncRequest(new WriteRequest(runtime) { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + if(graph.hasStatement(variable, sr.IndependentVariable_activeExpression)) + graph.deny(variable, sr.IndependentVariable_activeExpression); + graph.claim(variable, sr.IndependentVariable_activeExpression, lastExpression); + } + }); + } + }); + } + } + +}