001: /*
002: * ConfirmDialog.java
003: *
004: * Copyright (C) 2000-2004 Peter Graves
005: * $Id: ConfirmDialog.java,v 1.2 2004/02/27 16:46:47 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.awt.event.ActionEvent;
025: import java.awt.event.KeyEvent;
026: import javax.swing.JPanel;
027:
028: public class ConfirmDialog extends MessageDialog implements Constants {
029: private StandardButton yesButton;
030: private StandardButton noButton;
031: private StandardButton yesToAllButton;
032: private StandardButton cancelButton;
033:
034: private int result = RESPONSE_NO;
035:
036: private boolean confirmAll;
037: protected boolean cancel;
038:
039: protected ConfirmDialog(Editor editor) {
040: super (editor != null ? editor : Editor.currentEditor());
041: }
042:
043: public static int showConfirmDialog(String text, String title) {
044: return showConfirmDialog(Editor.currentEditor(), text, title);
045: }
046:
047: public static int showConfirmDialog(Editor editor, String text,
048: String title) {
049: ConfirmDialog d = new ConfirmDialog(editor);
050: d.initialize(text, title);
051: if (editor != null)
052: editor.setDefaultCursor();
053: d.show();
054: if (editor != null && Editor.getEditorList().contains(editor))
055: editor.setFocusToDisplay();
056: return d.result;
057: }
058:
059: public static int showConfirmAllDialog(Editor editor, String text,
060: String title) {
061: ConfirmDialog d = new ConfirmDialog(editor);
062: d.confirmAll = true;
063: d.initialize(text, title);
064: if (editor != null)
065: editor.setDefaultCursor();
066: d.show();
067: if (editor != null && Editor.getEditorList().contains(editor))
068: editor.setFocusToDisplay();
069: return d.result;
070: }
071:
072: public static int showConfirmDialogWithCancelButton(Editor editor,
073: String text, String title) {
074: ConfirmDialog d = new ConfirmDialog(editor);
075: d.cancel = true;
076: d.initialize(text, title);
077: if (editor != null)
078: editor.setDefaultCursor();
079: d.show();
080: if (editor != null && Editor.getEditorList().contains(editor))
081: editor.setFocusToDisplay();
082: return d.result;
083: }
084:
085: protected void initialize(String text, String title) {
086: super .initialize(text, title);
087: yesButton.requestFocus();
088: }
089:
090: protected void addButtons() {
091: JPanel buttonPanel = new JPanel();
092: buttonPanel.setAlignmentX(LEFT_ALIGNMENT);
093: mainPanel.add(buttonPanel);
094: yesButton = new StandardButton("Yes");
095: yesButton.setActionCommand("Yes");
096: yesButton.setMnemonic('Y');
097: yesButton.addActionListener(this );
098: yesButton.addKeyListener(this );
099: buttonPanel.add(yesButton);
100: noButton = new StandardButton("No");
101: noButton.setActionCommand("No");
102: noButton.setMnemonic('N');
103: noButton.addActionListener(this );
104: noButton.addKeyListener(this );
105: buttonPanel.add(noButton);
106: if (confirmAll) {
107: yesToAllButton = new StandardButton("Yes To All");
108: yesToAllButton.setActionCommand("Yes To All");
109: yesToAllButton.setMnemonic('A');
110: yesToAllButton.addActionListener(this );
111: yesToAllButton.addKeyListener(this );
112: buttonPanel.add(yesToAllButton);
113: }
114: if (confirmAll || cancel) {
115: cancelButton = new StandardButton("Cancel");
116: cancelButton.setActionCommand("Cancel");
117: cancelButton.setMnemonic('C');
118: cancelButton.addActionListener(this );
119: cancelButton.addKeyListener(this );
120: buttonPanel.add(cancelButton);
121: }
122: }
123:
124: protected void yes() {
125: result = RESPONSE_YES;
126: dispose();
127: }
128:
129: protected void no() {
130: result = RESPONSE_NO;
131: dispose();
132: }
133:
134: protected void yesToAll() {
135: result = RESPONSE_YES_TO_ALL;
136: dispose();
137: }
138:
139: protected void cancel() {
140: result = RESPONSE_CANCEL;
141: dispose();
142: }
143:
144: public void actionPerformed(ActionEvent e) {
145: if (e.getActionCommand().equals("Yes"))
146: yes();
147: else if (e.getActionCommand().equals("No"))
148: no();
149: else if (e.getActionCommand().equals("Yes To All"))
150: yesToAll();
151: else if (e.getActionCommand().equals("Cancel"))
152: cancel();
153: }
154:
155: public void keyPressed(KeyEvent e) {
156: int keyCode = e.getKeyCode();
157: if (confirmAll) {
158: switch (keyCode) {
159: case KeyEvent.VK_Y:
160: yes();
161: break;
162: case KeyEvent.VK_N:
163: no();
164: break;
165: case KeyEvent.VK_A:
166: yesToAll();
167: break;
168: case KeyEvent.VK_C:
169: cancel();
170: break;
171: }
172: } else if (cancel) {
173: switch (keyCode) {
174: case KeyEvent.VK_Y:
175: case KeyEvent.VK_ENTER:
176: yes();
177: break;
178: case KeyEvent.VK_N:
179: no();
180: break;
181: case KeyEvent.VK_ESCAPE:
182: cancel();
183: break;
184: }
185: } else {
186: switch (keyCode) {
187: case KeyEvent.VK_Y:
188: case KeyEvent.VK_ENTER:
189: yes();
190: break;
191: case KeyEvent.VK_N:
192: case KeyEvent.VK_ESCAPE:
193: no();
194: break;
195: }
196: }
197: }
198: }
|