View Javadoc
1   /*
2    * Copyright 2013-2023 Medical Information Systems Research Group (https://medical.zcu.cz),
3    * Department of Computer Science and Engineering, University of West Bohemia.
4    * Address: Univerzitni 8, 306 14 Plzen, Czech Republic.
5    *
6    * This file is part of Sparkle project.
7    *
8    * Sparkle is free software: you can redistribute it and/or modify
9    * it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License.
11   *
12   * Sparkle is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with Sparkle. If not, see <http://www.gnu.org/licenses/>.
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   * Component controller representing a variable of the VALUES clause.
36   *
37   * @author Josef Kazak
38   * @author Klara Hlavacova
39   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
40   */
41  public class SingleValuesVariableClausePane
42          extends SingleClauseRulePane
43          implements VariablesGenerator, VariablesCollector {
44  
45      private static final String XML_ELEMENT_NAME = "ValuesVariable"; //$NON-NLS-1$
46      public static final String XML_VALUES_VARIABLE_TEXT = "variableText"; //$NON-NLS-1$
47      public static final String XML_VALUES_VARIABLE_TYPE = "variableType"; //$NON-NLS-1$
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")); //$NON-NLS-1$
60          getChildren().add(INSERTION_FIRST_POS, textField);
61  
62          watch(textField);
63      }
64  
65      @Override
66      public final String getQueryPart() {
67          // If field type is <code>FieldType.Nil</code> then it return a space.
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) { // Variable
97              textField.setFieldType(FieldType.Variable);
98              textField.setText(clearNodeValue(((SparqlParser.VarContext) e).getText(), FieldType.Variable));
99          } else if (e instanceof String) { // Nil
100             textField.setFieldType(FieldType.Nil);
101         }
102     }
103 
104     @Override
105     public final String getXMLElementName() {
106         return XML_ELEMENT_NAME;
107     }
108 }