Class KeyStrokeField

java.lang.Object
ca.corbett.forms.fields.FormField
ca.corbett.forms.fields.KeyStrokeField
All Implemented Interfaces:
EventListener, DocumentListener

public class KeyStrokeField extends FormField implements DocumentListener
A custom FormField implementation that allows editing of a single KeyStroke assignment. Intended to be used with KeyStrokeManager, but can be used standalone as well.

Internally, this field is very similar to ShortTextField with some default values and a custom FieldValidator to ensure that the given shortcut string is valid. You can optionally allow blank input to indicate "no shortcut assigned". This is disabled by default (i.e. the given shortcut string must always be a valid keystroke).

You can optionally specify a list of reserved KeyStrokes that should not be allowed to be entered in this field. This is useful if you want to prevent users from assigning certain system-wide shortcuts that your application uses. This list is blank by default, meaning that any valid KeyStroke is allowed.

Since:
swing-extras 2.7
Author:
scorbo2
  • Field Details

  • Constructor Details

    • KeyStrokeField

      public KeyStrokeField(String label, KeyStroke keyStroke)
      Creates a new KeyStrokeField to represent the given KeyStroke, which can be null to start with an initial blank value. This will fail validation by default, unless you invoke setAllowBlank(true).
  • Method Details

    • getKeyStroke

      public KeyStroke getKeyStroke()
      Returns the KeyStroke currently represented by this field, or null if the field is blank or contains an invalid KeyStroke string.
    • setKeyStroke

      public KeyStrokeField setKeyStroke(KeyStroke keyStroke)
      Sets the KeyStroke represented by this field. Setting null or blank string is allowed, even if allowBlank is false, but the field will fail validation in that case.
    • getKeyStrokeString

      public String getKeyStrokeString()
      Returns the raw KeyStroke string currently in this field. If the current text does not represent a valid KeyStroke, this method will still return empty string.
    • getColumns

      public int getColumns()
      Returns the number of columns in the underlying JTextField.
    • setColumns

      public KeyStrokeField setColumns(int cols)
      Sets the number of columns in the underlying JTextField.
    • isAllowBlank

      public boolean isAllowBlank()
      Reports whether this field allows blank input (no keystroke assigned).
    • setAllowBlank

      public KeyStrokeField setAllowBlank(boolean allowBlank)
      Decides whether this field allows blank input (no keystroke assigned). The default is false, meaning a valid KeyStroke string must always be entered.
    • setReservedKeyStrokes

      public KeyStrokeField setReservedKeyStrokes(List<KeyStroke> reservedKeyStrokes)
      Optionally set a list of reserved KeyStrokes that cannot be assigned using this field. By default, no KeyStrokes are reserved. The given list will replace any previously set reserved KeyStrokes.
    • setReservedKeyStrokes

      public KeyStrokeField setReservedKeyStrokes(List<KeyStroke> reservedKeyStrokes, String reservedMsg)
      Optionally set a list of reserved KeyStrokes that cannot be assigned, and also sets a custom message to be used when validation fails because a reserved KeyStroke was entered. The given list will replace any previously set reserved KeyStrokes.
    • addReservedKeyStrokes

      public KeyStrokeField addReservedKeyStrokes(List<KeyStroke> additionalKeyStrokes)
      Adds additional reserved KeyStrokes to the list of KeyStrokes that cannot be assigned. Duplicates are automatically pruned. This method does not affect the reserved KeyStroke message.
    • clearReservedKeyStrokes

      public KeyStrokeField clearReservedKeyStrokes()
      Clears the list of reserved KeyStrokes, allowing all KeyStrokes to be assigned.
    • getReservedKeyStrokes

      public List<KeyStroke> getReservedKeyStrokes()
      Returns a copy of the list of reserved KeyStrokes that cannot be assigned
    • getReservedKeyStrokeMsg

      public String getReservedKeyStrokeMsg()
      Returns the validation message that will be given if a reserved KeyStroke is entered.
    • setReservedKeyStrokeMsg

      public KeyStrokeField setReservedKeyStrokeMsg(String reservedKeyStrokeMsg)
      Sets the validation message that will be given if a reserved KeyStroke is entered.
    • insertUpdate

      public void insertUpdate(DocumentEvent e)
      Specified by:
      insertUpdate in interface DocumentListener
    • removeUpdate

      public void removeUpdate(DocumentEvent e)
      Specified by:
      removeUpdate in interface DocumentListener
    • changedUpdate

      public void changedUpdate(DocumentEvent e)
      Specified by:
      changedUpdate in interface DocumentListener