001: /*
002: * EnhancedDialog.java - Handles OK/Cancel for you
003: * Copyright (C) 1998, 1999, 2001 Slava Pestov
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019:
020: package org.gjt.sp.jedit.gui;
021:
022: import javax.swing.*;
023: import java.awt.event.*;
024: import java.awt.*;
025:
026: /**
027: * A dialog box that handles window closing, the ENTER key and the ESCAPE
028: * key for you. All you have to do is implement ok() (called when
029: * Enter is pressed) and cancel() (called when Escape is pressed, or window
030: * is closed).
031: * @author Slava Pestov
032: * @version $Id: EnhancedDialog.java 5339 2006-01-25 23:12:07Z spestov $
033: */
034: public abstract class EnhancedDialog extends JDialog {
035: public EnhancedDialog(Frame parent, String title, boolean modal) {
036: super (parent, title, modal);
037: _init();
038: }
039:
040: public EnhancedDialog(Dialog parent, String title, boolean modal) {
041: super (parent, title, modal);
042: _init();
043: }
044:
045: public boolean getEnterEnabled() {
046: return enterEnabled;
047: }
048:
049: public void setEnterEnabled(boolean enterEnabled) {
050: this .enterEnabled = enterEnabled;
051: }
052:
053: public abstract void ok();
054:
055: public abstract void cancel();
056:
057: //{{{ Private members
058: private void _init() {
059: ((Container) getLayeredPane())
060: .addContainerListener(new ContainerHandler());
061: getContentPane().addContainerListener(new ContainerHandler());
062:
063: keyHandler = new KeyHandler();
064: addKeyListener(keyHandler);
065: addWindowListener(new WindowHandler());
066:
067: setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
068:
069: enterEnabled = true;
070: }
071:
072: //}}}
073:
074: // protected members
075: protected KeyHandler keyHandler;
076: protected boolean enterEnabled;
077:
078: // Recursively adds our key listener to sub-components
079: class ContainerHandler extends ContainerAdapter {
080: public void componentAdded(ContainerEvent evt) {
081: componentAdded(evt.getChild());
082: }
083:
084: public void componentRemoved(ContainerEvent evt) {
085: componentRemoved(evt.getChild());
086: }
087:
088: private void componentAdded(Component comp) {
089: comp.addKeyListener(keyHandler);
090: if (comp instanceof Container) {
091: Container cont = (Container) comp;
092: cont.addContainerListener(this );
093: Component[] comps = cont.getComponents();
094: for (int i = 0; i < comps.length; i++) {
095: componentAdded(comps[i]);
096: }
097: }
098: }
099:
100: private void componentRemoved(Component comp) {
101: comp.removeKeyListener(keyHandler);
102: if (comp instanceof Container) {
103: Container cont = (Container) comp;
104: cont.removeContainerListener(this );
105: Component[] comps = cont.getComponents();
106: for (int i = 0; i < comps.length; i++) {
107: componentRemoved(comps[i]);
108: }
109: }
110: }
111: }
112:
113: class KeyHandler extends KeyAdapter {
114: public void keyPressed(KeyEvent evt) {
115: if (evt.isConsumed())
116: return;
117:
118: if (evt.getKeyCode() == KeyEvent.VK_ENTER && enterEnabled) {
119: Component comp = getFocusOwner();
120: while (comp != null) {
121: if (comp instanceof JComboBox) {
122: JComboBox combo = (JComboBox) comp;
123: if (combo.isEditable()) {
124: Object selected = combo.getEditor()
125: .getItem();
126: if (selected != null)
127: combo.setSelectedItem(selected);
128: }
129: break;
130: }
131:
132: comp = comp.getParent();
133: }
134:
135: ok();
136: evt.consume();
137: } else if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
138: cancel();
139: evt.consume();
140: }
141: }
142: }
143:
144: class WindowHandler extends WindowAdapter {
145: public void windowClosing(WindowEvent evt) {
146: cancel();
147: }
148: }
149: }
|