1 package org.simantics.sysdyn.ui.trend.chart.properties;
\r
3 import org.eclipse.jface.dialogs.IInputValidator;
\r
4 import org.simantics.browsing.ui.swt.widgets.TrackedText;
\r
5 import org.simantics.browsing.ui.swt.widgets.impl.Widget;
\r
6 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
\r
7 import org.simantics.db.ReadGraph;
\r
8 import org.simantics.db.Resource;
\r
9 import org.simantics.db.exception.DatabaseException;
\r
10 import org.simantics.db.management.ISessionContext;
\r
11 import org.simantics.db.procedure.Listener;
\r
12 import org.simantics.db.request.Read;
\r
13 import org.simantics.layer0.Layer0;
\r
14 import org.simantics.sysdyn.SysdynResource;
\r
15 import org.simantics.ui.SimanticsUI;
\r
16 import org.simantics.ui.utils.AdaptionUtils;
\r
19 * Variable exists validator for tracked text widgets.
\r
21 * @author Teemu Lempinen
\r
24 public class VariableExistsValidator implements IInputValidator, Widget {
\r
26 private String[] names;
\r
27 private TrackedText text;
\r
28 private boolean allowEmpty;
\r
31 * Validate against all variables
\r
33 * Do not allow empty input
\r
34 * @param support WidgetSupport
\r
35 * @param text Text widget
\r
37 public VariableExistsValidator(WidgetSupport support, TrackedText text) {
\r
38 this(support, text, false);
\r
42 * Validate against all variables
\r
44 * @param support WidgetSupport
\r
45 * @param text Text widget
\r
46 * @param allowEmpty Allow empty input text
\r
48 public VariableExistsValidator(WidgetSupport support, TrackedText text, boolean allowEmpty) {
\r
49 support.register(this);
\r
50 names = new String[] {"time"};
\r
52 this.allowEmpty = allowEmpty;
\r
56 * Returns null if there is a variable named newText in the model
\r
59 public String isValid(String newText) {
\r
60 if(newText == null || newText.isEmpty()) {
\r
64 return "Empty name not allowed";
\r
67 synchronized (names) {
\r
68 for(String s : names) {
\r
69 if(newText.equals(s))
\r
74 return "Not a valid variable name";
\r
78 public void setInput(ISessionContext context, Object input) {
\r
79 final Resource resource = AdaptionUtils.adaptToSingle(input, Resource.class);
\r
81 if(resource == null) {
\r
82 names = new String[] {"time"};
\r
86 Resource model = null;
\r
88 /* Find the model resource. It can be found with PartOf
\r
89 relations from series resource in a chart */
\r
90 model = context.getSession().syncRequest(new Read<Resource>() {
\r
93 public Resource perform(ReadGraph graph) throws DatabaseException {
\r
94 Resource r = resource;
\r
95 while((r = graph.getPossibleObject(r, Layer0.getInstance(graph).PartOf)) != null) {
\r
96 if(graph.isInstanceOf(r, SysdynResource.getInstance(graph).SysdynModel))
\r
104 if(model != null) {
\r
105 // Find all variables and set them as the reference for isValid(String)
\r
106 SimanticsUI.getSession().asyncRequest(
\r
107 new AllVariablesOfModel(model)
\r
108 , new Listener<String[]>() {
\r
111 public void execute(String[] result) {
\r
116 public void exception(Throwable t) {
\r
117 t.printStackTrace();
\r
121 public boolean isDisposed() {
\r
122 return text.isDisposed();
\r
127 } catch (DatabaseException e) {
\r
128 e.printStackTrace();
\r