001: /*
002: * AbstractDialog.java
003: *
004: * Copyright (C) 1998-2003 Peter Graves
005: * $Id: AbstractDialog.java,v 1.3 2003/07/27 01:12:21 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.Dimension;
025: import java.awt.Point;
026: import java.awt.SystemColor;
027: import java.awt.Toolkit;
028: import java.awt.event.ActionEvent;
029: import java.awt.event.ActionListener;
030: import java.awt.event.KeyEvent;
031: import java.awt.event.KeyListener;
032: import java.awt.event.WindowEvent;
033: import java.awt.event.WindowListener;
034: import javax.swing.Box;
035: import javax.swing.BoxLayout;
036: import javax.swing.JComboBox;
037: import javax.swing.JComponent;
038: import javax.swing.JDialog;
039: import javax.swing.JLabel;
040: import javax.swing.JPanel;
041: import javax.swing.JTextField;
042: import javax.swing.border.EmptyBorder;
043:
044: public abstract class AbstractDialog extends JDialog implements
045: ActionListener, KeyListener, WindowListener {
046: protected boolean cancelled;
047: protected JPanel mainPanel;
048: protected StandardButton okButton;
049: protected StandardButton cancelButton;
050:
051: private Frame owner;
052:
053: protected AbstractDialog(Editor editor) {
054: super (editor == null ? null : editor.getFrame());
055: this .owner = editor == null ? null : editor.getFrame();
056: initialize();
057: }
058:
059: protected AbstractDialog(Editor editor, String title, boolean modal) {
060: super (editor == null ? null : editor.getFrame(), title, modal);
061: this .owner = editor == null ? null : editor.getFrame();
062: initialize();
063: }
064:
065: protected AbstractDialog(Frame owner) {
066: super (owner);
067: this .owner = owner;
068: initialize();
069: }
070:
071: protected AbstractDialog(Frame owner, String title, boolean modal) {
072: super (owner, title, modal);
073: this .owner = owner;
074: initialize();
075: }
076:
077: private void initialize() {
078: setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
079: addWindowListener(this );
080: getContentPane().setLayout(
081: new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
082: setBackground(SystemColor.control);
083: mainPanel = new JPanel();
084: mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
085: mainPanel.setBorder(new EmptyBorder(6, 6, 6, 6));
086: getContentPane().add(mainPanel);
087: addKeyListener(this );
088: }
089:
090: public final boolean cancelled() {
091: return cancelled;
092: }
093:
094: // Add label and text field below it with right amount of space between them.
095: protected void addLabelAndTextField(JLabel label,
096: JTextField textField) {
097: label.setLabelFor(textField);
098: if (label.getBorder() == null)
099: label.setBorder(new EmptyBorder(0, 0, 3, 0));
100: mainPanel.add(label);
101: mainPanel.add(textField);
102: textField.addKeyListener(this );
103: }
104:
105: protected void addLabelAndComponent(JLabel label,
106: JComponent component) {
107: if (label.getLabelFor() == null)
108: label.setLabelFor(component);
109: if (label.getBorder() == null)
110: label.setBorder(new EmptyBorder(0, 0, 3, 0));
111: mainPanel.add(label);
112: mainPanel.add(component);
113: component.addKeyListener(this );
114: }
115:
116: protected final void addCheckBox(CheckBox checkBox) {
117: mainPanel.add(checkBox);
118: checkBox.addKeyListener(this );
119: }
120:
121: protected final void addVerticalStrut() {
122: mainPanel.add(Box.createVerticalStrut(6));
123: }
124:
125: protected void addOK() {
126: JPanel buttonPanel = new JPanel();
127: buttonPanel.setAlignmentX(LEFT_ALIGNMENT);
128: mainPanel.add(buttonPanel);
129: okButton = new StandardButton("OK");
130: okButton.setActionCommand("OK");
131: okButton.addActionListener(this );
132: okButton.addKeyListener(this );
133: buttonPanel.add(okButton);
134: }
135:
136: protected void addCancel() {
137: JPanel buttonPanel = new JPanel();
138: buttonPanel.setAlignmentX(LEFT_ALIGNMENT);
139: mainPanel.add(buttonPanel);
140: cancelButton = new StandardButton("Cancel");
141: cancelButton.setActionCommand("Cancel");
142: cancelButton.addActionListener(this );
143: cancelButton.addKeyListener(this );
144: buttonPanel.add(cancelButton);
145: }
146:
147: protected void addOKCancel() {
148: JPanel buttonPanel = new JPanel();
149: buttonPanel.setAlignmentX(LEFT_ALIGNMENT);
150: mainPanel.add(buttonPanel);
151: okButton = new StandardButton("OK");
152: okButton.setActionCommand("OK");
153: okButton.addActionListener(this );
154: okButton.addKeyListener(this );
155: buttonPanel.add(okButton);
156: cancelButton = new StandardButton("Cancel");
157: cancelButton.setActionCommand("Cancel");
158: cancelButton.addActionListener(this );
159: cancelButton.addKeyListener(this );
160: buttonPanel.add(cancelButton);
161: }
162:
163: protected void ok() {
164: dispose();
165: }
166:
167: protected void cancel() {
168: cancelled = true;
169: dispose();
170: }
171:
172: protected void enter() {
173: ok();
174: }
175:
176: protected void escape() {
177: cancel();
178: }
179:
180: public void dispose() {
181: super .dispose();
182: Editor.restoreFocus();
183: }
184:
185: public void actionPerformed(ActionEvent e) {
186: if (e.getActionCommand().equals("Cancel"))
187: cancel();
188: else if (e.getActionCommand().equals("OK"))
189: ok();
190: }
191:
192: public void keyPressed(KeyEvent e) {
193: if (e.getModifiers() == 0) {
194: // Special case for combo box.
195: if (e.getComponent() instanceof JComboBox) {
196: JComboBox cb = (JComboBox) e.getComponent();
197: if (cb.isPopupVisible())
198: return;
199: // Combo box popup is not visible. Fall through...
200: }
201: switch (e.getKeyCode()) {
202: case KeyEvent.VK_ENTER:
203: e.consume();
204: enter();
205: break;
206: case KeyEvent.VK_ESCAPE:
207: // Ignore escape if key event has been consumed (textfield
208: // may be cancelling an expansion).
209: if (!e.isConsumed()) {
210: e.consume();
211: escape();
212: }
213: break;
214: }
215: }
216: }
217:
218: public void keyReleased(KeyEvent e) {
219: }
220:
221: public void keyTyped(KeyEvent e) {
222: }
223:
224: public void windowActivated(WindowEvent e) {
225: }
226:
227: public void windowDeactivated(WindowEvent e) {
228: }
229:
230: public void windowOpened(WindowEvent e) {
231: }
232:
233: public void windowClosing(WindowEvent e) {
234: cancelled = true;
235: dispose();
236: }
237:
238: public void windowClosed(WindowEvent e) {
239: }
240:
241: public void windowIconified(WindowEvent e) {
242: }
243:
244: public void windowDeiconified(WindowEvent e) {
245: }
246:
247: protected void centerDialog() {
248: Dimension window = getSize();
249: Point p;
250: if (owner != null) {
251: p = owner.getLocation();
252: Dimension parent = owner.getSize();
253: p.translate((parent.width - window.width) / 2,
254: (parent.height - window.height) / 2);
255: } else {
256: Dimension parent = Toolkit.getDefaultToolkit()
257: .getScreenSize();
258: p = new Point((parent.width - window.width) / 2,
259: (parent.height - window.height) / 2);
260: }
261: setLocation(p);
262: }
263: }
|