import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.jfreechart.chart.properties.IAllVariablesOfModel;\r
import org.simantics.layer0.Layer0;\r
import org.simantics.sysdyn.JFreeChartResource;\r
\r
Resource resource = (Resource)content;\r
String label = graph.getPossibleRelatedValue(resource, l0.HasLabel, Bindings.STRING);\r
if(label == null || label.isEmpty()) {\r
- label = graph.getPossibleRelatedValue(resource, jfree.variableRVI);\r
- if(label != null && !label.isEmpty() && label.length() > 1)\r
- label = label.substring(1).replace('/', '.');\r
- else\r
+ IAllVariablesOfModel vom = graph.adapt(resource, IAllVariablesOfModel.class);\r
+ \r
+ String rvi = graph.getPossibleRelatedValue(resource, jfree.variableRVI);\r
+ if (rvi != null) {\r
+ label =vom.getVariablesLabel(graph, rvi);\r
+ if (label == null)\r
+ label = rvi;\r
+ } else\r
label = "Set variable";\r
}\r
return Collections.singletonMap(ColumnKeys.SINGLE, label);\r
import org.simantics.db.Resource;\r
import org.simantics.db.exception.DatabaseException;\r
import org.simantics.db.request.Read;\r
+import org.simantics.utils.datastructures.Pair;\r
\r
-public class AllVariablesOfModel implements Read<String[]> {\r
+public class AllVariablesOfModel implements Read<Pair<String[],String[]>> {\r
\r
public Resource res;\r
\r
}\r
\r
@Override\r
- public String[] perform(ReadGraph graph) throws DatabaseException {\r
+ public Pair<String[],String[]> perform(ReadGraph graph) throws DatabaseException {\r
IAllVariablesOfModel query = graph.adapt(res, IAllVariablesOfModel.class);\r
- return graph.syncRequest(query);\r
+ String ids[] = graph.syncRequest(query.getVariablesIdsQuery());\r
+ String labels[] = new String[ids.length];\r
+ for (int i = 0; i < ids.length; i++) {\r
+ labels[i] = query.getVariablesLabel(graph, ids[i]);\r
+ }\r
+ return new Pair<String[], String[]>(ids, labels);\r
}\r
\r
}\r
package org.simantics.jfreechart.chart.properties;\r
\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
import org.simantics.db.request.Read;\r
\r
-public interface IAllVariablesOfModel extends Read<String[]>{\r
+public interface IAllVariablesOfModel {\r
+ \r
+ \r
+ public Read<String[]> getVariablesIdsQuery();\r
+ \r
+ public String getVariablesLabel(ReadGraph graph, String variableId) throws DatabaseException;\r
\r
}\r
--- /dev/null
+package org.simantics.jfreechart.chart.properties;\r
+\r
+import java.util.ArrayList;\r
+\r
+import org.eclipse.jface.fieldassist.ContentProposal;\r
+import org.eclipse.jface.fieldassist.IContentProposal;\r
+import org.eclipse.jface.fieldassist.IContentProposalProvider;\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+public class IdLabelProposalProvider implements IContentProposalProvider{\r
+\r
+ /*\r
+ * The proposals provided.\r
+ */\r
+ private Pair<String[],String[]> proposals;\r
+\r
+ /*\r
+ * The proposals mapped to IContentProposal. Cached for speed in the case\r
+ * where filtering is not used.\r
+ */\r
+ private IContentProposal[] contentProposals;\r
+\r
+ /*\r
+ * Boolean that tracks whether filtering is used.\r
+ */\r
+ private boolean filterProposals = false;\r
+\r
+ /**\r
+ * Construct a SimpleContentProposalProvider whose content proposals are\r
+ * always the specified array of Objects.\r
+ * \r
+ * @param proposals\r
+ * the array of Strings to be returned whenever proposals are\r
+ * requested.\r
+ */\r
+ public IdLabelProposalProvider(Pair<String[],String[]> proposals) {\r
+ super();\r
+ this.proposals = proposals;\r
+ }\r
+\r
+ /**\r
+ * Return an array of Objects representing the valid content proposals for a\r
+ * field. \r
+ * \r
+ * @param contents\r
+ * the current contents of the field (only consulted if filtering\r
+ * is set to <code>true</code>)\r
+ * @param position\r
+ * the current cursor position within the field (ignored)\r
+ * @return the array of Objects that represent valid proposals for the field\r
+ * given its current content.\r
+ */\r
+ public IContentProposal[] getProposals(String contents, int position) {\r
+ if (filterProposals) {\r
+ ArrayList list = new ArrayList();\r
+ for (int i = 0; i < proposals.first.length; i++) {\r
+ if (proposals.first[i].length() >= contents.length() && proposals.first[i].substring(0, contents.length()).equalsIgnoreCase(contents)) {\r
+ if (proposals.second != null)\r
+ list.add(new ContentProposal(proposals.first[i],proposals.second[i], null));\r
+ else\r
+ list.add(new ContentProposal(proposals.first[i]));\r
+ }\r
+ }\r
+ if (proposals.second != null) {\r
+ for (int i = 0; i < proposals.second.length; i++) {\r
+ if (proposals.second[i].length() >= contents.length() && proposals.second[i].substring(0, contents.length()).equalsIgnoreCase(contents)) {\r
+ list.add(new ContentProposal(proposals.first[i],proposals.second[i], null));\r
+ }\r
+ }\r
+ }\r
+ return (IContentProposal[]) list.toArray(new IContentProposal[list\r
+ .size()]);\r
+ }\r
+ if (contentProposals == null) {\r
+ contentProposals = new IContentProposal[proposals.first.length];\r
+ for (int i = 0; i < proposals.first.length; i++) {\r
+ if (proposals.second != null)\r
+ contentProposals[i] = new ContentProposal(proposals.first[i],proposals.second[i],null);\r
+ else\r
+ contentProposals[i] = new ContentProposal(proposals.first[i]);\r
+ }\r
+ }\r
+ return contentProposals;\r
+ }\r
+\r
+ /**\r
+ * Set the Strings to be used as content proposals.\r
+ * \r
+ * @param items\r
+ * the array of Strings to be used as proposals.\r
+ */\r
+ public void setProposals(Pair<String[],String[]> items) {\r
+ this.proposals = items;\r
+ contentProposals = null;\r
+ }\r
+\r
+ /**\r
+ * Set the boolean that controls whether proposals are filtered according to\r
+ * the current field content.\r
+ * \r
+ * @param filterProposals\r
+ * <code>true</code> if the proposals should be filtered to\r
+ * show only those that match the current contents of the field,\r
+ * and <code>false</code> if the proposals should remain the\r
+ * same, ignoring the field content.\r
+ * @since 3.3\r
+ */\r
+ public void setFiltering(boolean filterProposals) {\r
+ this.filterProposals = filterProposals;\r
+ // Clear any cached proposals.\r
+ contentProposals = null;\r
+ }\r
+}\r
e1.printStackTrace();\r
}\r
\r
- SimpleContentProposalProvider scpp = new VariableProposalProvider(control, support);\r
+ //SimpleContentProposalProvider scpp = new VariableProposalProvider(control, support);\r
+ IdLabelProposalProvider scpp = new VariableProposalProvider(control, support);\r
scpp.setFiltering(true);\r
\r
ContentProposalAdapter adapter = new ContentProposalAdapter(\r
import org.simantics.simulation.ontology.SimulationResource;\r
import org.simantics.ui.SimanticsUI;\r
import org.simantics.ui.utils.AdaptionUtils;\r
+import org.simantics.utils.datastructures.Pair;\r
\r
/**\r
* Variable exists validator for tracked text widgets. \r
private String[] names;\r
private TrackedText text;\r
private boolean allowEmpty;\r
+ private boolean useLabels = false;\r
\r
/**\r
* Validate against all variables\r
this(support, text, false);\r
}\r
\r
+ \r
+ \r
/**\r
* Validate against all variables\r
* \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
+ public VariableExistsValidator(WidgetSupport support, TrackedText text, boolean allowEmpty, boolean useLabels) {\r
+ this(support, text, allowEmpty);\r
+ this.useLabels = useLabels;\r
+ }\r
+ \r
/**\r
* Returns null if there is a variable named newText in the model\r
*/\r
final Resource resource = AdaptionUtils.adaptToSingle(input, Resource.class);\r
\r
if(resource == null) {\r
- names = new String[] {"time"};\r
+ names = new String[0];\r
return;\r
}\r
\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
+ , new Listener<Pair<String[],String[]>>() {\r
\r
@Override\r
- public void execute(String[] result) {\r
- names = result;\r
+ public void execute(Pair<String[],String[]> result) {\r
+ if (!useLabels)\r
+ names = result.first;\r
+ else\r
+ names = result.second;\r
}\r
\r
@Override\r
import org.simantics.db.procedure.Listener;\r
import org.simantics.ui.SimanticsUI;\r
import org.simantics.ui.utils.AdaptionUtils;\r
+import org.simantics.utils.datastructures.Pair;\r
\r
/**\r
* Provides all variables a model contains\r
* @author Teemu Lempinen\r
*\r
*/\r
-public class VariableProposalProvider extends SimpleContentProposalProvider implements Widget {\r
+public class VariableProposalProvider extends IdLabelProposalProvider implements Widget {\r
\r
/**\r
* Provides all variables a model contains. Given resource needs to be\r
* @param resource A resource that is part of a model\r
*/\r
public VariableProposalProvider(final Control control, WidgetSupport support) {\r
- super(new String [] {});\r
+ super(new Pair<String[], String[]>(new String[0], null));// super(String [] {});\r
support.register(this);\r
this.control = control;\r
}\r
\r
SimanticsUI.getSession().asyncRequest(\r
new AllVariablesOfModel(resource)\r
- , new Listener<String[]>() {\r
+ , new Listener<Pair<String[],String[]>>() {\r
\r
@Override\r
- public void execute(String[] result) {\r
+ public void execute(Pair<String[],String[]> result) {\r
setProposals(result);\r
}\r
\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.jfreechart.chart.properties.IAllVariablesOfModel;\r
import org.simantics.layer0.Layer0;\r
import org.simantics.simulation.ontology.SimulationResource;\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
+ @Override\r
+ public Read<String[]> getVariablesIdsQuery() {\r
+ return new VariableQuery();\r
+ } \r
+ \r
+ @Override\r
+ public String getVariablesLabel(ReadGraph graph, String variableId)\r
+ throws DatabaseException {\r
+ return variableId.substring(1).replace('/', '.');\r
+ }\r
+ \r
+ private class VariableQuery implements Read<String[]> {\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 = AllVariablesOfModel.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
+ \r
+ \r
/**\r
* Read components in a configuration and recursively all module configurations\r
* \r