From: lempinen Date: Mon, 23 Jan 2012 15:18:33 +0000 (+0000) Subject: Variable validator for chart configurations (refs #3082) X-Git-Tag: simantics-1.6~43 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=cfddc4693ab11cc11feb82096a3a0d091aaaf325;p=simantics%2Fsysdyn.git Variable validator for chart configurations (refs #3082) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@24021 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/newComponents/NewXYLineChartHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/newComponents/NewXYLineChartHandler.java index 98c94da8..c2aac244 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/newComponents/NewXYLineChartHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/newComponents/NewXYLineChartHandler.java @@ -78,7 +78,8 @@ public class NewXYLineChartHandler extends AbstractHandler { Resource domainAxis = GraphUtils.create2(g, jfree.NumberAxis, l0.HasName, "NumberAxis" + UUID.randomUUID().toString()); Resource rangeAxis = GraphUtils.create2(g, jfree.NumberAxis, - l0.HasName, "NumberAxis" + UUID.randomUUID().toString()); + l0.HasName, "NumberAxis" + UUID.randomUUID().toString(), + l0.HasLabel, "Y-axis"); Resource renderer = GraphUtils.create2(g, jfree.XYLineRenderer); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/ChartUtils.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/ChartUtils.java index 92e8ba4d..3caccec9 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/ChartUtils.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/ChartUtils.java @@ -41,7 +41,7 @@ public class ChartUtils { // Create range axis axis = GraphUtils.create2(graph, jfree.NumberAxis, l0.HasName, "NumberAxis" + UUID.randomUUID().toString(), - l0.HasLabel, NameUtils.findFreshLabel(graph, "Range", plot), + l0.HasLabel, NameUtils.findFreshLabel(graph, "Y-axis", plot), jfree.Plot_rangeAxis_Inverse, plot, l0.PartOf, plot); @@ -104,7 +104,7 @@ public class ChartUtils { // Create series Resource series = GraphUtils.create2(graph, jfree.Series, l0.HasName, "Series" + UUID.randomUUID().toString(), - jfree.variableRVI, rvi == null ? " Set variable" : rvi, + jfree.variableRVI, rvi == null ? " " : rvi, l0.PartOf, dataset); // Add series to the dataset's series list 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 index 00000000..66ae6054 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/AllVariablesOfModel.java @@ -0,0 +1,99 @@ +package org.simantics.sysdyn.ui.trend.chart.properties; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.request.Read; +import org.simantics.layer0.Layer0; +import org.simantics.simulation.ontology.SimulationResource; +import org.simantics.structural.stubs.StructuralResource2; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.utils.strings.AlphanumComparator; + +/** + * Request for getting all variables of a model in a String array. Includes also + * variables inside modules. + * + * @author Teemu Lempinen + * + */ +public class AllVariablesOfModel implements Read{ + + private Resource model; + + public AllVariablesOfModel(Resource model) { + this.model = model; + } + + @Override + public String[] perform(ReadGraph graph) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + SimulationResource simu = SimulationResource.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); + + // Find the model of this resource + Resource model = this.model; + while(model != null && !graph.isInstanceOf(model, sr.SysdynModel)) + model = graph.getPossibleObject(model, l0.PartOf); + + if(model == null) + return new String[0]; + + // Find the models configuration + Resource conf = graph.getSingleObject(model, simu.HasConfiguration); + List items = new ArrayList(); + + // Recursively read all configurations and add items + ReadConfiguration(graph, conf, "", items); + + // Add time to the variable list + items.add("time"); + + // Finally sort the results + Collections.sort(items, AlphanumComparator.CASE_INSENSITIVE_COMPARATOR); + return items.toArray(new String[items.size()]); + } + + /** + * Read components in a configuration and recursively all module configurations + * + * @param graph ReadGraph + * @param configuration Resource to be read + * @param path Current path from base realization + * @param items Found variables + * @throws DatabaseException + */ + private void ReadConfiguration(ReadGraph graph, Resource configuration, String path, Collection items) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + StructuralResource2 sr2 = StructuralResource2.getInstance(graph); + + String name; + for(Resource resource : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.IndependentVariable))) { + name = path + NameUtils.getSafeName(graph, resource); + items.add(name); + } + + for(Resource resource : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Input))) { + name = path + NameUtils.getSafeName(graph, resource); + items.add(name); + } + + for(Resource module : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Module))) { + Resource instanceOf = graph.getPossibleObject(module, l0.InstanceOf); + Resource conf = graph.getPossibleObject(instanceOf, sr2.IsDefinedBy); + if(conf != null) { + String p = path + NameUtils.getSafeName(graph, module) + "."; + ReadConfiguration(graph, conf, p, items); + } + } + } + +} 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 index 00000000..1247e821 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/VariableExistsValidator.java @@ -0,0 +1,132 @@ +package org.simantics.sysdyn.ui.trend.chart.properties; + +import org.eclipse.jface.dialogs.IInputValidator; +import org.simantics.browsing.ui.swt.widgets.TrackedText; +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.exception.DatabaseException; +import org.simantics.db.management.ISessionContext; +import org.simantics.db.procedure.Listener; +import org.simantics.db.request.Read; +import org.simantics.layer0.Layer0; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.ui.SimanticsUI; +import org.simantics.ui.utils.AdaptionUtils; + +/** + * Variable exists validator for tracked text widgets. + * + * @author Teemu Lempinen + * + */ +public class VariableExistsValidator implements IInputValidator, Widget { + + private String[] names; + private TrackedText text; + private boolean allowEmpty; + + /** + * Validate against all variables + * + * Do not allow empty input + * @param support WidgetSupport + * @param text Text widget + */ + public VariableExistsValidator(WidgetSupport support, TrackedText text) { + this(support, text, false); + } + + /** + * Validate against all variables + * + * @param support WidgetSupport + * @param text Text widget + * @param allowEmpty Allow empty input text + */ + public VariableExistsValidator(WidgetSupport support, TrackedText text, boolean allowEmpty) { + support.register(this); + names = new String[] {"time"}; + this.text = text; + this.allowEmpty = allowEmpty; + } + + /** + * Returns null if there is a variable named newText in the model + */ + @Override + public String isValid(String newText) { + if(newText == null || newText.isEmpty()) { + if(allowEmpty) + return null; + else + return "Empty name not allowed"; + } + + synchronized (names) { + for(String s : names) { + if(newText.equals(s)) + return null; + } + } + + return "Not a valid variable name"; + } + + @Override + public void setInput(ISessionContext context, Object input) { + final Resource resource = AdaptionUtils.adaptToSingle(input, Resource.class); + + if(resource == null) { + names = new String[] {"time"}; + return; + } + + Resource model = null; + try { + /* Find the model resource. It can be found with PartOf + relations from series resource in a chart */ + model = context.getSession().syncRequest(new Read() { + + @Override + public Resource perform(ReadGraph graph) throws DatabaseException { + Resource r = resource; + while((r = graph.getPossibleObject(r, Layer0.getInstance(graph).PartOf)) != null) { + if(graph.isInstanceOf(r, SysdynResource.getInstance(graph).SysdynModel)) + return r; + } + return null; + } + + }); + + if(model != null) { + // Find all variables and set them as the reference for isValid(String) + SimanticsUI.getSession().asyncRequest( + new AllVariablesOfModel(model) + , new Listener() { + + @Override + public void execute(String[] result) { + names = result; + } + + @Override + public void exception(Throwable t) { + t.printStackTrace(); + } + + @Override + public boolean isDisposed() { + return text.isDisposed(); + } + + }); + } + } catch (DatabaseException e) { + e.printStackTrace(); + } + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/VariableProposalProvider.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/VariableProposalProvider.java index f4382101..c073516c 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/VariableProposalProvider.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/VariableProposalProvider.java @@ -11,30 +11,15 @@ *******************************************************************************/ package org.simantics.sysdyn.ui.trend.chart.properties; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - import org.eclipse.jface.fieldassist.SimpleContentProposalProvider; import org.eclipse.swt.widgets.Control; 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.common.request.ObjectsWithType; -import org.simantics.db.common.utils.NameUtils; -import org.simantics.db.exception.DatabaseException; import org.simantics.db.management.ISessionContext; import org.simantics.db.procedure.Listener; -import org.simantics.db.request.Read; -import org.simantics.layer0.Layer0; -import org.simantics.simulation.ontology.SimulationResource; -import org.simantics.structural.stubs.StructuralResource2; -import org.simantics.sysdyn.SysdynResource; import org.simantics.ui.SimanticsUI; import org.simantics.ui.utils.AdaptionUtils; -import org.simantics.utils.strings.AlphanumComparator; /** * Provides all variables a model contains @@ -68,35 +53,9 @@ public class VariableProposalProvider extends SimpleContentProposalProvider impl return; this.resource = resource; - SimanticsUI.getSession().asyncRequest(new Read() { - - @Override - public String[] perform(ReadGraph graph) throws DatabaseException { - Layer0 l0 = Layer0.getInstance(graph); - SimulationResource simu = SimulationResource.getInstance(graph); - SysdynResource sr = SysdynResource.getInstance(graph); - - // Find the model of this resource - Resource model = resource; - while(model != null && !graph.isInstanceOf(model, sr.SysdynModel)) - model = graph.getPossibleObject(model, l0.PartOf); - - if(model == null) - return new String[0]; - - // Find the models configuration - Resource conf = graph.getSingleObject(model, simu.HasConfiguration); - List items = new ArrayList(); - - // Recursively read all configurations and add items - ReadConfiguration(graph, conf, "", items); - - // Finally sort the results - Collections.sort(items, AlphanumComparator.CASE_INSENSITIVE_COMPARATOR); - return items.toArray(new String[items.size()]); - } - - }, new Listener() { + SimanticsUI.getSession().asyncRequest( + new AllVariablesOfModel(resource) + , new Listener() { @Override public void execute(String[] result) { @@ -119,39 +78,4 @@ public class VariableProposalProvider extends SimpleContentProposalProvider impl } - /** - * Read components in a configuration and recursively all module configurations - * - * @param graph ReadGraph - * @param configuration Resource to be read - * @param path Current path from base realization - * @param items Found variables - * @throws DatabaseException - */ - private void ReadConfiguration(ReadGraph graph, Resource configuration, String path, Collection items) throws DatabaseException { - SysdynResource sr = SysdynResource.getInstance(graph); - Layer0 l0 = Layer0.getInstance(graph); - StructuralResource2 sr2 = StructuralResource2.getInstance(graph); - - String name; - for(Resource resource : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.IndependentVariable))) { - name = path + NameUtils.getSafeName(graph, resource); - items.add(name); - } - - for(Resource resource : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Input))) { - name = path + NameUtils.getSafeName(graph, resource); - items.add(name); - } - - for(Resource module : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Module))) { - Resource instanceOf = graph.getPossibleObject(module, l0.InstanceOf); - Resource conf = graph.getPossibleObject(instanceOf, sr2.IsDefinedBy); - if(conf != null) { - String p = path + NameUtils.getSafeName(graph, module) + "."; - ReadConfiguration(graph, conf, p, items); - } - } - } - } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/bar/BarSeriesPropertyComposite.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/bar/BarSeriesPropertyComposite.java index c7597ac0..da07fe55 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/bar/BarSeriesPropertyComposite.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/bar/BarSeriesPropertyComposite.java @@ -30,6 +30,7 @@ import org.simantics.sysdyn.ui.trend.chart.properties.JFreeChartPropertyColorPro import org.simantics.sysdyn.ui.trend.chart.properties.RVIFactory; import org.simantics.sysdyn.ui.trend.chart.properties.RVIModifier; import org.simantics.sysdyn.ui.trend.chart.properties.RangeComposite; +import org.simantics.sysdyn.ui.trend.chart.properties.VariableExistsValidator; /** * Composite for modifying properties of a series in a bar chart @@ -53,6 +54,7 @@ public class BarSeriesPropertyComposite extends Composite { variable = new TrackedText(this, support, SWT.BORDER); variable.setTextFactory(new RVIFactory()); variable.addModifyListener(new RVIModifier(variable.getWidget(), support)); + variable.setInputValidator(new VariableExistsValidator(support, variable)); variable.setColorProvider(new JFreeChartPropertyColorProvider(this.variable.getResourceManager())); GridDataFactory.fillDefaults().grab(true, false).applyTo(this.variable.getWidget()); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/pie/PieSeriesPropertyComposite.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/pie/PieSeriesPropertyComposite.java index dad20d4a..7ea26a41 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/pie/PieSeriesPropertyComposite.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/pie/PieSeriesPropertyComposite.java @@ -34,6 +34,7 @@ import org.simantics.sysdyn.ui.trend.chart.properties.JFreeChartPropertyColorPro import org.simantics.sysdyn.ui.trend.chart.properties.RVIFactory; import org.simantics.sysdyn.ui.trend.chart.properties.RVIModifier; import org.simantics.sysdyn.ui.trend.chart.properties.RangeComposite; +import org.simantics.sysdyn.ui.trend.chart.properties.VariableExistsValidator; /** * Composite containing the properties of a series @@ -57,6 +58,7 @@ public class PieSeriesPropertyComposite extends Composite { variable = new TrackedText(this, support, SWT.BORDER); variable.setTextFactory(new RVIFactory()); variable.addModifyListener(new RVIModifier(variable.getWidget(), support)); + variable.setInputValidator(new VariableExistsValidator(support, variable)); variable.setColorProvider(new JFreeChartPropertyColorProvider(this.variable.getResourceManager())); GridDataFactory.fillDefaults().grab(true, false).applyTo(this.variable.getWidget()); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/xyline/SeriesPropertyComposite.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/xyline/SeriesPropertyComposite.java index f0568510..1dbe6d4c 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/xyline/SeriesPropertyComposite.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/xyline/SeriesPropertyComposite.java @@ -40,6 +40,7 @@ import org.simantics.sysdyn.ui.trend.chart.properties.RVIFactory; import org.simantics.sysdyn.ui.trend.chart.properties.RVIModifier; import org.simantics.sysdyn.ui.trend.chart.properties.RangeComposite; import org.simantics.sysdyn.ui.trend.chart.properties.TrackedSpinner; +import org.simantics.sysdyn.ui.trend.chart.properties.VariableExistsValidator; import org.simantics.ui.utils.AdaptionUtils; /** @@ -67,6 +68,7 @@ public class SeriesPropertyComposite extends Composite { variable.setTextFactory(new RVIFactory()); variable.addModifyListener(new RVIModifier(variable.getWidget(), support)); variable.setColorProvider(new JFreeChartPropertyColorProvider(this.variable.getResourceManager())); + variable.setInputValidator(new VariableExistsValidator(support, variable)); GridDataFactory.fillDefaults().grab(true, false).applyTo(this.variable.getWidget()); // Range diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/xyline/XYLineGeneralPropertiesTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/xyline/XYLineGeneralPropertiesTab.java index c4e730da..9bba7fcc 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/xyline/XYLineGeneralPropertiesTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/xyline/XYLineGeneralPropertiesTab.java @@ -58,6 +58,7 @@ import org.simantics.sysdyn.ui.trend.chart.properties.RVIFactory; import org.simantics.sysdyn.ui.trend.chart.properties.RVIModifier; import org.simantics.sysdyn.ui.trend.chart.properties.TitleFactory; import org.simantics.sysdyn.ui.trend.chart.properties.TitleModifier; +import org.simantics.sysdyn.ui.trend.chart.properties.VariableExistsValidator; import org.simantics.ui.utils.AdaptionUtils; /** @@ -168,6 +169,7 @@ public class XYLineGeneralPropertiesTab extends LabelPropertyTabContributor impl xvariable.setTextFactory(new RVIFactory()); xvariable.addModifyListener(new RVIModifier(xvariable.getWidget(), domainAxisSupport)); xvariable.setColorProvider(new JFreeChartPropertyColorProvider(xvariable.getResourceManager())); + xvariable.setInputValidator(new VariableExistsValidator(support, xvariable, true)); GridDataFactory.fillDefaults().grab(true, false).applyTo(xvariable.getWidget()); Composite axisHide = new AxisHidePropertyComposite(xgroup, context, domainAxisSupport, SWT.NONE);