Class InputHandler

  • All Implemented Interfaces:
    java.awt.event.KeyListener, java.util.EventListener
    Direct Known Subclasses:
    DefaultInputHandler

    public abstract class InputHandler
    extends java.lang.Object
    implements java.awt.event.KeyListener
    An input handler converts the user's key strokes into concrete actions. It also takes care of macro recording and action repetition.

    This class provides all the necessary support code for an input handler, but doesn't actually do any key binding logic. It is up to the implementations of this class to do so.

    See Also:
    DefaultInputHandler
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  InputHandler.NonRepeatable
      If an action implements this interface, it should not be repeated.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int repeatCount  
    • Constructor Summary

      Constructors 
      Constructor Description
      InputHandler()  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      abstract void addDefaultKeyBindings()
      Adds the default key bindings to this input handler.
      abstract InputHandler copy()
      Returns a copy of this input handler that shares the same key bindings.
      protected void executeAction​(java.awt.event.ActionListener listener, java.lang.Object source, @Nullable java.lang.String actionCommand)
      Executes the specified action, repeating and recording it as necessary.
      int getRepeatCount()
      Returns the number of times the next action will be repeated.
      static @NotNull JEditTextArea getTextArea​(java.util.EventObject evt)
      Returns the text area that fired the specified event.
      protected boolean handleGrabAction​(java.awt.event.KeyEvent evt)
      If a key is being grabbed, this method should be called with the appropriate key event.
      protected boolean isRepeatEnabled()
      Returns if repetition is enabled.
      void setRepeatCount​(int repeatCount)
      Sets the number of times the next action will be repeated.
      void setRepeatEnabled​(boolean repeat)
      Sets the enabled state of repetition.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.awt.event.KeyListener

        keyPressed, keyReleased, keyTyped
    • Field Detail

      • repeatCount

        protected int repeatCount
    • Constructor Detail

      • InputHandler

        public InputHandler()
    • Method Detail

      • addDefaultKeyBindings

        public abstract void addDefaultKeyBindings()
        Adds the default key bindings to this input handler. This should not be called in the constructor of this input handler, because applications might load the key bindings from a file, etc.
      • isRepeatEnabled

        protected boolean isRepeatEnabled()
        Returns if repetition is enabled. When repetition is enabled, actions will be executed multiple times. This is usually invoked with a special key stroke in the input handler.
        Returns:
        true if repeating is enabled, otherwise false
      • setRepeatEnabled

        public void setRepeatEnabled​(boolean repeat)
        Sets the enabled state of repetition. When repetition is enabled, actions will be executed multiple times. Once repeating is enabled, the input handler should read a number from the keyboard.
        Parameters:
        repeat - true for enabling repetition, false for disabling it
      • getRepeatCount

        public int getRepeatCount()
        Returns the number of times the next action will be repeated.
        Returns:
        number of times the next action will be repeated (1 or the set repeat count)
      • setRepeatCount

        public void setRepeatCount​(int repeatCount)
        Sets the number of times the next action will be repeated.
        Parameters:
        repeatCount - the repeat count
      • copy

        public abstract InputHandler copy()
        Returns a copy of this input handler that shares the same key bindings. Setting key bindings in the copy will also set them in the original.
        Returns:
        copy of this InputHandler
      • executeAction

        protected void executeAction​(java.awt.event.ActionListener listener,
                                     java.lang.Object source,
                                     @Nullable
                                     @Nullable java.lang.String actionCommand)
        Executes the specified action, repeating and recording it as necessary.
        Parameters:
        listener - the action listener
        source - the event source
        actionCommand - the action command
      • getTextArea

        @NotNull
        public static @NotNull JEditTextArea getTextArea​(java.util.EventObject evt)
        Returns the text area that fired the specified event. This method will throw an Error if evt does not have a JEditTextArea in it's source component hierarchy.
        Parameters:
        evt - the event
        Returns:
        the JEditTextArea found as the source of evt
      • handleGrabAction

        protected boolean handleGrabAction​(java.awt.event.KeyEvent evt)
        If a key is being grabbed, this method should be called with the appropriate key event. It executes the grab action with the typed character as the parameter.
        Parameters:
        evt - the key event the key should be grabbed of
        Returns:
        whether a grab action was active