]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Fixed some equation editor bugs (focus problems, negative numbers in lookup table...
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 30 Jul 2010 08:28:46 +0000 (08:28 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 30 Jul 2010 08:28:46 +0000 (08:28 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16876 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ChartTableWidget.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ChartWidget.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/BasicExpression.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionValidation.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupInputOutputTable.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java

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