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.dialogs.query;
21  
22  import cz.zcu.mre.sparkle.SparklePreferences;
23  import cz.zcu.mre.sparkle.gui.tools.AbstractDialogController;
24  import cz.zcu.mre.sparkle.gui.tools.FormControllerFactory;
25  import cz.zcu.mre.sparkle.tools.Definitions;
26  import javafx.event.ActionEvent;
27  import javafx.fxml.FXML;
28  import javafx.scene.control.ComboBox;
29  import javafx.scene.control.Label;
30  import javafx.scene.control.TextField;
31  import javafx.stage.Window;
32  import java.io.IOException;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  import java.util.prefs.Preferences;
36  
37  /**
38   * Dialog controller that obtains searching level of RDF properties.
39   *
40   * @author Josef Kazak
41   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
42   */
43  public class PathDepthDialog
44          extends AbstractDialogController {
45  
46      private static final Logger LOG = LoggerFactory.getLogger(PathDepthDialog.class);
47  
48      @FXML
49      private ComboBox<String> langComboBox;
50      @FXML
51      private Label messageLabel;
52      @FXML
53      private TextField messageTextField;
54  
55      private boolean changeTextFieldToDefaultValue = true;
56  
57      @FXML
58      private void confirmButtonOnClick(final ActionEvent event) {
59          this.changeTextFieldToDefaultValue = false;
60          close();
61      }
62  
63      @Override
64      protected final void onDialogInitialized() {
65          final Preferences prefs = SparklePreferences.getUserInstance();
66          initLangComboBox(prefs);
67      }
68  
69      @FXML
70      private void cancelButtonOnClick(final ActionEvent event) {
71          close();
72      }
73  
74      private void setMessage(String message) {
75          this.messageLabel.setText(message);
76      }
77  
78      private void setTextFieldValue(String value) {
79          this.messageTextField.setText(value);
80      }
81  
82      public static final PathDepthDialog open(final Window owner, final String title,
83              final String textFieldValue) {
84  
85          try {
86              final PathDepthDialog dlg = FormControllerFactory.load(owner, PathDepthDialog.class);
87              dlg.setTitle(title);
88  
89              dlg.setTextFieldValue(textFieldValue);
90              dlg.showAndWait();
91              return dlg;
92  
93          } catch (final IOException e) {
94              LOG.error("Exception: ", e); //$NON-NLS-1$
95          }
96          return null;
97      }
98  
99      @Override
100     protected boolean onCloseRequest() {
101         if (this.changeTextFieldToDefaultValue) {
102             this.setTextFieldValue(Definitions.BLANK_NODE_PREFIX);
103         }
104 
105         final Preferences prefs = SparklePreferences.getUserInstance();
106         Object lang = langComboBox.getSelectionModel().getSelectedItem();
107         prefs.put(SparklePreferences.LANG_LITERALS_PREFS, lang != null ? (String) lang
108                 : Definitions.EMPTY_STRING);
109 
110         return true;
111     }
112 
113     public String getResult() {
114         return this.messageTextField.getText();
115     }
116 
117     /**
118      * Nastavi seznam jazyku
119      *
120      * @param prefs preference
121      */
122     private void initLangComboBox(Preferences prefs) {
123         final String lang = prefs.get(SparklePreferences.LANG_LITERALS_PREFS, Definitions.EMPTY_STRING);
124         if (langComboBox.getItems().indexOf(lang) < 0) {
125             langComboBox.getItems().add(lang);
126         }
127         langComboBox.getSelectionModel().select(lang);
128         langComboBox.focusTraversableProperty().set(false);
129     }
130 
131     public String getPreferredLanguage() {
132         return langComboBox.getEditor().getText();
133     }
134 }