]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Variable validator for chart configurations (refs #3082)
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 23 Jan 2012 15:18:33 +0000 (15:18 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 23 Jan 2012 15:18:33 +0000 (15:18 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@24021 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/newComponents/NewXYLineChartHandler.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/ChartUtils.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/AllVariablesOfModel.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/VariableExistsValidator.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/VariableProposalProvider.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/bar/BarSeriesPropertyComposite.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/pie/PieSeriesPropertyComposite.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/xyline/SeriesPropertyComposite.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/xyline/XYLineGeneralPropertiesTab.java

index 98c94da8c2dd2f9d5d3bf676723831f0af101963..c2aac24458cdd5cb040736d7f1644eca084c2101 100644 (file)
@@ -78,7 +78,8 @@ public class NewXYLineChartHandler extends AbstractHandler {
                 Resource domainAxis = GraphUtils.create2(g, jfree.NumberAxis,\r
                         l0.HasName, "NumberAxis" + UUID.randomUUID().toString());\r
                 Resource rangeAxis = GraphUtils.create2(g, jfree.NumberAxis,\r
-                        l0.HasName, "NumberAxis" + UUID.randomUUID().toString());\r
+                        l0.HasName, "NumberAxis" + UUID.randomUUID().toString(),\r
+                        l0.HasLabel, "Y-axis");\r
                 \r
                 Resource renderer = GraphUtils.create2(g, jfree.XYLineRenderer);\r
 \r
index 92e8ba4d15738cf4e40056bd7b9d99a092f3462c..3caccec9ba70c2d0943c0ef5f1d19701efe9053f 100644 (file)
@@ -41,7 +41,7 @@ public class ChartUtils {
             // Create range axis\r
             axis = GraphUtils.create2(graph, jfree.NumberAxis,\r
                     l0.HasName, "NumberAxis" + UUID.randomUUID().toString(),\r
-                    l0.HasLabel, NameUtils.findFreshLabel(graph, "Range", plot),\r
+                    l0.HasLabel, NameUtils.findFreshLabel(graph, "Y-axis", plot),\r
                     jfree.Plot_rangeAxis_Inverse, plot,\r
                     l0.PartOf, plot);\r
             \r
@@ -104,7 +104,7 @@ public class ChartUtils {
         // Create series\r
         Resource series = GraphUtils.create2(graph, jfree.Series,\r
                 l0.HasName, "Series" + UUID.randomUUID().toString(),\r
-                jfree.variableRVI, rvi == null ? " Set variable" : rvi,\r
+                jfree.variableRVI, rvi == null ? " <Write variable name>" : rvi,\r
                 l0.PartOf, dataset);\r
 \r
         // Add series to the dataset's series list\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/AllVariablesOfModel.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/AllVariablesOfModel.java
new file mode 100644 (file)
index 0000000..66ae605
--- /dev/null
@@ -0,0 +1,99 @@
+package org.simantics.sysdyn.ui.trend.chart.properties;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.List;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ObjectsWithType;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.utils.strings.AlphanumComparator;\r
+\r
+/**\r
+ * Request for getting all variables of a model in a String array. Includes also \r
+ * variables inside modules.\r
+ * \r
+ * @author Teemu Lempinen\r
+ *\r
+ */\r
+public class AllVariablesOfModel implements Read<String[]>{\r
+    \r
+    private Resource model;\r
+    \r
+    public AllVariablesOfModel(Resource model) {\r
+        this.model = model;\r
+    }\r
+\r
+    @Override\r
+    public String[] perform(ReadGraph graph) throws DatabaseException {\r
+        Layer0 l0 = Layer0.getInstance(graph);\r
+        SimulationResource simu = SimulationResource.getInstance(graph);\r
+        SysdynResource sr = SysdynResource.getInstance(graph);\r
+        \r
+        // Find the model of this resource\r
+        Resource model = this.model;\r
+        while(model != null && !graph.isInstanceOf(model, sr.SysdynModel))\r
+            model = graph.getPossibleObject(model, l0.PartOf);\r
+        \r
+        if(model == null)\r
+            return new String[0];\r
+        \r
+        // Find the models configuration\r
+        Resource conf = graph.getSingleObject(model, simu.HasConfiguration);\r
+        List<String> items = new ArrayList<String>();\r
+        \r
+        // Recursively read all configurations and add items\r
+        ReadConfiguration(graph, conf, "", items);\r
+        \r
+        // Add time to the variable list\r
+        items.add("time");\r
+        \r
+        // Finally sort the results\r
+        Collections.sort(items, AlphanumComparator.CASE_INSENSITIVE_COMPARATOR);\r
+        return items.toArray(new String[items.size()]);\r
+    }\r
+    \r
+    /**\r
+     * Read components in a configuration and recursively all module configurations\r
+     * \r
+     * @param graph ReadGraph\r
+     * @param configuration Resource to be read\r
+     * @param path Current path from base realization\r
+     * @param items Found variables\r
+     * @throws DatabaseException\r
+     */\r
+    private void ReadConfiguration(ReadGraph graph, Resource configuration, String path, Collection<String> items) throws DatabaseException {\r
+        SysdynResource sr = SysdynResource.getInstance(graph);\r
+        Layer0 l0 = Layer0.getInstance(graph);\r
+        StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
+\r
+        String name;\r
+        for(Resource resource : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.IndependentVariable))) {\r
+            name = path + NameUtils.getSafeName(graph, resource);\r
+            items.add(name);\r
+        }\r
+        \r
+        for(Resource resource : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Input))) {\r
+            name = path + NameUtils.getSafeName(graph, resource);\r
+            items.add(name);\r
+        }\r
+\r
+        for(Resource module : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Module))) {\r
+            Resource instanceOf = graph.getPossibleObject(module, l0.InstanceOf);\r
+            Resource conf = graph.getPossibleObject(instanceOf, sr2.IsDefinedBy);\r
+            if(conf != null) {\r
+                String p = path + NameUtils.getSafeName(graph, module)  + ".";\r
+                ReadConfiguration(graph, conf, p, items);\r
+            }\r
+        }\r
+    }\r
+\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/VariableExistsValidator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/VariableExistsValidator.java
new file mode 100644 (file)
index 0000000..1247e82
--- /dev/null
@@ -0,0 +1,132 @@
+package org.simantics.sysdyn.ui.trend.chart.properties;\r
+\r
+import org.eclipse.jface.dialogs.IInputValidator;\r
+import org.simantics.browsing.ui.swt.widgets.TrackedText;\r
+import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.db.procedure.Listener;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.utils.AdaptionUtils;\r
+\r
+/**\r
+ * Variable exists validator for tracked text widgets. \r
+ * \r
+ * @author Teemu Lempinen\r
+ *\r
+ */\r
+public class VariableExistsValidator implements IInputValidator, Widget {\r
+\r
+    private String[] names;\r
+    private TrackedText text;\r
+    private boolean allowEmpty;\r
+    \r
+    /**\r
+     * Validate against all variables\r
+     * \r
+     * Do not allow empty input\r
+     * @param support WidgetSupport\r
+     * @param text Text widget\r
+     */\r
+    public VariableExistsValidator(WidgetSupport support, TrackedText text) {\r
+        this(support, text, false);\r
+    }\r
+    \r
+    /**\r
+     * Validate against all variables\r
+     * \r
+     * @param support WidgetSupport\r
+     * @param text Text widget\r
+     * @param allowEmpty Allow empty input text\r
+     */\r
+    public VariableExistsValidator(WidgetSupport support, TrackedText text, boolean allowEmpty) {\r
+        support.register(this);\r
+        names = new String[] {"time"};\r
+        this.text = text;\r
+        this.allowEmpty = allowEmpty;\r
+    }\r
+    \r
+    /**\r
+     * Returns null if there is a variable named newText in the model\r
+     */\r
+    @Override\r
+    public String isValid(String newText) {\r
+        if(newText == null || newText.isEmpty()) {\r
+            if(allowEmpty)\r
+                return null;\r
+            else\r
+                return "Empty name not allowed";\r
+        }\r
+        \r
+        synchronized (names) {\r
+            for(String s : names) {\r
+                if(newText.equals(s))\r
+                    return null;\r
+            }\r
+        }\r
+        \r
+        return "Not a valid variable name";\r
+    }\r
+\r
+    @Override\r
+    public void setInput(ISessionContext context, Object input) {\r
+        final Resource resource = AdaptionUtils.adaptToSingle(input, Resource.class);\r
+        \r
+        if(resource == null) {\r
+            names = new String[] {"time"};\r
+            return;\r
+        }\r
+        \r
+        Resource model = null;\r
+        try {\r
+            /* Find the model resource. It can be found with PartOf \r
+               relations from series resource in a chart */\r
+            model  = context.getSession().syncRequest(new Read<Resource>() {\r
+\r
+                @Override\r
+                public Resource perform(ReadGraph graph) throws DatabaseException {\r
+                    Resource r = resource;\r
+                    while((r = graph.getPossibleObject(r, Layer0.getInstance(graph).PartOf)) != null) {\r
+                        if(graph.isInstanceOf(r, SysdynResource.getInstance(graph).SysdynModel))\r
+                            return r;\r
+                    }\r
+                    return null;\r
+                }\r
+                \r
+            });\r
+            \r
+            if(model != null) {\r
+                // Find all variables and set them as the reference for isValid(String)\r
+                SimanticsUI.getSession().asyncRequest(\r
+                        new AllVariablesOfModel(model)\r
+                , new Listener<String[]>() {\r
+\r
+                    @Override\r
+                    public void execute(String[] result) {\r
+                        names = result;\r
+                    }\r
+\r
+                    @Override\r
+                    public void exception(Throwable t) {\r
+                        t.printStackTrace();\r
+                    }\r
+\r
+                    @Override\r
+                    public boolean isDisposed() {\r
+                        return text.isDisposed();\r
+                    }\r
+\r
+                }); \r
+            }\r
+        } catch (DatabaseException e) {\r
+            e.printStackTrace();\r
+        }\r
+    }\r
+\r
+}\r
index f4382101d9beee3c21b2a50ad59c58640614d0aa..c073516ce2e991d1296a17218f5c03bb11edb866 100644 (file)
  *******************************************************************************/\r
 package org.simantics.sysdyn.ui.trend.chart.properties;\r
 \r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-import java.util.List;\r
-\r
 import org.eclipse.jface.fieldassist.SimpleContentProposalProvider;\r
 import org.eclipse.swt.widgets.Control;\r
 import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
-import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
-import org.simantics.db.common.request.ObjectsWithType;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.db.management.ISessionContext;\r
 import org.simantics.db.procedure.Listener;\r
-import org.simantics.db.request.Read;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.simulation.ontology.SimulationResource;\r
-import org.simantics.structural.stubs.StructuralResource2;\r
-import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.ui.SimanticsUI;\r
 import org.simantics.ui.utils.AdaptionUtils;\r
-import org.simantics.utils.strings.AlphanumComparator;\r
 \r
 /**\r
  * Provides all variables a model contains\r
@@ -68,35 +53,9 @@ public class VariableProposalProvider extends SimpleContentProposalProvider impl
             return;\r
         this.resource = resource;\r
         \r
-        SimanticsUI.getSession().asyncRequest(new Read<String[]>() {\r
-\r
-            @Override\r
-            public String[] perform(ReadGraph graph) throws DatabaseException {\r
-                Layer0 l0 = Layer0.getInstance(graph);\r
-                SimulationResource simu = SimulationResource.getInstance(graph);\r
-                SysdynResource sr = SysdynResource.getInstance(graph);\r
-                \r
-                // Find the model of this resource\r
-                Resource model = resource;\r
-                while(model != null && !graph.isInstanceOf(model, sr.SysdynModel))\r
-                    model = graph.getPossibleObject(model, l0.PartOf);\r
-                \r
-                if(model == null)\r
-                    return new String[0];\r
-                \r
-                // Find the models configuration\r
-                Resource conf = graph.getSingleObject(model, simu.HasConfiguration);\r
-                List<String> items = new ArrayList<String>();\r
-                \r
-                // Recursively read all configurations and add items\r
-                ReadConfiguration(graph, conf, "", items);\r
-                \r
-                // Finally sort the results\r
-                Collections.sort(items, AlphanumComparator.CASE_INSENSITIVE_COMPARATOR);\r
-                return items.toArray(new String[items.size()]);\r
-            }\r
-\r
-        }, new Listener<String[]>() {\r
+        SimanticsUI.getSession().asyncRequest(\r
+                new AllVariablesOfModel(resource)\r
+        , new Listener<String[]>() {\r
 \r
             @Override\r
             public void execute(String[] result) {\r
@@ -119,39 +78,4 @@ public class VariableProposalProvider extends SimpleContentProposalProvider impl
         \r
     }\r
     \r
-    /**\r
-     * Read components in a configuration and recursively all module configurations\r
-     * \r
-     * @param graph ReadGraph\r
-     * @param configuration Resource to be read\r
-     * @param path Current path from base realization\r
-     * @param items Found variables\r
-     * @throws DatabaseException\r
-     */\r
-    private void ReadConfiguration(ReadGraph graph, Resource configuration, String path, Collection<String> items) throws DatabaseException {\r
-        SysdynResource sr = SysdynResource.getInstance(graph);\r
-        Layer0 l0 = Layer0.getInstance(graph);\r
-        StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
-\r
-        String name;\r
-        for(Resource resource : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.IndependentVariable))) {\r
-            name = path + NameUtils.getSafeName(graph, resource);\r
-            items.add(name);\r
-        }\r
-        \r
-        for(Resource resource : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Input))) {\r
-            name = path + NameUtils.getSafeName(graph, resource);\r
-            items.add(name);\r
-        }\r
-\r
-        for(Resource module : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Module))) {\r
-            Resource instanceOf = graph.getPossibleObject(module, l0.InstanceOf);\r
-            Resource conf = graph.getPossibleObject(instanceOf, sr2.IsDefinedBy);\r
-            if(conf != null) {\r
-                String p = path + NameUtils.getSafeName(graph, module)  + ".";\r
-                ReadConfiguration(graph, conf, p, items);\r
-            }\r
-        }\r
-    }\r
-\r
 }\r
index c7597ac041a59e0dffc34735c0af548a7e223a65..da07fe55b4014bc9fa4138f44c170fd38a7f755d 100644 (file)
@@ -30,6 +30,7 @@ import org.simantics.sysdyn.ui.trend.chart.properties.JFreeChartPropertyColorPro
 import org.simantics.sysdyn.ui.trend.chart.properties.RVIFactory;\r
 import org.simantics.sysdyn.ui.trend.chart.properties.RVIModifier;\r
 import org.simantics.sysdyn.ui.trend.chart.properties.RangeComposite;\r
+import org.simantics.sysdyn.ui.trend.chart.properties.VariableExistsValidator;\r
 \r
 /**\r
  * Composite for modifying properties of a series in a bar chart\r
@@ -53,6 +54,7 @@ public class BarSeriesPropertyComposite extends Composite {
         variable = new TrackedText(this, support, SWT.BORDER);\r
         variable.setTextFactory(new RVIFactory());\r
         variable.addModifyListener(new RVIModifier(variable.getWidget(), support));\r
+        variable.setInputValidator(new VariableExistsValidator(support, variable));\r
         variable.setColorProvider(new JFreeChartPropertyColorProvider(this.variable.getResourceManager()));\r
         GridDataFactory.fillDefaults().grab(true, false).applyTo(this.variable.getWidget());\r
 \r
index dad20d4a1613f3decc94563c8c960ee8c81f113b..7ea26a41a046f51fcdcabb00a61a40bce6d91e00 100644 (file)
@@ -34,6 +34,7 @@ import org.simantics.sysdyn.ui.trend.chart.properties.JFreeChartPropertyColorPro
 import org.simantics.sysdyn.ui.trend.chart.properties.RVIFactory;\r
 import org.simantics.sysdyn.ui.trend.chart.properties.RVIModifier;\r
 import org.simantics.sysdyn.ui.trend.chart.properties.RangeComposite;\r
+import org.simantics.sysdyn.ui.trend.chart.properties.VariableExistsValidator;\r
 \r
 /**\r
  * Composite containing the properties of a series\r
@@ -57,6 +58,7 @@ public class PieSeriesPropertyComposite extends Composite {
         variable = new TrackedText(this, support, SWT.BORDER);\r
         variable.setTextFactory(new RVIFactory());\r
         variable.addModifyListener(new RVIModifier(variable.getWidget(), support));\r
+        variable.setInputValidator(new VariableExistsValidator(support, variable));\r
         variable.setColorProvider(new JFreeChartPropertyColorProvider(this.variable.getResourceManager()));\r
         GridDataFactory.fillDefaults().grab(true, false).applyTo(this.variable.getWidget());\r
 \r
index f0568510ba4143f26e3d125e44b397166ba2c66f..1dbe6d4c8833f2ef170faadc24e530fd8837cf5f 100644 (file)
@@ -40,6 +40,7 @@ import org.simantics.sysdyn.ui.trend.chart.properties.RVIFactory;
 import org.simantics.sysdyn.ui.trend.chart.properties.RVIModifier;\r
 import org.simantics.sysdyn.ui.trend.chart.properties.RangeComposite;\r
 import org.simantics.sysdyn.ui.trend.chart.properties.TrackedSpinner;\r
+import org.simantics.sysdyn.ui.trend.chart.properties.VariableExistsValidator;\r
 import org.simantics.ui.utils.AdaptionUtils;\r
 \r
 /**\r
@@ -67,6 +68,7 @@ public class SeriesPropertyComposite extends Composite {
         variable.setTextFactory(new RVIFactory());\r
         variable.addModifyListener(new RVIModifier(variable.getWidget(), support));\r
         variable.setColorProvider(new JFreeChartPropertyColorProvider(this.variable.getResourceManager()));\r
+        variable.setInputValidator(new VariableExistsValidator(support, variable));\r
         GridDataFactory.fillDefaults().grab(true, false).applyTo(this.variable.getWidget());\r
 \r
         // Range\r
index c4e730da501d271ae1e0f3ad110257cdd497654d..9bba7fcc8d5d3086f7c98b96c564b434fa1e3b59 100644 (file)
@@ -58,6 +58,7 @@ import org.simantics.sysdyn.ui.trend.chart.properties.RVIFactory;
 import org.simantics.sysdyn.ui.trend.chart.properties.RVIModifier;\r
 import org.simantics.sysdyn.ui.trend.chart.properties.TitleFactory;\r
 import org.simantics.sysdyn.ui.trend.chart.properties.TitleModifier;\r
+import org.simantics.sysdyn.ui.trend.chart.properties.VariableExistsValidator;\r
 import org.simantics.ui.utils.AdaptionUtils;\r
 \r
 /**\r
@@ -168,6 +169,7 @@ public class XYLineGeneralPropertiesTab extends LabelPropertyTabContributor impl
         xvariable.setTextFactory(new RVIFactory());\r
         xvariable.addModifyListener(new RVIModifier(xvariable.getWidget(), domainAxisSupport));\r
         xvariable.setColorProvider(new JFreeChartPropertyColorProvider(xvariable.getResourceManager()));\r
+        xvariable.setInputValidator(new VariableExistsValidator(support, xvariable, true));\r
         GridDataFactory.fillDefaults().grab(true, false).applyTo(xvariable.getWidget());\r
         \r
         Composite axisHide = new AxisHidePropertyComposite(xgroup, context, domainAxisSupport, SWT.NONE);\r