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.autoComplete;
21  
22  import cz.zcu.mre.sparkle.data.PrefixesStorage;
23  import cz.zcu.mre.sparkle.gui.query.QueryFormPane;
24  import cz.zcu.mre.sparkle.gui.query.other.TypedTextField;
25  import cz.zcu.mre.sparkle.tools.Utils;
26  import javafx.collections.ObservableList;
27  import javafx.scene.control.TextField;
28  
29  /**
30   * Handler pro seznam návrhů využitelný s {@link TypedTextField} nastaveným pro
31   * zápis názvu proměnné.
32   *
33   * @author Jan Smucr
34   * @author Klara Hlavacova
35   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
36   */
37  public class VariablesAutoCompleteListHandler
38          implements AutoCompleteListHandler {
39  
40      private final QueryFormPane<?> queryFormPane;
41  
42      public VariablesAutoCompleteListHandler(final QueryFormPane<?> queryFormPane) {
43          this.queryFormPane = queryFormPane;
44      }
45  
46      @Override
47      public PrefixesStorage getPrefixesStorage() {
48          return queryFormPane.getQueryPrefixesStorage();
49      }
50  
51      @Override
52      public ObservableList<Object> getAutoCompleteList(final TextField textField) {
53          final String textFieldContent = textField.getText(0, textField.getCaretPosition());
54          if ((textFieldContent == null) || (textFieldContent.isEmpty())) {
55              return Utils.getObjectList(queryFormPane.getSortedVariables());
56          }
57          return Utils.getObjectList(queryFormPane.getSortedVariables()
58                  .filtered((t) -> Utils.stringStartsWithIgnoreCase(t, textFieldContent)));
59      }
60  
61  }