Class KeyBindings

java.lang.Object
com.j3d.engine.interact.input.keyboard.KeyBindings

public class KeyBindings extends Object
A class that manages the key bindings for the application. It allows for easy addition and removal of key bindings, and handles the actions associated with each key binding.
Author:
Lehlogonolo Poole
See Also:
  • Field Details

    • inputMap

      private final InputMap inputMap
    • actionMap

      private final ActionMap actionMap
    • keys

      private final ArrayList<J3Key> keys
      List of registered keys.
    • globalKeyStrokes

      public static ArrayList<KeyStroke> globalKeyStrokes
    • prohibited

      private ArrayList<KeyStroke> prohibited
      A list of prohibited key bindings that should not be added to the input and action maps. This is used to prevent common key bindings that would interfere with the application's functionality from being added by the user.

      These are defined by EngineFrame.jMenuBar1 using accelerators.

    • clearInferredSelectionType

      public static AbstractAction clearInferredSelectionType
  • Constructor Details

    • KeyBindings

      public KeyBindings(EngineFrame e)
    • KeyBindings

      public KeyBindings(InputMap im, ActionMap am)
      Initialises the key bindings for the application.
      Parameters:
      im - the input map to use for key bindings
      am - the action map to use for key bindings
  • Method Details

    • flatten

      public static Stream<JMenuItem> flatten(MenuElement m)
      Flattens a MenuElement into a stream of JMenuItems.
      Parameters:
      m - The menu element to flatten.
      Returns:
      A stream of JMenuItems.
    • getGlobalKeyStrokes

      public static ArrayList<KeyStroke> getGlobalKeyStrokes()
    • rJ3Key

      private void rJ3Key(J3Key key)
      Registers a J3Key. unsafely.
      Parameters:
      key - the J3Key to register
    • registerJ3Key

      public void registerJ3Key(J3Key key)
      Registers a J3Key
      Parameters:
      key - the J3Key to register
    • updateLinks

      private void updateLinks(J3Key leader, KeyStroke newKeyStroke)
    • childModifiersClash

      public static boolean childModifiersClash(J3Key leader)
      Inspects whether the leader of a chain of keystroke's updated modifier will clash or cause a clash with any 1 child keystroke.

      Preamble : KeyEvent are bit masks, meaning combinations are just the bitwise OR and two combinations such as SHIFT | SHIFT become SHIFT

      In essence a conflict is made when the bitwise OR of the leader's modifiers with any child modifier is equal to the leader modifier.

      
                (leaderMod | childMod) == leaderMod
                

      Say we have the following test cases, where the first keystroke is the leader and the rest are childrten.

      
            1. (I) -> (SHIFT + I) -> (CTRL + I)
            2. (P) -> (ALT + P) -> (SHIFT + P)
            3. K -> (CTRL + K) -> (ALT + K)
            
      And we'd like to rebind the leader of each link to ALT + O, SHIFT + S and CTRL + ALT + J respectively. (We Only care about the SHIFT, CTRL, ALT, ALT_GRADE and META modifiers, so the letters themselves will be ignored and replaced with a question mark to represent the letter)

      In link 1 it becomes

      
                (ALT + ?) -> (SHIFT + ALT + ?) -> (CTRL + ALT + ?)
                
      No child's modifier conflicts with the leader key's modifier. This is a valid change.

      In link 2

      
                (SHIFT + ?) -> (ALT + SHIFT + ?) -> (SHIFT + SHIFT + ?)
                // The 2 shifts simplify to a singular shift via bitwise OR
                (SHIFT + ?) -> (ALT + SHIFT + ?) -> (SHIFT + ?)
                
      Here due to the simplification, the leader conflicts with the last child. This is an invalid case.

      In link 3

      
                (CTRL + ALT + ?) -> (CTRL + CTRl + ALT + ?) -> (ALT + CTRL + ALT + ?)
                // Again the duplicate shifts OR together
                (CTRL + ALT + ?) -> (CTRL + ALT + ?) -> (CTRL + ALT + ?)
                
      This link, obviously also produces conflicts, accept both children conflict with the parent node.

      And this method, simply checks for these.
      Parameters:
      leader - The leader in question.
      Returns:
      Whether any child modifier of this key clash with said key or not.
      See Also:
    • rebindJ3KeyKeystroke

      public KeyBindings.UpdatedJ3Key rebindJ3KeyKeystroke(KeyStroke old, J3Key key)
      Updates the keystroke of a J3Key. If an existing J3Key with the same keystroke is found, it swaps the 2 keys.
      Parameters:
      old - The old keystroke of this key.
      key - The key in question
      Returns:
      An KeyBindings.UpdatedJ3Key object that indicates the result of the update.
    • removeJ3Key

      public J3Key removeJ3Key(UUID id)
      Removes a J3Key
      Parameters:
      id - the id of the J3Key to remove
      Returns:
      the J3Key that was removed
    • removeJ3KeyConsumer

      public void removeJ3KeyConsumer(UUID id)
      Removes a J3Key
      Parameters:
      id - the id of the J3Key to remove
    • getActionMap

      public ActionMap getActionMap()
      Returns the action map containing the actions bound to keys.
      Returns:
      the action map
    • getInputMap

      public InputMap getInputMap()
      Returns the input map containing the key bindings.
      Returns:
      the input map
    • commandPaletteFocusOwner

      public static boolean commandPaletteFocusOwner()