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.triplePane;
21  
22  import cz.zcu.mre.sparkle.gui.query.helpers.FieldType;
23  import cz.zcu.mre.sparkle.gui.query.other.TypedTextField;
24  import cz.zcu.mre.sparkle.gui.query.queryTypes.describe.DescribeQueryFormPane;
25  import cz.zcu.mre.sparkle.gui.tools.ShortCutHandler;
26  import javafx.collections.ObservableList;
27  import javafx.scene.Node;
28  import javafx.scene.input.KeyCode;
29  
30  /**
31   * @author Klara Hlavacova
32   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
33   */
34  public class TriplePaneShortcuts {
35  
36      private final TriplePane triplePane;
37  
38      public TriplePaneShortcuts(TriplePane triplePane) {
39          this.triplePane = triplePane;
40      }
41  
42      /**
43       * Handler zkratky Ctrl+A umožňující přidat proměnnou z právě editovaného
44       * pole do klauzule SELECT či DESCRIBE.
45       */
46      private final ShortCutHandler addVariableToSelectClauseHandler
47              = new ShortCutHandler(KeyCode.A, ShortCutHandler.Modifier.CONTROL) {
48  
49          @Override
50          public final boolean handle(final Object source) {
51              if (!(source instanceof TypedTextField)) {
52                  return false;
53              }
54  
55              final TypedTextField f = (TypedTextField) source;
56              boolean condition;
57              condition = (f == triplePane.getSubjectField()) && (triplePane.getSubjectField().getFieldType()
58                      == FieldType.Variable);
59              condition |= (f == triplePane.getPropertyField()) && (triplePane.getPropertyField().getFieldType()
60                      == FieldType.Variable);
61              condition |= (f == triplePane.getObjectField()) && (triplePane.getObjectField().getFieldType()
62                      == FieldType.Variable);
63  
64              if (!condition) {
65                  return false;
66              }
67  
68              /*if (parentQueryFormPane instanceof SelectQueryFormPane) {
69                  ((SelectQueryFormPane) parentQueryFormPane).tryAddVariableToSelectClause(f.getText().trim());
70              } else */
71              if (triplePane.getParentQueryFormPane() instanceof DescribeQueryFormPane) {
72                  ((DescribeQueryFormPane) triplePane.getParentQueryFormPane())
73                          .tryAddVariableToDescribeClause(f.getText().trim());
74              }
75              return true;
76          }
77      };
78  
79      /**
80       * Handler zkratky pro rychlý přesun na další pole.
81       */
82      private final ShortCutHandler moveFurtherHandler = new ShortCutHandler(KeyCode.ENTER) {
83  
84          @Override
85          public boolean handle(final Object source) {
86              if (!(source instanceof TypedTextField)) {
87                  return false;
88              }
89  
90              final TypedTextField f = (TypedTextField) source;
91              if (f.isAutoCompleteVisible()) {
92                  return false;
93              }
94  
95              if (f == triplePane.getSubjectField()) {
96                  triplePane.getPropertyField().requestFocus();
97                  return true;
98              }
99  
100             if (f == triplePane.getPropertyField()) {
101                 triplePane.getObjectField().requestFocus();
102                 return true;
103             }
104 
105             if (f == triplePane.getObjectField()) {
106                 // Výběr pole následujícího po objektu je složitější
107 
108                 final ObservableList<Node> mySubTriples = triplePane.getContainer().getChildren();
109                 ObservableList<Node> nodes = mySubTriples.isEmpty() ? triplePane.getSiblings() : mySubTriples;
110                 int nextSiblingIndex = mySubTriples.isEmpty() ? nodes.indexOf(triplePane) + 1 : 0;
111                 boolean last = nextSiblingIndex == nodes.size();
112 
113                 while (true) {
114                     if (!last) {
115                         // Skok do následující trojice
116                         do {
117                             final Node node = nodes.get(nextSiblingIndex);
118                             if (node instanceof TriplePane) {
119                                 node.requestFocus();
120                                 return true;
121                             }
122                         } while (++nextSiblingIndex < nodes.size());
123                     }
124 
125                     // Skok do skupiny o úroveň níž
126                     final TriplePane parentTriplePane = triplePane.getParentTriplePane();
127                     if (parentTriplePane == null) {
128                         break;
129                     }
130 
131                     nodes = triplePane.getParentTriplePane().getSiblings();
132                     nextSiblingIndex = nodes.indexOf(triplePane.getParentTriplePane()) + 1;
133                     last = nextSiblingIndex == nodes.size();
134                 }
135 
136                 return false;
137             }
138 
139             return false;
140         }
141     };
142 
143     public ShortCutHandler getMoveFurtherHandler() {
144         return moveFurtherHandler;
145     }
146 
147     public ShortCutHandler getAddVariableToSelectClauseHandler() {
148         return addVariableToSelectClauseHandler;
149     }
150 }