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.tools;
21  
22  import javafx.event.EventHandler;
23  import javafx.scene.Node;
24  import javafx.scene.input.KeyCode;
25  import javafx.scene.input.KeyEvent;
26  
27  /**
28   * Třída pro rychlou tvorbu klávesových zkratek aktivních nad libovolnou
29   * komponentou odvozenout od {@link Node}.
30   *
31   * @author Jan Smucr
32   * @author Klara Hlavacova
33   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
34   */
35  public abstract class ShortCutHandler
36          implements EventHandler<KeyEvent> {
37  
38      public enum Modifier {
39          CONTROL, ALT, SHIFT, META, SHORTCUT
40      }
41  
42      private final KeyCode keyCode;
43      private final Modifier[] modifiers;
44  
45      public ShortCutHandler(final KeyCode keyCode, final Modifier... modifiers) {
46          this.keyCode = keyCode;
47          this.modifiers = modifiers;
48      }
49  
50      /**
51       * Připojí handler klávesové zkratky ke komponentě.
52       *
53       * @param node Uzel komponenty.
54       */
55      public final void attach(final Node node) {
56          node.addEventFilter(KeyEvent.KEY_PRESSED, this);
57      }
58  
59      /**
60       * Odpojí handler klávesové zkratky od komponenty.
61       *
62       * @param node Uzel komponenty.
63       */
64      public final void detach(final Node node) {
65          node.removeEventFilter(KeyEvent.KEY_PRESSED, this);
66      }
67  
68      @Override
69      public final void handle(final KeyEvent event) {
70          if (event.isConsumed() || (event.getCode() != keyCode)) {
71              return;
72          }
73          for (final ShortCutHandler.Modifier modifier : modifiers) {
74              switch (modifier) {
75                  case ALT:
76                      if (!event.isAltDown()) {
77                          return;
78                      }
79                      break;
80                  case CONTROL:
81                      if (!event.isControlDown()) {
82                          return;
83                      }
84                      break;
85                  case SHIFT:
86                      if (!event.isShiftDown()) {
87                          return;
88                      }
89                      break;
90                  case META:
91                      if (!event.isMetaDown()) {
92                          return;
93                      }
94                      break;
95                  case SHORTCUT:
96                      if (!event.isShortcutDown()) {
97                          return;
98                      }
99                  default:
100                     break;
101             }
102         }
103 
104         if (handle(event.getSource())) {
105             event.consume();
106         }
107     }
108 
109     /**
110      * Zpracuje událost stisku klávesové zkratky.
111      *
112      * @param source Zdroj události.
113      *
114      * @return <code>true</code>, pokud byla událost zpracována,
115      * <code>false</code> pokud ne.
116      */
117     protected abstract boolean handle(final Object source);
118 }