ides.api.utilities
Class GeneralUtils.JOptionPaneKeyBinder

java.lang.Object
  extended by ides.api.utilities.GeneralUtils.JOptionPaneKeyBinder
Enclosing class:
GeneralUtils

public static class GeneralUtils.JOptionPaneKeyBinder
extends java.lang.Object

This class allows the use of simple keyboard shortcuts for the Yes/No/etc. buttons in a JOptionPane.

Normally, a JOptionPane has mnemonic key bindings with its buttons. For example, in order to select the Yes button via the keyboard, one needs to press Alt+Y. However, this may be counter-intuitive as many users would expect to use simply the key Y.

This class provides a hack which lets the use of simple key shortcuts instead of the mnemonic shortcuts. It does the following:

  1. Creates a JComponent with the given text message.
  2. Checks what are the default mnemonic shortcuts. In the default English Java installation these should be Alt+Y for the Yes button, Alt+N for the No button and no shortcuts for the OK and Cancel buttons.
  3. Creates new simplified shortcuts (e.g., Y and N) and associates these shortcuts with the text component.
  4. For each one of the shortcuts, an Action is created which looks up the corresponding button in the JOptionPane and calls AbstractButton.doClick() on it.
All of this is necessary since the buttons of a JOptionPane cannot be accessed and customized directly. More information can be found on this page.

In order to use this hack, simply wrap your message. For example
JOptionPane.showConfirmDialog(window,message,title,JOptionPane.YES_NO_CANCEL_OPTION);
becomes
JOptionPane.showConfirmDialog(window,GeneralUtils.JOptionPaneKeyBinder.messageLabel(message),title,JOptionPane.YES_NO_CANCEL_OPTION);

Author:
Lenko Grigorov

Nested Class Summary
protected static class GeneralUtils.JOptionPaneKeyBinder.ShortcutHandler
          Handler for the activation of the simplified custom shortcuts.
 
Field Summary
protected static java.lang.String CANCEL
          ID for Cancel button.
protected static java.lang.String NO
          ID for No button.
protected static java.lang.String OK
          ID for OK button.
protected static java.lang.String YES
          ID for Yes button.
 
Constructor Summary
GeneralUtils.JOptionPaneKeyBinder()
           
 
Method Summary
protected static void clickButton(java.awt.Container container, java.lang.String buttonText)
          Calls the AbstractButton.doClick() method on the button with the given text in the given container.
protected static java.lang.String getMnemonicKey(java.lang.String button)
          Retrieve the simple key from the menmonic shortcut for the given JOptionPane button.
static javax.swing.JComponent messageLabel(java.lang.String message)
          Wraps a message in a JComponent with simplified key bindings for the JOptionPane buttons.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

YES

protected static final java.lang.String YES
ID for Yes button.

See Also:
Constant Field Values

NO

protected static final java.lang.String NO
ID for No button.

See Also:
Constant Field Values

OK

protected static final java.lang.String OK
ID for OK button.

See Also:
Constant Field Values

CANCEL

protected static final java.lang.String CANCEL
ID for Cancel button.

See Also:
Constant Field Values
Constructor Detail

GeneralUtils.JOptionPaneKeyBinder

public GeneralUtils.JOptionPaneKeyBinder()
Method Detail

messageLabel

public static javax.swing.JComponent messageLabel(java.lang.String message)
Wraps a message in a JComponent with simplified key bindings for the JOptionPane buttons. Use the component instead of the string message when calling option pane methods.

For example
JOptionPane.showConfirmDialog(window,message,title,JOptionPane.YES_NO_CANCEL_OPTION);
becomes
JOptionPane.showConfirmDialog(window,GeneralUtils.JOptionPaneKeyBinder.messageLabel(message),title,JOptionPane.YES_NO_CANCEL_OPTION);

Parameters:
message - text message
Returns:
a JComponent with simplified key bindings for the JOptionPane buttons

getMnemonicKey

protected static java.lang.String getMnemonicKey(java.lang.String button)
Retrieve the simple key from the menmonic shortcut for the given JOptionPane button. Only one of YES, NO, OK and CANCEL is a valid input. If the simple key cannot be retrieved (e.g., there is no mnemonic shortcut associated with the button), returns null.

Parameters:
button - one of YES, NO, OK and CANCEL
Returns:
the simple key from the menmonic shortcut for the given JOptionPane button, or null if there is no such key

clickButton

protected static void clickButton(java.awt.Container container,
                                  java.lang.String buttonText)
Calls the AbstractButton.doClick() method on the button with the given text in the given container. If the button cannot be found in the container, does nothing.

Parameters:
container - container with the button
buttonText - text of the button