psmith      2003/12/23 14:22:53

  Modified:    src/java/org/apache/log4j/chainsaw/helper SwingHelper.java
  Log:
  added a helper method to create a standard Cancel
  dialog action.
  
  Revision  Changes    Path
  1.2       +34 -0     
jakarta-log4j/src/java/org/apache/log4j/chainsaw/helper/SwingHelper.java
  
  Index: SwingHelper.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-log4j/src/java/org/apache/log4j/chainsaw/helper/SwingHelper.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SwingHelper.java  22 Dec 2003 10:59:23 -0000      1.1
  +++ SwingHelper.java  23 Dec 2003 22:22:53 -0000      1.2
  @@ -52,6 +52,16 @@
   import java.awt.Component;
   import java.awt.Dimension;
   import java.awt.Toolkit;
  +import java.awt.event.ActionEvent;
  +import java.awt.event.KeyEvent;
  +
  +import javax.swing.AbstractAction;
  +import javax.swing.Action;
  +import javax.swing.InputMap;
  +import javax.swing.JButton;
  +import javax.swing.JComponent;
  +import javax.swing.JDialog;
  +import javax.swing.KeyStroke;
   
   /**
    * A collection of standard utility methods for use within Swing.
  @@ -70,5 +80,29 @@
       component.setLocation(
         (screenSize.width / 2) - (component.getWidth() / 2),
         (screenSize.height / 2) - (component.getHeight() / 2));
  +  }
  +  
  +  /**
  +   * This method configures a standard Cancel action, bound to the ESC key, to 
dispose of the dialog,
  +   * and sets the buttons action to be this action, and adds the action to the 
dialog's rootPane 
  +   * action map
  +   * @param dialog
  +   * @param cancelButton
  +   */
  +  public static void configureCancelForDialog(final JDialog dialog, JButton 
cancelButton) {
  +    String CANCEL_ACTION_KEY = "CANCEL_ACTION_KEY";
  +    int noModifiers = 0;
  +    KeyStroke escapeKey = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, noModifiers, 
false);
  +    InputMap inputMap = 
dialog.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
  +    inputMap.put(escapeKey, CANCEL_ACTION_KEY);
  +    
  +    Action closeAction = new AbstractAction("Cancel") {
  +
  +      public void actionPerformed(ActionEvent arg0) {
  +        dialog.dispose();
  +      }};
  +    cancelButton.setAction(closeAction);
  +    dialog.getRootPane().getActionMap().put(CANCEL_ACTION_KEY, closeAction);
  +    
     }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to