1 package org.simantics.jfreechart.chart.properties;
\r
3 import java.util.ArrayList;
\r
4 import java.util.Collection;
\r
6 import org.eclipse.jface.dialogs.IInputValidator;
\r
7 import org.simantics.browsing.ui.swt.widgets.TrackedText;
\r
8 import org.simantics.browsing.ui.swt.widgets.impl.Widget;
\r
9 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
\r
10 import org.simantics.db.Resource;
\r
11 import org.simantics.db.exception.DatabaseException;
\r
12 import org.simantics.db.management.ISessionContext;
\r
13 import org.simantics.db.procedure.Listener;
\r
14 import org.simantics.ui.SimanticsUI;
\r
15 import org.simantics.utils.ui.AdaptionUtils;
\r
18 * Variable exists validator for tracked text widgets.
\r
20 * @author Teemu Lempinen
\r
23 public class VariableExistsValidator implements IInputValidator, Widget {
\r
25 protected Collection<ChartVariable> variables;
\r
26 protected TrackedText text;
\r
27 private boolean allowEmpty;
\r
28 @SuppressWarnings("unused")
\r
29 private boolean useLabels = false;
\r
32 * Validate against all variables
\r
34 * Do not allow empty input
\r
35 * @param support WidgetSupport
\r
36 * @param text Text widget
\r
38 public VariableExistsValidator(WidgetSupport support, TrackedText text) {
\r
39 this(support, text, false);
\r
45 * Validate against all variables
\r
47 * @param support WidgetSupport
\r
48 * @param text Text widget
\r
49 * @param allowEmpty Allow empty input text
\r
51 public VariableExistsValidator(WidgetSupport support, TrackedText text, boolean allowEmpty) {
\r
52 support.register(this);
\r
53 this.variables = new ArrayList<ChartVariable>();
\r
55 this.allowEmpty = allowEmpty;
\r
58 public VariableExistsValidator(WidgetSupport support, TrackedText text, boolean allowEmpty, boolean useLabels) {
\r
59 this(support, text, allowEmpty);
\r
60 this.useLabels = useLabels;
\r
64 * Returns null if there is a variable named newText in the model
\r
67 public String isValid(String newText) {
\r
68 if(newText == null || newText.isEmpty()) {
\r
72 return "Empty name not allowed";
\r
75 synchronized (variables) {
\r
76 for(ChartVariable variable : variables) {
\r
77 if(newText.equals(variable.getLabel()))
\r
79 if(newText.equals(variable.getRvi()))
\r
84 return "Not a valid variable name";
\r
88 public void setInput(ISessionContext context, Object input) {
\r
89 final Resource resource = AdaptionUtils.adaptToSingle(input, Resource.class);
\r
91 if(resource == null) {
\r
92 variables = new ArrayList<ChartVariable>();
\r
98 /* Find the model resource. It can be found with PartOf
\r
99 relations from series resource in a chart */
\r
100 AllVariablesOfModel query = AllVariablesOfModel.withRandomResource(context, resource);
\r
102 if(query != null) {
\r
103 // Find all variables and set them as the reference for isValid(String)
\r
104 SimanticsUI.getSession().asyncRequest(query
\r
105 , new Listener<Collection<ChartVariable>>() {
\r
108 public void execute(Collection<ChartVariable> variables) {
\r
109 VariableExistsValidator.this.variables = variables;
\r
113 public void exception(Throwable t) {
\r
114 t.printStackTrace();
\r
118 public boolean isDisposed() {
\r
119 return text.isDisposed();
\r
124 } catch (DatabaseException e) {
\r
125 e.printStackTrace();
\r