Print this page
4719 Common patchset for jdk1.7 support preparation
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/krb5/kadmin/gui/dchanger/DCPanel.java
+++ new/usr/src/cmd/krb5/kadmin/gui/dchanger/DCPanel.java
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License, Version 1.0 only
6 6 * (the "License"). You may not use this file except in compliance
7 7 * with the License.
8 8 *
9 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 10 * or http://www.opensolaris.org/os/licensing.
11 11 * See the License for the specific language governing permissions
12 12 * and limitations under the License.
13 13 *
14 14 * When distributing Covered Code, include this CDDL HEADER in each
15 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 16 * If applicable, add the following below this CDDL HEADER, with the
17 17 * fields enclosed by brackets "[]" replaced with your own identifying
18 18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 19 *
20 20 * CDDL HEADER END
21 21 */
22 22 /*
23 23 * ident "%Z%%M% %I% %E% SMI"
24 24 *
25 25 * Copyright (c) 1999-2000 by Sun Microsystems, Inc.
26 26 * All rights reserved.
27 27 */
28 28
29 29 import java.awt.*;
30 30 import java.awt.event.*;
31 31
32 32 /**
33 33 * Creates a panel with two buttons (+ and - side by side on it). The
34 34 * panel registers a DCListener with it that gets notified whenever
35 35 * these butons are clicked. <bold>The buttons may also be kept continously
36 36 * pressed for faster increments/decrements.</bold>
37 37 * <para>
38 38 * On a single click of the button, the listener is notified to
39 39 * increment/decrement itself by a small amount. When the button is kept
40 40 * pressed the following notifications are sent out for larger
41 41 * increments/decrements. (It is up to the listener to decide the
42 42 * increment/decrement corresponding to large/small.) Moreover, these
43 43 * notifications will be sent out much faster if the button is kept
44 44 * pressed.
45 45 */
46 46
47 47 // The panel waits for a period of BIG_SLEEP_TIME before the faster
48 48 // increments are sent out. They, in turn, are sent out after
49 49 // intervals of SMALL_SLEEP_TIME. Therfore, an instance of this class
50 50 // is associated with 2 timers - a longer one that starts off and then
51 51 // schedules the shorter one. The shorter one keeps scheduling itself
52 52 // every time it wakes up.
53 53
54 54 public class DCPanel extends Panel {
55 55
56 56 private Button plusButton;
57 57 private Button minusButton;
58 58
59 59 private DCListener listener = null;
60 60
61 61 private Timer bigTimer;
62 62 private Timer smallTimer;
63 63
64 64 private static int BIG_SLEEP_TIME = 1000;
65 65 private static int SMALL_SLEEP_TIME = 100;
66 66
67 67 private boolean incrementFlag;
68 68
69 69 public DCPanel() {
70 70
71 71 setLayout(new GridLayout(1, 2));
72 72
73 73 bigTimer = new BigTimer();
74 74 smallTimer = new SmallTimer();
75 75
76 76 bigTimer.start();
77 77 smallTimer.start();
78 78
79 79 plusButton = new DCButton("+");
80 80 minusButton = new DCButton("-");
81 81
82 82 add(plusButton);
↓ open down ↓ |
82 lines elided |
↑ open up ↑ |
83 83 add(minusButton);
84 84
85 85 }
86 86
87 87 /**
88 88 * Ensures that this component is not brought into focus by
89 89 * tabbing. This prevents the tab focus from moving in here instead
90 90 * of going to a text field.
91 91 * @return false always.
92 92 */
93 - public boolean isFocusTraversable() {
93 + public boolean isFocusable() {
94 94 return false;
95 95 }
96 96
97 97 /**
98 98 * Sets the listener for this tab.
99 99 * @param listener the DCListener that needs to be notified when the
100 100 * buttons on this panel are pressed.
101 101 * @return the old listener
102 102 */
103 103 public DCListener setListener(DCListener listener) {
104 104 DCListener oldListener = this.listener;
105 105 this.listener = listener;
106 106 return oldListener;
107 107 }
108 108
109 109 /**
110 110 * Removes the listener when it no longer need to be notified.
111 111 * @return the old listener
112 112 */
113 113 public DCListener removeListener() {
114 114 return setListener(null);
115 115 }
116 116
117 117 /**
118 118 * Kicks the times into action. Is called when a button is pressed.
119 119 */
120 120 private void startAction() {
121 121 bigTimer.request();
122 122 }
123 123
124 124 /**
125 125 * Stops the timers. Is called when a button is released.
126 126 */
127 127 private void stopAction() {
128 128 smallTimer.cancel();
129 129 bigTimer.cancel();
130 130 }
131 131
132 132 /**
133 133 * Notifies the listener about whether to increment or decrement and
134 134 * by how much.
135 135 * @param bigFlag true if the listener needs to increment/decrement
136 136 * by a large amount, false otherwise.
137 137 */
138 138 private void informListener(boolean bigFlag) {
139 139 // System.out.println("DCPanel.informListener: " + bigFlag);
140 140
141 141 if (listener != null) {
142 142
143 143 if (bigFlag) {
144 144 // request a big change
145 145 if (incrementFlag)
146 146 listener.bigIncrement();
147 147 else
148 148 listener.bigDecrement();
149 149 } else {
150 150 // request a small change
151 151 if (incrementFlag)
152 152 listener.increment();
153 153 else
154 154 listener.decrement();
155 155 }
156 156
157 157 }
158 158
159 159 } // informListener
160 160
161 161
162 162 // ***********************************************
163 163 // I N N E R C L A S S E S F O L L O W
164 164 // ***********************************************
165 165
166 166 /**
167 167 * A timer class since java does not have one.
168 168 */
169 169 private abstract class Timer extends Thread {
170 170 private boolean running = false;
171 171
172 172 /**
173 173 * Sleeps till the timer's services are requested using wait() and
174 174 * notify(). Then it does its task and goes back to sleep. And
175 175 * loops forever like this.
176 176 */
177 177 public void run() {
178 178 while (true) {
179 179 try {
180 180 synchronized (this) {
181 181 running = false;
182 182 // Wait till the timer is required
183 183 wait();
184 184 running = true;
185 185 }
186 186 doTask();
187 187 } catch (InterruptedException e) {}
188 188 } // while loop
189 189 } // run method
190 190
191 191 protected void doTask() {} // bug in java workshop
192 192
193 193 /**
194 194 * Wakes up the timer.
195 195 */
196 196 public synchronized void request() {
197 197 notify();
198 198 }
199 199
200 200 /**
201 201 * Cancels the timer if it is running.
202 202 */
203 203 public void cancel() {
204 204 if (running) {
205 205 interrupt();
206 206 }
207 207 }
208 208
209 209 }// class Timer
210 210
211 211 /**
212 212 * The first stage of timer - is a longer timer. Wait to see if the
213 213 * user really wants to amek the increments/decrements go by fast.
214 214 */
215 215 private class BigTimer extends Timer {
216 216
217 217 /**
218 218 * Sleep for the long amount of time. Then inform the listener
219 219 * to have a bigIncrement/bigDecrement. After that, your job is
220 220 * done, schedule the smaller (faster) timer from this point on.
221 221 */
222 222 protected void doTask() {
223 223 try {
224 224 sleep(BIG_SLEEP_TIME);
225 225 informListener(true);
226 226 smallTimer.request();
227 227 } catch (InterruptedException e) {
228 228 informListener(false);
229 229 }
230 230 }
231 231
232 232 } // class BigTimer
233 233
234 234
235 235 /**
236 236 * The second stage of timers. This timer keeps rescheduling itself
237 237 * everytime it wakes up. In between this, it sends a notification
238 238 * to the listener to do a big Increment/Decrement.
239 239 */
240 240 private class SmallTimer extends Timer {
241 241
242 242 protected void doTask() {
243 243 try {
244 244 // loop forever and keep rescheduling yourself
245 245 while (true) {
246 246 sleep(SMALL_SLEEP_TIME);
247 247 informListener(true);
248 248 }
249 249 } catch (InterruptedException e) {}
250 250 } // doTask method
251 251
252 252 } // class SmallTimer
253 253
254 254 /**
255 255 * A mouse listener to detect when a button has been
256 256 * pressed/released. One instance of this is bound to the plus
257 257 * button and the other instance to the minus button.
258 258 */
259 259 private class DCMouseListener extends MouseAdapter {
260 260 private boolean plusOrMinus;
261 261
262 262 /**
263 263 * Constructor for DCMouseListener.
264 264 * @param plusOrMinus true if this is a listener for the plus
265 265 * button, false if it is for the minus button.
266 266 */
267 267 public DCMouseListener(boolean plusOrMinus) {
268 268 this.plusOrMinus = plusOrMinus;
269 269 }
270 270
271 271 /**
272 272 * Kicks in when the mouse is pressed.
273 273 */
274 274 public void mousePressed(MouseEvent e) {
275 275 incrementFlag = plusOrMinus;
276 276 DCPanel.this.startAction();
277 277 }
278 278
279 279 /**
280 280 * Kicks in when the mouse is released.
281 281 */
282 282 public void mouseReleased(MouseEvent e) {
283 283 incrementFlag = plusOrMinus;
284 284 DCPanel.this.stopAction();
285 285 }
286 286 }
287 287
288 288 /**
289 289 * The button used by this DCPanel.
290 290 */
291 291 private class DCButton extends Button {
292 292 public DCButton(String text) {
293 293 super(text);
↓ open down ↓ |
190 lines elided |
↑ open up ↑ |
294 294 if (text.equals("+"))
295 295 addMouseListener(new DCMouseListener(true));
296 296 else
297 297 addMouseListener(new DCMouseListener(false));
298 298 }
299 299
300 300 /**
301 301 * Make the button non-focus traversable so that it cannot be
302 302 * tabbed in to.
303 303 */
304 - public boolean isFocusTraversable() {
304 + public boolean isFocusable() {
305 305 return false;
306 306 }
307 307
308 308 } // DCButton
309 309
310 310
311 311 /**
312 312 * Test method for DCPanel class to see appearance.
313 313 */
314 314 public static void main(String args[]) {
315 315 Frame f = new Frame("Testing DCPanel");
316 316 f.add(new DCPanel());
317 317 f.setBounds(new Rectangle(100, 100, 100, 100));
318 318 f.setVisible(true);
319 319 }
320 320
321 321 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX