From fd365658e531d313672e898a9a59af845697c317 Mon Sep 17 00:00:00 2001 From: lempinen Date: Tue, 5 Oct 2010 11:51:42 +0000 Subject: [PATCH] Parameter and auxiliary types selected automatically. Numbers are parameters, non-numbers are auxiliaries. Constant changed to auxiliary if it is not a number. Expression saved when focus lost from the whole property view. Expression field can also have their own focus lost listeners like WithLookupExpression has to update Lookup Table tab. git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@18224 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/properties/EquationTab.java | 45 +++++++++++++------ .../properties/widgets/ExpressionWidget.java | 7 +-- .../widgets/expressions/BasicExpression.java | 20 ++++++--- ...onValidation.java => ExpressionUtils.java} | 18 +++++++- .../expressions/WithLookupExpression.java | 1 + 5 files changed, 69 insertions(+), 22 deletions(-) rename org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/{ExpressionValidation.java => ExpressionUtils.java} (90%) 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 8a10b1cc..5e61552b 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 @@ -18,7 +18,9 @@ import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableItem; +import org.eclipse.ui.IWorkbenchPartReference; import org.eclipse.ui.IWorkbenchSite; +import org.simantics.browsing.ui.platform.PropertyPageView; import org.simantics.browsing.ui.swt.PropertyTabContributorImpl; import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory; import org.simantics.browsing.ui.swt.widgets.TrackedCombo; @@ -47,9 +49,12 @@ public class EquationTab extends PropertyTabContributorImpl { TrackedCombo expressionTypeCombo, unitCombo; ShortcutTabWidget shortcutTabWidget; ExpressionWidget expressionWidget; + org.eclipse.ui.IPartListener2 focusLostListener; + IWorkbenchSite site; @Override public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) { + this.site = site; Composite composite = new Composite(body, SWT.NONE); GridDataFactory.fillDefaults().grab(true, true).applyTo(composite); GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(6).applyTo(composite); @@ -124,7 +129,6 @@ public class EquationTab extends PropertyTabContributorImpl { @Override public void modifyText(TrackedModifyEvent e) { - System.out.println(e.getText()); expressionWidget.displayExpression(e.getText(), false); expressionWidget.save(); } @@ -226,18 +230,33 @@ public class EquationTab extends PropertyTabContributorImpl { expressionWidget.validateFields(); } }); - - expressionWidget.addFocusListener(new FocusListener() { - - @Override - public void focusLost(FocusEvent e) { - expressionWidget.save(); - } - - @Override - public void focusGained(FocusEvent e) { - expressionWidget.validateFields(); + + focusLostListener = new org.eclipse.ui.IPartListener2() + { + public void partInputChanged(IWorkbenchPartReference partRef) {} + public void partVisible(IWorkbenchPartReference partRef) {} + public void partHidden(IWorkbenchPartReference partRef) {} + public void partOpened(IWorkbenchPartReference partRef) {} + public void partDeactivated(IWorkbenchPartReference partRef) + { + if(partRef.getPart(false) instanceof PropertyPageView) { + // Save expressions + if(expressionWidget != null) { + expressionWidget.save(); + } + } } - }); + public void partClosed(IWorkbenchPartReference partRef) {} + public void partBroughtToTop(IWorkbenchPartReference partRef) {} + public void partActivated(IWorkbenchPartReference partRef) {} + }; + site.getPage().addPartListener(focusLostListener); + } + + @Override + public void dispose() { + if(focusLostListener != null && site != null) + site.getPage().removePartListener(focusLostListener); + super.dispose(); } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionWidget.java index a3ad7704..823eb4b7 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionWidget.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionWidget.java @@ -25,7 +25,7 @@ import org.simantics.sysdyn.ui.properties.widgets.expressions.AuxiliaryExpressio import org.simantics.sysdyn.ui.properties.widgets.expressions.ConstantExpression; import org.simantics.sysdyn.ui.properties.widgets.expressions.DelayExpression; import org.simantics.sysdyn.ui.properties.widgets.expressions.EmptyExpression; -import org.simantics.sysdyn.ui.properties.widgets.expressions.ExpressionValidation; +import org.simantics.sysdyn.ui.properties.widgets.expressions.ExpressionUtils; import org.simantics.sysdyn.ui.properties.widgets.expressions.IExpression; import org.simantics.sysdyn.ui.properties.widgets.expressions.LookupExpression; import org.simantics.sysdyn.ui.properties.widgets.expressions.ParameterExpression; @@ -106,7 +106,8 @@ public class ExpressionWidget implements Widget { exp.createExpressionFields(parent, data); exp.addModifyListener(modifyListener); - exp.addFocusListener(focusListener); + if(focusListener != null) + exp.addFocusListener(focusListener); this.expression = exp; this.parent.layout(); validateFields(); @@ -143,7 +144,7 @@ public class ExpressionWidget implements Widget { return null; } }); - ExpressionValidation.validateExpressionFields(expression, connectedVariables, configuration); + ExpressionUtils.validateExpressionFields(expression, connectedVariables, configuration); } catch (DatabaseException e1) { e1.printStackTrace(); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/BasicExpression.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/BasicExpression.java index 01acc49e..79acf421 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/BasicExpression.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/BasicExpression.java @@ -58,7 +58,7 @@ public class BasicExpression implements IExpression { @Override public void readData(final Resource variable, Map data) { String equation = null; - if (variable != null && data.get("equation") == null && expressionType != null) { + if (variable != null && data.get("equation") == null) { try { equation = SimanticsUI.getSession().syncRequest(new Read() { @@ -66,11 +66,14 @@ public class BasicExpression implements IExpression { public String perform(ReadGraph graph) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); Resource expression = graph.getPossibleObject(variable, sr.HasExpression); - if (expression != null && graph.isInstanceOf(expression, expressionType)) { - return graph.getRelatedValue(expression, sr.HasEquation); - } else { - return ""; + if (expression != null) { + String equation = graph.getPossibleRelatedValue(expression, sr.HasEquation); + if(equation != null) + return equation; } + + return ""; + } }); @@ -108,6 +111,13 @@ public class BasicExpression implements IExpression { throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(g); Resource expression = g.getPossibleObject(variable, sr.HasExpression); + if(ExpressionUtils.isParameter(currentText)) { + System.out.println(expressionType); + if(!expressionType.equals(sr.ConstantExpression)) + expressionType = sr.ParameterExpression; + } else { + expressionType = sr.NormalExpression; + } if (oldEquation != null && expression != null && g.isInstanceOf(expression, expressionType) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionValidation.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionUtils.java similarity index 90% rename from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionValidation.java rename to org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionUtils.java index 2c844005..2b11a302 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionValidation.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionUtils.java @@ -25,7 +25,23 @@ import org.simantics.sysdyn.representation.Variable; import org.simantics.sysdyn.ui.properties.widgets.expressions.IExpression; import org.simantics.ui.SimanticsUI; -public class ExpressionValidation { +public class ExpressionUtils { + + /** + * Determines if the given expression is a parameter expression. Parameters are numbers. + * If the expression contains anything other than numbers, it is not a parameter. + * + * @param expression The expression to be checked + * @return is the expression a parameter + */ + static public boolean isParameter(String expression) { + try { + Double.parseDouble(expression); + return true; + } catch (NumberFormatException e) { + return false; + } + } static public void validateExpressionFields(IExpression expression, TableItem[] connectedVariables, Resource configuration) { ExpressionParser parser = new ExpressionParser(new StringReader("")); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java index 24b075da..a0bcf27a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java @@ -113,6 +113,7 @@ public class WithLookupExpression implements IExpression { @Override public void focusLost(FocusEvent e) { lastSelectedText = lookup; + save(variable, data); } }); -- 2.47.1