/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/gui/EnhancedDialog.java
Java | 160 lines | 109 code | 20 blank | 31 comment | 15 complexity | 2705fb76ce15f0ec1df090017d0fe65e MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
1/*
2 * EnhancedDialog.java - Handles OK/Cancel for you
3 * Copyright (C) 1998, 1999, 2001 Slava Pestov
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20package org.gjt.sp.jedit.gui;
21
22import javax.swing.*;
23import java.awt.event.*;
24import java.awt.*;
25
26/**
27 * A dialog box that handles window closing, the ENTER key and the ESCAPE
28 * key for you. All you have to do is implement ok() (called when
29 * Enter is pressed) and cancel() (called when Escape is pressed, or window
30 * is closed).
31 * @author Slava Pestov
32 * @version $Id: EnhancedDialog.java 4352 2002-10-07 21:13:20Z spestov $
33 */
34public abstract class EnhancedDialog extends JDialog
35{
36 public EnhancedDialog(Frame parent, String title, boolean modal)
37 {
38 super(parent,title,modal);
39 _init();
40 }
41
42 public EnhancedDialog(Dialog parent, String title, boolean modal)
43 {
44 super(parent,title,modal);
45 _init();
46 }
47
48 public abstract void ok();
49 public abstract void cancel();
50
51 //{{{ Private members
52
53 private void _init() {
54 ((Container)getLayeredPane()).addContainerListener(
55 new ContainerHandler());
56 getContentPane().addContainerListener(new ContainerHandler());
57
58 keyHandler = new KeyHandler();
59 addKeyListener(keyHandler);
60 addWindowListener(new WindowHandler());
61
62 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
63 }
64
65 //}}}
66
67 // protected members
68 protected KeyHandler keyHandler;
69
70 // Recursively adds our key listener to sub-components
71 class ContainerHandler extends ContainerAdapter
72 {
73 public void componentAdded(ContainerEvent evt)
74 {
75 componentAdded(evt.getChild());
76 }
77
78 public void componentRemoved(ContainerEvent evt)
79 {
80 componentRemoved(evt.getChild());
81 }
82
83 private void componentAdded(Component comp)
84 {
85 comp.addKeyListener(keyHandler);
86 if(comp instanceof Container)
87 {
88 Container cont = (Container)comp;
89 cont.addContainerListener(this);
90 Component[] comps = cont.getComponents();
91 for(int i = 0; i < comps.length; i++)
92 {
93 componentAdded(comps[i]);
94 }
95 }
96 }
97
98 private void componentRemoved(Component comp)
99 {
100 comp.removeKeyListener(keyHandler);
101 if(comp instanceof Container)
102 {
103 Container cont = (Container)comp;
104 cont.removeContainerListener(this);
105 Component[] comps = cont.getComponents();
106 for(int i = 0; i < comps.length; i++)
107 {
108 componentRemoved(comps[i]);
109 }
110 }
111 }
112 }
113
114 class KeyHandler extends KeyAdapter
115 {
116 public void keyPressed(KeyEvent evt)
117 {
118 if(evt.isConsumed())
119 return;
120
121 if(evt.getKeyCode() == KeyEvent.VK_ENTER)
122 {
123 // crusty workaround
124 Component comp = getFocusOwner();
125 while(comp != null)
126 {
127 if(comp instanceof JComboBox)
128 {
129 JComboBox combo = (JComboBox)comp;
130 if(combo.isEditable())
131 {
132 Object selected = combo.getEditor().getItem();
133 if(selected != null)
134 combo.setSelectedItem(selected);
135 }
136 break;
137 }
138
139 comp = comp.getParent();
140 }
141
142 ok();
143 evt.consume();
144 }
145 else if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
146 {
147 cancel();
148 evt.consume();
149 }
150 }
151 }
152
153 class WindowHandler extends WindowAdapter
154 {
155 public void windowClosing(WindowEvent evt)
156 {
157 cancel();
158 }
159 }
160}