1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package cz.zcu.mre.sparkle.gui.query.clause;
21
22 import cz.zcu.mre.sparkle.Messages;
23 import cz.zcu.mre.sparkle.gui.query.QueryFormPane;
24 import cz.zcu.mre.sparkle.gui.query.helpers.FieldType;
25 import cz.zcu.mre.sparkle.gui.query.helpers.VariablesCollector;
26 import cz.zcu.mre.sparkle.gui.query.helpers.VariablesGenerator;
27 import cz.zcu.mre.sparkle.gui.query.other.TypedTextField;
28 import cz.zcu.mre.sparkle.tools.Saveable;
29 import cz.zcu.mre.sparkle.tools.SparqlParser;
30 import org.w3c.dom.Element;
31 import static cz.zcu.mre.sparkle.tools.Definitions.SPACE;
32 import static cz.zcu.mre.sparkle.tools.sparqlParser.SparqlParserUtils.clearNodeValue;
33
34
35
36
37
38
39
40
41 public class SingleValuesVariableClausePane
42 extends SingleClauseRulePane
43 implements VariablesGenerator, VariablesCollector {
44
45 private static final String XML_ELEMENT_NAME = "ValuesVariable";
46 public static final String XML_VALUES_VARIABLE_TEXT = "variableText";
47 public static final String XML_VALUES_VARIABLE_TYPE = "variableType";
48
49 public SingleValuesVariableClausePane(final QueryFormPane<?> parentQueryFormPane) {
50 super(parentQueryFormPane);
51 }
52
53 @Override
54 protected final void init(final QueryFormPane<?> parentQueryFormPane) {
55 textField = new TypedTextField(parentQueryFormPane.getQueryPrefixesStorage(), true, FieldType.Variable, FieldType.Nil);
56 textField.setAutoCompleteListHandler(FieldType.Variable,
57 parentQueryFormPane.DEFAULT_VARIABLES_AUTOCOMPLETE_LIST_HANDLER);
58 textField.addVariablesCollector(this);
59 textField.setPlaceholder(Messages.getString("VARIABLE"));
60 getChildren().add(INSERTION_FIRST_POS, textField);
61
62 watch(textField);
63 }
64
65 @Override
66 public final String getQueryPart() {
67
68 if (textField.getFieldType() != FieldType.Nil) {
69 return textField.getValue();
70 } else {
71 return SPACE;
72 }
73 }
74
75 public void removeValuesVariable() {
76 removeButtonOnAction();
77 }
78
79 @Override
80 public final void save(final Element e) {
81 e.setAttribute(XML_VALUES_VARIABLE_TEXT, textField.getText());
82 e.setAttribute(XML_VALUES_VARIABLE_TYPE, textField.getFieldType().name().toLowerCase());
83 }
84
85 @Override
86 public final void load(final Object e) throws Saveable.LoadException {
87 if (e instanceof Element) {
88 Element eNode = (Element) e;
89 final String fieldTypeString = eNode.getAttribute(XML_VALUES_VARIABLE_TYPE);
90 final FieldType textFieldType = FieldType.valueOfIgnoreCase(fieldTypeString);
91 if ((textFieldType == null) || (!textFieldType.oneOf(textField.getAllowedFieldTypes()))) {
92 throw new LoadException(eNode, XML_VALUES_VARIABLE_TYPE);
93 }
94 textField.setFieldType(textFieldType);
95 textField.setText(((Element) e).getAttribute(XML_VALUES_VARIABLE_TEXT));
96 } else if (e instanceof SparqlParser.VarContext) {
97 textField.setFieldType(FieldType.Variable);
98 textField.setText(clearNodeValue(((SparqlParser.VarContext) e).getText(), FieldType.Variable));
99 } else if (e instanceof String) {
100 textField.setFieldType(FieldType.Nil);
101 }
102 }
103
104 @Override
105 public final String getXMLElementName() {
106 return XML_ELEMENT_NAME;
107 }
108 }