Class DummyKeyBindingsOptionsAction

  • All Implemented Interfaces:
    DockingActionIf, HelpDescriptor

    public class DummyKeyBindingsOptionsAction
    extends DockingAction
    A dummy action that allows key bindings to be edited through the key bindings options without requiring the user to implement a system action that will be added to the tool. Without this class the only editable tool key bindings are those that have corresponding DockingActions added to the tool.

    A typical usage of this class: Suppose a plugin has an action that it adds to the tool, which is logically the same action (with the same name) that a second plugin adds to the tool. Both of these actions are logically equivalent and share the same default key binding. Since these actions are logically the same, then they should share the same key binding and only have one entry in the key binding options, instead of two. This class enables both actions to have key bindings assigned via one dummy action. To do this each of the above primary actions will set themselves to not manage key bindings, so they don't appear in the key bindings options, and will then create an instance of this class and register it with the tool. Then, each of those primary actions will listen for options changes to know when the user has edited the key binding of the dummy action. The following snippet is an example of this usage, taken from the constructor of a DockingAction:

           // setup key binding management
           setKeyBindingManaged( false ); // our dummy will handle this task, not us
           KeyStroke keyStroke = ...;
           PluginTool tool = plugin.getTool();
           tool.addAction( new DummyKeyBindingsOptionsAction( ACTION_NAME, keyStroke ) );
           
           // setup options to know when the dummy key binding is changed
           Options options = tool.getOptions(ToolConstants.KEY_BINDINGS);        
           KeyStroke optionsKeyStroke = options.getKeyStroke( "Tool", ACTION_NAME, keyStroke );
           
           if (!keyStroke(optionsKeyStroke)) {
               // user-defined keystroke
               setUnvalidatedKeyBindingData(new KeyBindingData(keyStroke));
           }
           else {
               setKeyBindingData(new KeyBindingData(keyStroke));
           }
    
           options.addOptionsChangeListener( ... );
     
    And for changes to the options keybinding value:
      public void optionsChanged(Options options, String name, Object oldValue, Object newValue) {
          KeyStroke keyStroke = (KeyStroke) newValue;
          if (name.startsWith(KEY_BINDING_NAME)) {
              setUnvalidatedKeyBindingData(new KeyBindingData(keyStroke));
          }
      }
     
    • Constructor Detail

      • DummyKeyBindingsOptionsAction

        public DummyKeyBindingsOptionsAction​(java.lang.String name,
                                             javax.swing.KeyStroke defaultKeyStroke)
        Creates a new dummy action by the given name and default keystroke value.
        Parameters:
        name - The name of the action--this will be displayed in the options as the name of key binding's action.
        defaultKeyStroke - The default keystroke value for this action. This value may be null.
    • Method Detail

      • isAddToPopup

        public boolean isAddToPopup​(ActionContext context)
        Description copied from interface: DockingActionIf
        method is used to determine if this action should be displayed on the current popup. This method will only be called if the action has popup PopupMenuData set.

        Generally, actions don't need to override this method as the default implementation will defer to the #isEnabledForContext(), which will have the effect of adding the action to the popup only if it is enabled for a given context. By overriding this method, you can change this behavior so that the action will be added to the popup, even if it is disabled for the context, by having this method return true even if the #isEnabledForContext() method will return false, resulting in the action appearing in the popup menu, but begin disabled.

        Specified by:
        isAddToPopup in interface DockingActionIf
        Overrides:
        isAddToPopup in class DockingAction
        Parameters:
        context - the ActionContext from the active provider.
        Returns:
        true if this action is appropriate for the given context.
      • isEnabledForContext

        public boolean isEnabledForContext​(ActionContext context)
        Description copied from interface: DockingActionIf
        Method used to determine if this action should be enabled for the given context.

        This is the method implementors override to control when the action may be used.

        This method will be called by the DockingWindowManager for actions on the global menuBar and toolBar and for actions that have a keyBinding.

        This method will be called whenever one of the following events occur:

        1. when the user invokes the action via its keyBinding,
        2. the user changes focus from one component provider to another,
        3. the user moves a component to another position in the window or into another window,
        4. a component provider reports a change in it's context,
        5. any plugin or software component reports a general change in context (calls the tool.contextChanged(ComponentProvider) with a null parameter).
        The default implementation will simply return this action's enablement state.
        Specified by:
        isEnabledForContext in interface DockingActionIf
        Overrides:
        isEnabledForContext in class DockingAction
        Parameters:
        context - the current ActionContext for the window.
        Returns:
        true if the action should be enabled for the context or false otherwise.