From f580446a30c12c100a8245dc9bbf660d6bb46f03 Mon Sep 17 00:00:00 2001 From: lempinen Date: Fri, 30 Jul 2010 08:28:46 +0000 Subject: [PATCH] Fixed some equation editor bugs (focus problems, negative numbers in lookup table parser, etc.) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16876 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../elements2/HoverTextElementNoBounds.java | 9 +-- .../sysdyn/ui/properties/EquationTab.java | 1 + .../properties/widgets/ChartTableWidget.java | 7 ++- .../ui/properties/widgets/ChartWidget.java | 8 ++- .../widgets/expressions/BasicExpression.java | 47 +++++++-------- .../expressions/ExpressionValidation.java | 6 +- .../expressions/LookupInputOutputTable.java | 2 - .../widgets/expressions/StockExpression.java | 56 +++++++----------- .../expressions/WithLookupExpression.java | 57 ++++++++++--------- 9 files changed, 90 insertions(+), 103 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java index 224db322..0275e952 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java @@ -42,7 +42,7 @@ public class HoverTextElementNoBounds extends TextElementNoBounds { public static final HoverTextElementNoBounds INSTANCE = new HoverTextElementNoBounds(); private IHintListener hoverHintListener; - + public HoverTextElementNoBounds() { super(0, 0, Alignment.LEADING, 0); } @@ -106,7 +106,7 @@ public class HoverTextElementNoBounds extends TextElementNoBounds { node.setPadding(paddingX, paddingY); node.setBorderWidth((float) borderWidth); node.setEditable(editable); - + this.hoverHintListener = new IHintListener() { @Override public void hintRemoved(IHintObservable sender, Key key, Object oldValue) { @@ -118,13 +118,14 @@ public class HoverTextElementNoBounds extends TextElementNoBounds { if(key == ElementHints.KEY_HOVER) { IElement e = (IElement)sender; TextNode name = (TextNode) e.getHint(SG_NODE); - name.setHover((Boolean)e.getHint(ElementHints.KEY_HOVER)); + if(name != null) + name.setHover((Boolean)e.getHint(ElementHints.KEY_HOVER)); } } }; e.addHintListener(hoverHintListener); } - + @Override public void cleanup(IElement e) { if(hoverHintListener != null) 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 406dbb4e..8e43dd3a 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 @@ -186,6 +186,7 @@ public class EquationTab extends PropertyTabContributorImpl { public void run() { if(expressionWidget!= null) { expressionWidget.getExpression().replaceSelection(var); + expressionWidget.getExpression().focus(); } } }); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ChartTableWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ChartTableWidget.java index d4552cbb..b0e6e63e 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ChartTableWidget.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ChartTableWidget.java @@ -174,10 +174,11 @@ public class ChartTableWidget implements Widget { try { parser.table(); ArrayList xTokens = parser.getXTokens(); - for(Token token : xTokens) { + ArrayList yTokens = parser.getYTokens(); + for(int i = 0; i < xTokens.size(); i++) { table.addLocation(new Point2D.Double( - Double.parseDouble(token.image), - Double.parseDouble(token.next.next.image))); + Double.parseDouble(xTokens.get(i).image), + Double.parseDouble(yTokens.get(i).image))); } } catch (ParseException e1) { } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ChartWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ChartWidget.java index 0dbf66a1..2f3aa635 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ChartWidget.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ChartWidget.java @@ -157,9 +157,11 @@ public class ChartWidget implements Widget { try { parser.table(); ArrayList xTokens = parser.getXTokens(); - for(Token token : xTokens) { - series.add(Double.parseDouble(token.image), - Double.parseDouble(token.next.next.image)); + ArrayList yTokens = parser.getYTokens(); + for(int i = 0; i < xTokens.size(); i++) { + series.add( + Double.parseDouble(xTokens.get(i).image), + Double.parseDouble(yTokens.get(i).image)); } } catch (ParseException e1) { } 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 b7373e0a..fe27bb3e 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 @@ -9,8 +9,6 @@ import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.swt.SWT; -import org.eclipse.swt.events.FocusAdapter; -import org.eclipse.swt.events.FocusEvent; import org.eclipse.swt.events.FocusListener; import org.eclipse.swt.events.KeyListener; import org.eclipse.swt.events.ModifyListener; @@ -28,39 +26,28 @@ import org.simantics.sysdyn.SysdynResource; import org.simantics.ui.SimanticsUI; public class BasicExpression implements IExpression { - + private ExpressionField expression; protected Resource expressionType; - private Point caretPosition; // Bug or something in source viewer's text widget. Cursor goes to 0, 0 on focus lost - + @Override public void createExpressionFields(Composite parent, Map data) { GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent); String equation = data.get("equation") != null ? (String)data.get("equation") : ""; - + Label l = new Label(parent, SWT.NONE); l.setText("="); - + expression = new ExpressionField(parent, SWT.BORDER); expression.setExpression(equation); GridDataFactory.fillDefaults().grab(true, true).applyTo(expression); - - expression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() { - @Override - public void focusLost(FocusEvent e) { - caretPosition = expression.getSelection(); - } - }); } @Override public void focus() { - if(this.expression != null && caretPosition != null) { - this.expression.focus(); - expression.getSourceViewer().getTextWidget().setSelection(caretPosition.x, caretPosition.x + caretPosition.y); - } - + this.expression.focus(); + } @Override @@ -111,7 +98,9 @@ public class BasicExpression implements IExpression { @Override public void save(final Resource variable, Map data) { final String currentText = expression.getExpression(); - if(currentText != null && expressionType != null) { + String oldEquation = (String)data.get("equation"); + if(oldEquation == null || + (currentText != null && expressionType != null && !currentText.equals(oldEquation))) { data.put("equation", currentText); SimanticsUI.getSession().asyncRequest(new WriteRequest() { @Override @@ -120,18 +109,20 @@ public class BasicExpression implements IExpression { SysdynResource sr = SysdynResource.getInstance(g); Resource expression = g.getPossibleObject(variable, sr.HasExpression); Builtins b = g.getBuiltins(); - if(expression != null) { + if(expression != null && !g.isInstanceOf(expression, expressionType)) { g.deny(variable, sr.HasExpression); + expression = null; + } + if(expression == null) { + expression = g.newResource(); + g.claim(expression, b.InstanceOf, null, expressionType); + g.claim(variable, sr.HasExpression, expression); } - expression = g.newResource(); - g.claim(expression, b.InstanceOf, null, expressionType); - g.claim(variable, sr.HasExpression, expression); g.claimValue(expression, sr.HasEquation, currentText); } }); } - expression.setExpression(currentText); } @Override @@ -143,15 +134,15 @@ public class BasicExpression implements IExpression { @Override public void addKeyListener(KeyListener listener) { this.expression.getSourceViewer().getTextWidget().addKeyListener(listener); - + } @Override public void addModifyListener(ModifyListener listener) { this.expression.getSourceViewer().getTextWidget().addModifyListener(listener); - + } - + @Override public void addFocusListener(FocusListener listener) { this.expression.getSourceViewer().getTextWidget().addFocusListener(listener); 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/ExpressionValidation.java index 92ef2326..5c8a2cdd 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/ExpressionValidation.java @@ -19,7 +19,7 @@ import org.simantics.sysdyn.manager.SysdynModel; import org.simantics.sysdyn.manager.SysdynModelManager; import org.simantics.sysdyn.representation.Configuration; import org.simantics.sysdyn.representation.IElement; -import org.simantics.sysdyn.representation.IndependentVariable; +import org.simantics.sysdyn.representation.Variable; import org.simantics.sysdyn.ui.properties.widgets.expressions.IExpression; import org.simantics.ui.SimanticsUI; @@ -56,8 +56,8 @@ public class ExpressionValidation { Configuration conf = model.getConfiguration(); ArrayList elements = conf.getElements(); for(IElement e : elements) { - if(e instanceof IndependentVariable) { - IndependentVariable v = (IndependentVariable) e; + if(e instanceof Variable) { + Variable v = (Variable) e; modelVariables.add(v.getName()); } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupInputOutputTable.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupInputOutputTable.java index 27d539a0..60104211 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupInputOutputTable.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupInputOutputTable.java @@ -134,7 +134,6 @@ public class LookupInputOutputTable extends Composite { * @param clazz String.class or Double.class * @return input as string or double or null if asked for something else */ - @SuppressWarnings("unchecked") public Object getInput(Class clazz) { if(clazz == String.class) { return "" + input; @@ -162,7 +161,6 @@ public class LookupInputOutputTable extends Composite { * @param clazz String.class or Double.class * @return output as string or double or null if asked for something else */ - @SuppressWarnings("unchecked") public Object getOutput(Class clazz) { if(clazz == String.class) { return "" + output; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java index f9be1605..6d357d73 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java @@ -10,8 +10,6 @@ import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.swt.SWT; -import org.eclipse.swt.events.FocusAdapter; -import org.eclipse.swt.events.FocusEvent; import org.eclipse.swt.events.FocusListener; import org.eclipse.swt.events.KeyListener; import org.eclipse.swt.events.ModifyListener; @@ -33,49 +31,36 @@ public class StockExpression implements IExpression { private Text integral; private ExpressionField expression; - private Point caretPosition; // Bug or something in source viewer's text widget. Cursor goes to 0, 0 on focus lost - + @Override public void createExpressionFields(Composite parent, Map data) { GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent); String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : ""; String integralEquation = data.get("integral") != null ? (String)data.get("integral") : ""; - + Label label = new Label(parent, SWT.NONE); label.setText("Integral"); - + integral = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER); integral.setEditable(false); integral.setText(integralEquation); GridDataFactory.fillDefaults().grab(true, true).applyTo(integral); - - + + label = new Label(parent, SWT.NONE); label.setText("Initial\nValue"); - + expression = new ExpressionField(parent, SWT.BORDER); expression.setExpression(initialEquation); - + GridDataFactory.fillDefaults().grab(true, true).applyTo(expression); - - expression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() { - @Override - public void focusLost(FocusEvent e) { - caretPosition = expression.getSelection(); - } - }); - } @Override public void focus() { - if(this.expression != null && caretPosition != null) { - this.expression.focus(); - expression.getSourceViewer().getTextWidget().setSelection(caretPosition.x, caretPosition.x + caretPosition.y); - } - + this.expression.focus(); } @Override @@ -110,7 +95,7 @@ public class StockExpression implements IExpression { } data.put("integral", getIntegral(variable)); - + } @Override @@ -139,12 +124,15 @@ public class StockExpression implements IExpression { SysdynResource sr = SysdynResource.getInstance(g); Resource expression = g.getPossibleObject(variable, sr.HasExpression); Builtins b = g.getBuiltins(); - if(expression != null) { + if(expression != null && !g.isInstanceOf(expression, sr.StockExpression)) { g.deny(variable, sr.HasExpression); + expression = null; } - expression = g.newResource(); - g.claim(expression, b.InstanceOf, null, sr.StockExpression); - g.claim(variable, sr.HasExpression, expression); + if(expression == null) { + expression = g.newResource(); + g.claim(expression, b.InstanceOf, null, sr.StockExpression); + g.claim(variable, sr.HasExpression, expression); + } g.claimValue(expression, sr.HasInitialEquation, currentText); } @@ -160,8 +148,8 @@ public class StockExpression implements IExpression { if(this.integral != null && this.integral.getText() != null) data.put("integral", this.integral.getText()); } - - + + private String getIntegral(final Resource variable) { String integral = ""; try { @@ -183,7 +171,7 @@ public class StockExpression implements IExpression { Object name = graph.getPossibleRelatedValue(tail, b.HasName); if (name != null) builder.append(" + " + name); - + } } } @@ -212,15 +200,15 @@ public class StockExpression implements IExpression { @Override public void addKeyListener(KeyListener listener) { this.expression.getSourceViewer().getTextWidget().addKeyListener(listener); - + } @Override public void addModifyListener(ModifyListener listener) { this.expression.getSourceViewer().getTextWidget().addModifyListener(listener); - + } - + @Override public void addFocusListener(FocusListener listener) { this.expression.getSourceViewer().getTextWidget().addFocusListener(listener); 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 619aeaf1..8d9afd02 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 @@ -59,7 +59,7 @@ public class WithLookupExpression implements IExpression { private ChartPanel smallPanel; private Frame smallFrame; - + private Resource variable; public WithLookupExpression(Resource variable) { @@ -126,8 +126,8 @@ public class WithLookupExpression implements IExpression { updateChartTimer.restart(); } }); - - + + SimanticsUI.getSession().asyncRequest(new Read() { @Override @@ -151,7 +151,7 @@ public class WithLookupExpression implements IExpression { public void execute(final String result) { if(lookup != null) lookup.getDisplay().asyncExec(new Runnable() { - + @Override public void run() { lookup.setExpression(result); @@ -184,11 +184,11 @@ public class WithLookupExpression implements IExpression { @Override public void readData(final Resource variable, Map data) { - + class Auxiliary { String equation, lookup; } - + Auxiliary results = null; if (variable != null && data.get("equation") == null) { @@ -237,8 +237,13 @@ public class WithLookupExpression implements IExpression { public void save(final Resource variable, Map data) { final String currentExpression = expression.getExpression(); final String currentLookupTable = lookup.getExpression(); + String oldExpression = (String)data.get("equation"); + String oldLookupTable = (String)data.get("lookup"); - if(currentExpression != null && currentLookupTable != null) { + if(oldExpression == null || oldLookupTable == null || + (currentExpression != null && currentLookupTable != null + && (!currentExpression.equals(oldExpression) || + !currentLookupTable.equals(oldLookupTable)))) { data.putAll(data); data.put("equation", currentExpression); data.put("lookup", currentLookupTable); @@ -249,22 +254,22 @@ public class WithLookupExpression implements IExpression { SysdynResource sr = SysdynResource.getInstance(g); Resource expression = g.getPossibleObject(variable, sr.HasExpression); Builtins b = g.getBuiltins(); - if(expression != null) { + if(expression != null && !g.isInstanceOf(expression, sr.WithLookupExpression)) { g.deny(variable, sr.HasExpression); + expression = null; } - expression = g.newResource(); - g.claim(expression, b.InstanceOf, null, sr.WithLookupExpression); - g.claim(variable, sr.HasExpression, expression); - g.claimValue(expression, sr.HasEquation, currentExpression); - g.claimValue(expression, sr.HasLookup, currentLookupTable); - if(!g.hasStatement(expression, sr.HasMinX)) + if(expression == null) { + expression = g.newResource(); + g.claim(expression, b.InstanceOf, null, sr.WithLookupExpression); + g.claim(variable, sr.HasExpression, expression); g.claimValue(expression, sr.HasMinX, 0.0); - if(!g.hasStatement(expression, sr.HasMaxX)) g.claimValue(expression, sr.HasMaxX, 10.0); - if(!g.hasStatement(expression, sr.HasMinY)) g.claimValue(expression, sr.HasMinY, 0.0); - if(!g.hasStatement(expression, sr.HasMaxY)) g.claimValue(expression, sr.HasMaxY, 10.0); + + } + g.claimValue(expression, sr.HasEquation, currentExpression); + g.claimValue(expression, sr.HasLookup, currentLookupTable); } }); } @@ -324,12 +329,12 @@ public class WithLookupExpression implements IExpression { false ); chart.removeLegend(); - chart.getXYPlot().getDomainAxis().setTickLabelsVisible(false); + chart.getXYPlot().getDomainAxis().setTickLabelsVisible(true); chart.getXYPlot().getDomainAxis().setAxisLineVisible(false); - chart.getXYPlot().getDomainAxis().setTickMarksVisible(false); - chart.getXYPlot().getRangeAxis().setTickLabelsVisible(false); + chart.getXYPlot().getDomainAxis().setTickMarksVisible(true); + chart.getXYPlot().getRangeAxis().setTickLabelsVisible(true); chart.getXYPlot().getRangeAxis().setAxisLineVisible(false); - chart.getXYPlot().getRangeAxis().setTickMarksVisible(false); + chart.getXYPlot().getRangeAxis().setTickMarksVisible(true); chart.getXYPlot().getRenderer().setSeriesStroke(0, new BasicStroke(3.0f)); return chart; } @@ -341,17 +346,17 @@ public class WithLookupExpression implements IExpression { try { parser.table(); ArrayList xTokens = parser.getXTokens(); - for(Token token : xTokens) { + ArrayList yTokens = parser.getYTokens(); + for(int i = 0; i < xTokens.size(); i++) { dataPoints.add(new Point2D.Double( - Double.parseDouble(token.image), - Double.parseDouble(token.next.next.image))); + Double.parseDouble(xTokens.get(i).image), + Double.parseDouble(yTokens.get(i).image))); } } catch (ParseException e1) { +// e1.printStackTrace(); return; } - if(dataPoints.size() == 0) return; - XYSeries series = new XYSeries("Lookup Table"); for(Point2D point : dataPoints) { series.add(point.getX(), point.getY()); -- 2.47.1