From 33f7e07a0b9f3a022620d18e2251b3de57bb5bb0 Mon Sep 17 00:00:00 2001 From: luukkainen Date: Thu, 17 Jan 2013 13:57:47 +0000 Subject: [PATCH] Initial variable rvi / label separation. Work in progress... refs #3988 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@26633 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../jfreechart/chart/ge/SeriesLabelRule.java | 13 +- .../chart/properties/AllVariablesOfModel.java | 12 +- .../properties/IAllVariablesOfModel.java | 9 +- .../properties/IdLabelProposalProvider.java | 113 ++++++++++++++++++ .../chart/properties/RVIModifier.java | 3 +- .../properties/VariableExistsValidator.java | 21 +++- .../properties/VariableProposalProvider.java | 9 +- .../sysdyn/ui/trend/AllVariablesOfModel.java | 70 ++++++----- 8 files changed, 205 insertions(+), 45 deletions(-) create mode 100644 org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/IdLabelProposalProvider.java diff --git a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/ge/SeriesLabelRule.java b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/ge/SeriesLabelRule.java index f3db811e..399c4d02 100644 --- a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/ge/SeriesLabelRule.java +++ b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/ge/SeriesLabelRule.java @@ -20,6 +20,7 @@ import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; +import org.simantics.jfreechart.chart.properties.IAllVariablesOfModel; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.JFreeChartResource; @@ -48,10 +49,14 @@ public class SeriesLabelRule implements LabelRule { Resource resource = (Resource)content; String label = graph.getPossibleRelatedValue(resource, l0.HasLabel, Bindings.STRING); if(label == null || label.isEmpty()) { - label = graph.getPossibleRelatedValue(resource, jfree.variableRVI); - if(label != null && !label.isEmpty() && label.length() > 1) - label = label.substring(1).replace('/', '.'); - else + IAllVariablesOfModel vom = graph.adapt(resource, IAllVariablesOfModel.class); + + String rvi = graph.getPossibleRelatedValue(resource, jfree.variableRVI); + if (rvi != null) { + label =vom.getVariablesLabel(graph, rvi); + if (label == null) + label = rvi; + } else label = "Set variable"; } return Collections.singletonMap(ColumnKeys.SINGLE, label); diff --git a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/AllVariablesOfModel.java b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/AllVariablesOfModel.java index 251bdfe1..c3c1f76e 100644 --- a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/AllVariablesOfModel.java +++ b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/AllVariablesOfModel.java @@ -4,8 +4,9 @@ import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; import org.simantics.db.request.Read; +import org.simantics.utils.datastructures.Pair; -public class AllVariablesOfModel implements Read { +public class AllVariablesOfModel implements Read> { public Resource res; @@ -14,9 +15,14 @@ public class AllVariablesOfModel implements Read { } @Override - public String[] perform(ReadGraph graph) throws DatabaseException { + public Pair perform(ReadGraph graph) throws DatabaseException { IAllVariablesOfModel query = graph.adapt(res, IAllVariablesOfModel.class); - return graph.syncRequest(query); + String ids[] = graph.syncRequest(query.getVariablesIdsQuery()); + String labels[] = new String[ids.length]; + for (int i = 0; i < ids.length; i++) { + labels[i] = query.getVariablesLabel(graph, ids[i]); + } + return new Pair(ids, labels); } } diff --git a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/IAllVariablesOfModel.java b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/IAllVariablesOfModel.java index c46c4938..2bc61906 100644 --- a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/IAllVariablesOfModel.java +++ b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/IAllVariablesOfModel.java @@ -1,7 +1,14 @@ package org.simantics.jfreechart.chart.properties; +import org.simantics.db.ReadGraph; +import org.simantics.db.exception.DatabaseException; import org.simantics.db.request.Read; -public interface IAllVariablesOfModel extends Read{ +public interface IAllVariablesOfModel { + + + public Read getVariablesIdsQuery(); + + public String getVariablesLabel(ReadGraph graph, String variableId) throws DatabaseException; } diff --git a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/IdLabelProposalProvider.java b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/IdLabelProposalProvider.java new file mode 100644 index 00000000..0730fc85 --- /dev/null +++ b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/IdLabelProposalProvider.java @@ -0,0 +1,113 @@ +package org.simantics.jfreechart.chart.properties; + +import java.util.ArrayList; + +import org.eclipse.jface.fieldassist.ContentProposal; +import org.eclipse.jface.fieldassist.IContentProposal; +import org.eclipse.jface.fieldassist.IContentProposalProvider; +import org.simantics.utils.datastructures.Pair; + +public class IdLabelProposalProvider implements IContentProposalProvider{ + + /* + * The proposals provided. + */ + private Pair proposals; + + /* + * The proposals mapped to IContentProposal. Cached for speed in the case + * where filtering is not used. + */ + private IContentProposal[] contentProposals; + + /* + * Boolean that tracks whether filtering is used. + */ + private boolean filterProposals = false; + + /** + * Construct a SimpleContentProposalProvider whose content proposals are + * always the specified array of Objects. + * + * @param proposals + * the array of Strings to be returned whenever proposals are + * requested. + */ + public IdLabelProposalProvider(Pair proposals) { + super(); + this.proposals = proposals; + } + + /** + * Return an array of Objects representing the valid content proposals for a + * field. + * + * @param contents + * the current contents of the field (only consulted if filtering + * is set to true) + * @param position + * the current cursor position within the field (ignored) + * @return the array of Objects that represent valid proposals for the field + * given its current content. + */ + public IContentProposal[] getProposals(String contents, int position) { + if (filterProposals) { + ArrayList list = new ArrayList(); + for (int i = 0; i < proposals.first.length; i++) { + if (proposals.first[i].length() >= contents.length() && proposals.first[i].substring(0, contents.length()).equalsIgnoreCase(contents)) { + if (proposals.second != null) + list.add(new ContentProposal(proposals.first[i],proposals.second[i], null)); + else + list.add(new ContentProposal(proposals.first[i])); + } + } + if (proposals.second != null) { + for (int i = 0; i < proposals.second.length; i++) { + if (proposals.second[i].length() >= contents.length() && proposals.second[i].substring(0, contents.length()).equalsIgnoreCase(contents)) { + list.add(new ContentProposal(proposals.first[i],proposals.second[i], null)); + } + } + } + return (IContentProposal[]) list.toArray(new IContentProposal[list + .size()]); + } + if (contentProposals == null) { + contentProposals = new IContentProposal[proposals.first.length]; + for (int i = 0; i < proposals.first.length; i++) { + if (proposals.second != null) + contentProposals[i] = new ContentProposal(proposals.first[i],proposals.second[i],null); + else + contentProposals[i] = new ContentProposal(proposals.first[i]); + } + } + return contentProposals; + } + + /** + * Set the Strings to be used as content proposals. + * + * @param items + * the array of Strings to be used as proposals. + */ + public void setProposals(Pair items) { + this.proposals = items; + contentProposals = null; + } + + /** + * Set the boolean that controls whether proposals are filtered according to + * the current field content. + * + * @param filterProposals + * true if the proposals should be filtered to + * show only those that match the current contents of the field, + * and false if the proposals should remain the + * same, ignoring the field content. + * @since 3.3 + */ + public void setFiltering(boolean filterProposals) { + this.filterProposals = filterProposals; + // Clear any cached proposals. + contentProposals = null; + } +} diff --git a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/RVIModifier.java b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/RVIModifier.java index 6a036607..b9359e0d 100644 --- a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/RVIModifier.java +++ b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/RVIModifier.java @@ -62,7 +62,8 @@ public class RVIModifier extends TextModifyListenerImpl { e1.printStackTrace(); } - SimpleContentProposalProvider scpp = new VariableProposalProvider(control, support); + //SimpleContentProposalProvider scpp = new VariableProposalProvider(control, support); + IdLabelProposalProvider scpp = new VariableProposalProvider(control, support); scpp.setFiltering(true); ContentProposalAdapter adapter = new ContentProposalAdapter( diff --git a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/VariableExistsValidator.java b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/VariableExistsValidator.java index 08815bc8..c393bae6 100644 --- a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/VariableExistsValidator.java +++ b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/VariableExistsValidator.java @@ -14,6 +14,7 @@ import org.simantics.layer0.Layer0; import org.simantics.simulation.ontology.SimulationResource; import org.simantics.ui.SimanticsUI; import org.simantics.ui.utils.AdaptionUtils; +import org.simantics.utils.datastructures.Pair; /** * Variable exists validator for tracked text widgets. @@ -26,6 +27,7 @@ public class VariableExistsValidator implements IInputValidator, Widget { private String[] names; private TrackedText text; private boolean allowEmpty; + private boolean useLabels = false; /** * Validate against all variables @@ -38,6 +40,8 @@ public class VariableExistsValidator implements IInputValidator, Widget { this(support, text, false); } + + /** * Validate against all variables * @@ -47,11 +51,15 @@ public class VariableExistsValidator implements IInputValidator, Widget { */ public VariableExistsValidator(WidgetSupport support, TrackedText text, boolean allowEmpty) { support.register(this); - names = new String[] {"time"}; this.text = text; this.allowEmpty = allowEmpty; } + public VariableExistsValidator(WidgetSupport support, TrackedText text, boolean allowEmpty, boolean useLabels) { + this(support, text, allowEmpty); + this.useLabels = useLabels; + } + /** * Returns null if there is a variable named newText in the model */ @@ -79,7 +87,7 @@ public class VariableExistsValidator implements IInputValidator, Widget { final Resource resource = AdaptionUtils.adaptToSingle(input, Resource.class); if(resource == null) { - names = new String[] {"time"}; + names = new String[0]; return; } @@ -105,11 +113,14 @@ public class VariableExistsValidator implements IInputValidator, Widget { // Find all variables and set them as the reference for isValid(String) SimanticsUI.getSession().asyncRequest( new AllVariablesOfModel(model) - , new Listener() { + , new Listener>() { @Override - public void execute(String[] result) { - names = result; + public void execute(Pair result) { + if (!useLabels) + names = result.first; + else + names = result.second; } @Override diff --git a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/VariableProposalProvider.java b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/VariableProposalProvider.java index d6962cfa..993085c4 100644 --- a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/VariableProposalProvider.java +++ b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/VariableProposalProvider.java @@ -20,6 +20,7 @@ import org.simantics.db.management.ISessionContext; import org.simantics.db.procedure.Listener; import org.simantics.ui.SimanticsUI; import org.simantics.ui.utils.AdaptionUtils; +import org.simantics.utils.datastructures.Pair; /** * Provides all variables a model contains @@ -27,7 +28,7 @@ import org.simantics.ui.utils.AdaptionUtils; * @author Teemu Lempinen * */ -public class VariableProposalProvider extends SimpleContentProposalProvider implements Widget { +public class VariableProposalProvider extends IdLabelProposalProvider implements Widget { /** * Provides all variables a model contains. Given resource needs to be @@ -37,7 +38,7 @@ public class VariableProposalProvider extends SimpleContentProposalProvider impl * @param resource A resource that is part of a model */ public VariableProposalProvider(final Control control, WidgetSupport support) { - super(new String [] {}); + super(new Pair(new String[0], null));// super(String [] {}); support.register(this); this.control = control; } @@ -55,10 +56,10 @@ public class VariableProposalProvider extends SimpleContentProposalProvider impl SimanticsUI.getSession().asyncRequest( new AllVariablesOfModel(resource) - , new Listener() { + , new Listener>() { @Override - public void execute(String[] result) { + public void execute(Pair result) { setProposals(result); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/AllVariablesOfModel.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/AllVariablesOfModel.java index 18da4cb9..10bc8c15 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/AllVariablesOfModel.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/AllVariablesOfModel.java @@ -10,6 +10,7 @@ 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.jfreechart.chart.properties.IAllVariablesOfModel; import org.simantics.layer0.Layer0; import org.simantics.simulation.ontology.SimulationResource; @@ -32,35 +33,50 @@ public class AllVariablesOfModel implements IAllVariablesOfModel{ 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()]); + @Override + public Read getVariablesIdsQuery() { + return new VariableQuery(); + } + + @Override + public String getVariablesLabel(ReadGraph graph, String variableId) + throws DatabaseException { + return variableId.substring(1).replace('/', '.'); + } + + private class VariableQuery implements 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 = AllVariablesOfModel.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 * -- 2.47.1