1
2
3
4
5 package org.astrogrid.ui;
6
7 import java.awt.Window;
8 import java.awt.Component;
9 import java.awt.Container;
10 import java.awt.Font;
11 import java.awt.event.*;
12 import javax.swing.JButton;
13
14 /***
15 * This is an adapter for catching esc & enter keypresses in, eg, a
16 * dialog, so that it calls the default button or cancel button.
17 * Further catches can be made by subclassing. Also ties windows
18 * close button to cancel button. Should be created and registered
19 * after the dialog and its child components have been created and
20 * assembled, so that it can add itself ot all the subcomponents.
21 * <P>
22 * To use, add the following line <B>after</B> the components have been assembled:
23 * <pre>
24 * new EscEnterListener(dialog box, default button, cancel button)
25 * </pre>
26 * It works by registering itself as a keypress listener for all the child
27 * components of the dialog box, trapping all [Esc] or [Enter] keypresses.
28 * <p>
29 * Scope for improvement: If
30 * changes are made to the componen tree after constructing the instance as
31 * above, keypresses in the new components will not be trapped. This class
32 * could also register itself as a ComponentListener to track such changes if
33 * necessary. At the moment the method RegisterThis(root container) can be
34 * used 'manually' to register it with new components.
35 *
36 * @author M Hill
37 */
38
39
40
41
42
43 public class EscEnterListener extends KeyAdapter
44 {
45 JButton defaultBtn = null;
46 JButton cancelBtn = null;
47 Window window = null;
48
49 /*** Standard constructor - close presses the cancel button but the window itself
50 * must listen out for the button events
51 */
52 public EscEnterListener(Window aWindow, JButton aDefault, JButton aCancel)
53 {
54 this(aWindow, aDefault, aCancel, false);
55 }
56
57 /***
58 * Constructor providing a disposeOnCancel argument which will close &
59 * dispose the display if cancel (or, therefore, the close icon) is
60 * pressed
61 */
62 public EscEnterListener(Window aWindow, JButton aDefault, JButton aCancel, boolean disposeOnCancel)
63 {
64 window = aWindow;
65 setDefaultBtn(aDefault);
66 setCancelBtn(aCancel);
67 registerWith(window);
68
69
70
71 window.addWindowListener(
72 new WindowAdapter()
73 {
74 public void windowClosing(WindowEvent e)
75 {
76 if (cancelBtn == null)
77 {
78
79 window.setVisible(false);
80 }
81 else if (cancelBtn.isEnabled())
82 {
83 cancelBtn.doClick();
84 }
85 }
86 }
87 );
88
89 if ((disposeOnCancel) && (cancelBtn != null))
90 {
91
92 cancelBtn.addActionListener(
93 new ActionListener()
94 {
95 public void actionPerformed(ActionEvent e)
96 {
97 window.hide();
98 }
99 }
100 );
101 }
102 }
103
104 /*** Sets the button that will close the dialog box when [Esc] is pressed
105 */
106 public void setCancelBtn(JButton b)
107 {
108 cancelBtn = b;
109 }
110
111 /*** Sets the default button. Also sets the button's font to bold
112 * to show which one is default. This button will be activated
113 * by the [Enter] key. */
114 public void setDefaultBtn(JButton b)
115 {
116 defaultBtn = b;
117 if (defaultBtn != null)
118 defaultBtn.setFont(defaultBtn.getFont().deriveFont(Font.BOLD));
119 }
120
121 /***
122 * Registers this listener with all the components in the tree given
123 * by comp. Call this with the root dialog frame and all components
124 * will be recursively called down the container tree. It does not
125 * (yet :-) handle later changes to component trees - a subclass could
126 * register as a container listener too.. */
127 public void registerWith(Component comp)
128 {
129
130 comp.removeKeyListener(this);
131
132
133 comp.addKeyListener(this);
134 if (comp instanceof Container)
135 {
136 Component[] children = ((Container) comp).getComponents();
137 for (int i=0;i<children.length;i++)
138 {
139 registerWith(children[i]);
140 }
141 }
142 }
143
144 /*** Check for ESC or ENTER being pressed. If so, and the relevent
145 * button is enabled, click it */
146 public void keyPressed(KeyEvent ke)
147 {
148 if ((ke.getKeyCode() == ke.VK_ENTER) && (defaultBtn != null) && (defaultBtn.isEnabled()))
149 {
150 defaultBtn.doClick();
151 }
152 if ((ke.getKeyCode() == ke.VK_ESCAPE) && (cancelBtn != null) && (cancelBtn.isEnabled()))
153 {
154 cancelBtn.doClick();
155 }
156 if ((ke.getKeyCode() == ke.VK_ESCAPE) && (cancelBtn == null))
157 {
158 window.hide();
159 }
160 }
161
162 }
163
164 /***
165 $Log: EscEnterListener.java,v $
166 Revision 1.4 2005/03/29 16:26:45 mch
167 Fix in case of null cancel button
168
169 Revision 1.3 2004/04/15 16:34:53 mch
170 Tidied up, introduced stuff from datacenter ui
171
172 Revision 1.1 2004/03/03 17:40:58 mch
173 Moved ui package
174
175 Revision 1.1 2004/02/17 16:04:06 mch
176 New Desktop GUI
177
178 Revision 1.1.1.1 2003/08/25 18:36:32 mch
179 Reimported to fit It02 source structure
180
181 Revision 1.3 2003/07/02 19:15:14 mch
182 Removed windows/linux double spacing
183
184 */
185