KeyboardShortcuts.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package de.tu_darmstadt.informatik.tk.scopviz.ui.handlers;
  2. import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
  3. import de.tu_darmstadt.informatik.tk.scopviz.main.CreationMode;
  4. import de.tu_darmstadt.informatik.tk.scopviz.main.Layer;
  5. import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
  6. import de.tu_darmstadt.informatik.tk.scopviz.ui.GUIController;
  7. import de.tu_darmstadt.informatik.tk.scopviz.ui.GraphDisplayManager;
  8. import de.tu_darmstadt.informatik.tk.scopviz.ui.MenuBarManager;
  9. import de.tu_darmstadt.informatik.tk.scopviz.ui.mapView.MapViewFunctions;
  10. import javafx.event.ActionEvent;
  11. import javafx.event.EventHandler;
  12. import javafx.scene.input.KeyCode;
  13. import javafx.scene.input.KeyCodeCombination;
  14. import javafx.scene.input.KeyCombination;
  15. import javafx.scene.input.KeyEvent;
  16. import javafx.stage.Stage;
  17. /**
  18. * Class for defining keyboard shortcuts for all kind of actions. Shortcuts for
  19. * actions in the menu bar should be defined in the fxml file as accelerators.
  20. *
  21. * @author Julian Ohl (julian.ohl95@web.de)
  22. * @version 1.3
  23. *
  24. */
  25. public final class KeyboardShortcuts {
  26. /**
  27. * Reference to the GUI Controller for Access to various GUI Elements.
  28. */
  29. private static GUIController controller;
  30. // example of keycombinations
  31. final static KeyCombination mShift = new KeyCodeCombination(KeyCode.M, KeyCombination.SHIFT_DOWN);
  32. final static KeyCombination rAltShift = new KeyCodeCombination(KeyCode.R, KeyCombination.ALT_DOWN,
  33. KeyCombination.SHIFT_DOWN);
  34. /**
  35. * Private constructor to prevent Instantiation.
  36. */
  37. private KeyboardShortcuts() {
  38. }
  39. /**
  40. * Initialize the Keyboard Shortcuts, add them to the Stage.
  41. *
  42. * @param primaryStage
  43. * the Stage
  44. */
  45. public static void initialize(Stage primaryStage, GUIController c) {
  46. controller = c;
  47. primaryStage.addEventFilter(KeyEvent.KEY_PRESSED, buttonsPressed);
  48. primaryStage.addEventFilter(KeyEvent.KEY_RELEASED, buttonsReleased);
  49. }
  50. /**
  51. * A general Handler for any Button presses.
  52. */
  53. private static EventHandler<KeyEvent> buttonsPressed = new EventHandler<KeyEvent>() {
  54. @Override
  55. public void handle(KeyEvent event) {
  56. if (event.getCode() == KeyCode.CONTROL) {
  57. // for functionality of holding down ctrl for creating mapping
  58. // edges
  59. if (Main.getInstance().getGraphManager().getGraph().getAttribute("layer") == Layer.MAPPING) {
  60. Main.getInstance().setCreationMode(CreationMode.CREATE_DIRECTED_EDGE);
  61. Debug.out("Ctrl pressed");
  62. }
  63. }
  64. if (event.getCode() == KeyCode.ESCAPE) {
  65. // clears toolbox selection/selectionmode
  66. controller.toolbox.getSelectionModel().clearSelection();
  67. Main.getInstance().getGraphManager().deselectEdgeCreationNodes();
  68. }
  69. }
  70. };
  71. /**
  72. * A general Handler for any Button releases
  73. */
  74. private static EventHandler<KeyEvent> buttonsReleased = new EventHandler<KeyEvent>() {
  75. @Override
  76. public void handle(KeyEvent event) {
  77. if (mShift.match(event)) {
  78. Debug.out("M+Shift released");
  79. }
  80. else if (rAltShift.match(event)) {
  81. Debug.out("Alt+Shift+R released");
  82. }
  83. else if (event.getCode() == KeyCode.CONTROL) {
  84. // for functionality of holding down ctrl for creating mapping
  85. // edges
  86. if (Main.getInstance().getGraphManager().getGraph().getAttribute("layer") == Layer.MAPPING) {
  87. Main.getInstance().setCreationMode(CreationMode.CREATE_NONE);
  88. Main.getInstance().getGraphManager().deselectEdgeCreationNodes();
  89. Debug.out("Ctrl released");
  90. }
  91. }
  92. else if (event.getCode().equals(KeyCode.RIGHT)) {
  93. if (GraphDisplayManager.getCurrentLayer().equals(Layer.SYMBOL)) {
  94. MapViewFunctions.switchToNextWaypoint();
  95. }
  96. }
  97. else if (event.getCode().equals(KeyCode.LEFT)) {
  98. if (GraphDisplayManager.getCurrentLayer().equals(Layer.SYMBOL)) {
  99. MapViewFunctions.switchToPreviousWaypoint();
  100. }
  101. }
  102. }
  103. };
  104. }