From 4d7ee8f06ed5a27721299ca8dcbd2c72a5ebac95 Mon Sep 17 00:00:00 2001 From: Maxim Shafirov Date: Sun, 20 Feb 2005 21:06:55 +0300 Subject: [PATCH] IDEADEV-274. Yes/No buttons can be choosen with just plain Y and N keystrokes. --- .../intellij/openapi/ui/DialogWrapper.java | 64 ++++++++++++++----- .../src/com/intellij/openapi/ui/Messages.java | 22 +++---- 2 files changed, 58 insertions(+), 28 deletions(-) diff --git a/openapi/src/com/intellij/openapi/ui/DialogWrapper.java b/openapi/src/com/intellij/openapi/ui/DialogWrapper.java index 32973df782bd..8420bd66a6da 100644 --- a/openapi/src/com/intellij/openapi/ui/DialogWrapper.java +++ b/openapi/src/com/intellij/openapi/ui/DialogWrapper.java @@ -41,10 +41,13 @@ import java.awt.event.*; import java.util.ArrayList; public abstract class DialogWrapper { - - /** The default exit code for "OK" action. */ + /** + * The default exit code for "OK" action. + */ public static final int OK_EXIT_CODE = 0; - /** The default exit code for "Cancel" action. */ + /** + * The default exit code for "Cancel" action. + */ public static final int CANCEL_EXIT_CODE = 1; /** * If you use your custom exit codes you have have to start them with @@ -63,12 +66,16 @@ public abstract class DialogWrapper { private final DialogWrapperPeer myPeer; private int myExitCode = CANCEL_EXIT_CODE; - /** The shared instance of default border for dialog's content pane. */ + /** + * The shared instance of default border for dialog's content pane. + */ private static final Border ourDefaultBorder = BorderFactory.createEmptyBorder(8, 8, 8, 8); private float myHorizontalStretch = 1.0f; private float myVerticalStretch = 1.0f; - /** Defines horizontal alignment of buttons. */ + /** + * Defines horizontal alignment of buttons. + */ private int myButtonAlignment = SwingConstants.RIGHT; private boolean myCrossClosesWindow = true; private Insets myButtonMargins = new Insets(2, 16, 2, 16); @@ -81,6 +88,8 @@ public abstract class DialogWrapper { private boolean myClosed = false; private static Object ourLock = new Object(); + private Action myYesAction = null; + private Action myNoAction = null; /** @@ -188,9 +197,9 @@ public abstract class DialogWrapper { } panel.add(// left strut - Box.createHorizontalGlue(), - new GridBagConstraints(gridx++, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, - new Insets(8, 0, 0, 0), 0, 0)); + Box.createHorizontalGlue(), + new GridBagConstraints(gridx++, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, + new Insets(8, 0, 0, 0), 0, 0)); if (actions.length > 0) { JPanel buttonsPanel = createButtons(actions, buttons); panel.add(buttonsPanel, @@ -199,9 +208,9 @@ public abstract class DialogWrapper { } if (SwingConstants.CENTER == myButtonAlignment) { panel.add(// right strut - Box.createHorizontalGlue(), - new GridBagConstraints(gridx, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, - new Insets(8, 0, 0, 0), 0, 0)); + Box.createHorizontalGlue(), + new GridBagConstraints(gridx, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, + new Insets(8, 0, 0, 0), 0, 0)); } myButtons = buttons.toArray(new Component[buttons.size()]); } @@ -211,13 +220,18 @@ public abstract class DialogWrapper { private JPanel createButtons(Action[] actions, ArrayList buttons) { JPanel buttonsPanel = new JPanel(new GridLayout(1, actions.length, 5, 0)); for (int i = 0; i < actions.length; i++) { - JButton button = createJButtonForAction(actions[i]); - final Object value = actions[i].getValue(Action.MNEMONIC_KEY); + final Action action = actions[i]; + JButton button = createJButtonForAction(action); + final Object value = action.getValue(Action.MNEMONIC_KEY); if (value instanceof Integer) { - button.setMnemonic(((Integer)value).intValue()); - } - else if (value instanceof Character) { - button.setMnemonic(((Character)value).charValue()); + final int mnemonic = ((Integer)value).intValue(); + if (mnemonic == 'Y') { + myYesAction = action; + } + else if (mnemonic == 'N') { + myNoAction = action; + } + button.setMnemonic(mnemonic); } buttons.add(button); @@ -259,6 +273,14 @@ public abstract class DialogWrapper { plainText.append(ch); } button.setText(plainText.toString()); + + if (mnemonic == KeyEvent.VK_Y) { + myYesAction = action; + } + else if (mnemonic == KeyEvent.VK_N) { + myNoAction = action; + } + button.setMnemonic(mnemonic); } setMargin(button); @@ -768,6 +790,14 @@ public abstract class DialogWrapper { KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); } + + if (myYesAction != null) { + getRootPane().registerKeyboardAction(myYesAction, KeyStroke.getKeyStroke(KeyEvent.VK_Y, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); + } + + if (myNoAction != null) { + getRootPane().registerKeyboardAction(myNoAction, KeyStroke.getKeyStroke(KeyEvent.VK_N, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); + } } private void focusPreviousButton() { diff --git a/openapi/src/com/intellij/openapi/ui/Messages.java b/openapi/src/com/intellij/openapi/ui/Messages.java index 47f33eac61da..79171254fdfc 100644 --- a/openapi/src/com/intellij/openapi/ui/Messages.java +++ b/openapi/src/com/intellij/openapi/ui/Messages.java @@ -4,10 +4,10 @@ */ package com.intellij.openapi.ui; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.Application; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.project.Project; import com.intellij.ui.DocumentAdapter; import javax.swing.*; @@ -116,14 +116,14 @@ public class Messages { * @return 0 if user pressed "Yes" and returns 1 if user pressed "No" button. */ public static int showYesNoDialog(Project project, String message, String title, Icon icon) { - return showDialog(project, message, title, new String[]{"Yes", "No"}, 0, icon); + return showDialog(project, message, title, new String[]{"&Yes", "&No"}, 0, icon); } /** * @return 0 if user pressed "Yes" and returns 1 if user pressed "No" button. */ public static int showYesNoDialog(Component parent, String message, String title, Icon icon) { - return showDialog(parent, message, title, new String[]{"Yes", "No"}, 0, icon); + return showDialog(parent, message, title, new String[]{"&Yes", "&No"}, 0, icon); } /** @@ -134,7 +134,7 @@ public class Messages { * @see #showYesNoDialog(Component, String, String, Icon) */ public static int showYesNoDialog(String message, String title, Icon icon) { - return showDialog(message, title, new String[]{"Yes", "No"}, 0, icon); + return showDialog(message, title, new String[]{"&Yes", "&No"}, 0, icon); } public static int showOkCancelDialog(Project project, String message, String title, Icon icon) { @@ -192,11 +192,11 @@ public class Messages { } public static int showYesNoCancelDialog(Project project, String message, String title, Icon icon) { - return showDialog(project, message, title, new String[]{"Yes", "No", "Cancel"}, 0, icon); + return showDialog(project, message, title, new String[]{"&Yes", "&No", "&Cancel"}, 0, icon); } public static int showYesNoCancelDialog(Component parent, String message, String title, Icon icon) { - return showDialog(parent, message, title, new String[]{"Yes", "No", "Cancel"}, 0, icon); + return showDialog(parent, message, title, new String[]{"&Yes", "&No", "&Cancel"}, 0, icon); } /** @@ -206,7 +206,7 @@ public class Messages { * @see #showYesNoCancelDialog(Component, String, String, Icon) */ public static int showYesNoCancelDialog(String message, String title, Icon icon) { - return showDialog(message, title, new String[]{"Yes", "No", "Cancel"}, 0, icon); + return showDialog(message, title, new String[]{"&Yes", "&No", "&Cancel"}, 0, icon); } /** @@ -376,10 +376,10 @@ public class Messages { private void assignMnemonic(String option, Action action) { int mnemoPos = option.indexOf("&"); - if (mnemoPos > 0 && mnemoPos < option.length() - 2) { - String mnemoChar = option.substring(mnemoPos + 1, mnemoPos + 3).trim(); + if (mnemoPos >= 0 && mnemoPos < option.length() - 2) { + String mnemoChar = option.substring(mnemoPos + 1, mnemoPos + 2).trim(); if (mnemoChar.length() == 1) { - action.putValue(Action.MNEMONIC_KEY, mnemoChar); + action.putValue(Action.MNEMONIC_KEY, new Integer(mnemoChar.charAt(0))); } } }