summaryrefslogtreecommitdiff
path: root/usr/src/cmd/krb5/kadmin/gui/dchanger/DCPanel.java
blob: b37f1c9dfe084ef9ce3f1b47d808e2679ac1d93b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * ident	"%Z%%M%	%I%	%E% SMI"
 *
 * Copyright (c) 1999-2000 by Sun Microsystems, Inc.
 * All rights reserved.
 */

    import java.awt.*;
    import java.awt.event.*;

    /**
     * Creates a panel with two buttons (+ and - side by side on it). The
     * panel registers a DCListener with it that gets notified whenever
     * these butons are clicked. <bold>The buttons may also be kept continously
     * pressed for faster increments/decrements.</bold>
     * <para>
     * On a single click of the button, the listener is notified to
     * increment/decrement itself by a small amount. When the button is kept
     * pressed the following notifications are sent out for larger
     * increments/decrements. (It is up to the listener to decide the
     * increment/decrement corresponding to large/small.) Moreover, these
     * notifications will be sent out much faster if the button is kept
     * pressed. 
     */

    // The panel waits for a period of BIG_SLEEP_TIME before the faster
    // increments are sent out. They, in turn, are sent out after
    // intervals of SMALL_SLEEP_TIME. Therfore, an instance of this class
    // is associated with 2 timers - a longer one that starts off and then
    // schedules the shorter one. The shorter one keeps scheduling itself
    // every time it wakes up.

    public class DCPanel extends Panel {
  
    private Button plusButton;
    private Button minusButton;
  
    private DCListener listener = null;

    private Timer bigTimer;
    private Timer smallTimer;

    private static int BIG_SLEEP_TIME   = 1000;
    private static int SMALL_SLEEP_TIME = 100;
  
    private boolean incrementFlag;

    public DCPanel() {

    setLayout(new GridLayout(1, 2));
    
    bigTimer     = new BigTimer();
    smallTimer   = new SmallTimer();
    
    bigTimer.start();
    smallTimer.start();
    
    plusButton = new DCButton("+");
    minusButton = new DCButton("-");

    add(plusButton);
    add(minusButton);

    }

    /**
     * Ensures that this component is not brought into focus by
     * tabbing. This prevents the tab focus from moving in here instead
     * of going to a text field.
     * @return false always.
     */
    public boolean isFocusTraversable() {
    return false;
    }

    /**
     * Sets the listener for this tab.
     * @param listener the DCListener that needs to be notified when the
     * buttons on this panel are pressed.
     * @return the old listener
     */
    public DCListener setListener(DCListener listener) {
    DCListener oldListener = this.listener;
    this.listener = listener;
    return oldListener;
    }

    /**
     * Removes the listener when it no longer need to be notified.
     * @return the old listener
     */
    public DCListener removeListener() {
    return setListener(null);
    }

    /**
     * Kicks the times into action. Is called when a button is pressed.
     */
    private void startAction() {
    bigTimer.request();
    }

    /**
     * Stops the timers. Is called when a button is released.
     */
    private void stopAction() {
    smallTimer.cancel();
    bigTimer.cancel();
    }

    /**
     * Notifies the listener about whether to increment or decrement and
     * by how much.
     * @param bigFlag true if the listener needs to increment/decrement
     * by a large amount, false otherwise.
     */
    private void informListener(boolean bigFlag) {
    // System.out.println("DCPanel.informListener: " + bigFlag);

        if (listener != null) {

            if (bigFlag) {
	    // request a big change
	    if (incrementFlag)
	        listener.bigIncrement();
	    else 
	        listener.bigDecrement();
            } else {
	    // request a small change
	    if (incrementFlag)
	        listener.increment();
	    else 
	        listener.decrement();
            }

        }
      
    } // informListener


    // ***********************************************
    // 	 I N N E R    C L A S S E S   F O L L O W
    // ***********************************************

    /**
     * A timer class since java does not have one.
     */
    private abstract class Timer extends Thread {
    private boolean running = false;

    /**
     * Sleeps till the timer's services are requested using wait() and
     * notify(). Then it does its task and goes back to sleep. And
     * loops forever like this.
     */
    public void run() {
        while (true) {
	try {
	  synchronized (this) {
	    running = false;
	    // Wait till the timer is required
	    wait();
	    running = true;
	  }
	  doTask();
	} catch (InterruptedException e) {}
        } // while loop
    } // run method

    protected void doTask() {} // bug in java workshop

    /**
     * Wakes up the timer.
     */
    public synchronized void request() {
        notify();
    }

    /**
     * Cancels the timer if it is running.
     */
    public void cancel() {
        if (running) {
	interrupt();
        }
    }

    }// class Timer

    /**
     * The first stage of timer - is a longer timer. Wait to see if the
     * user really wants to amek the increments/decrements go by fast.
     */
    private class BigTimer extends Timer {

    /**
     * Sleep for the long amount of time. Then inform the listener
     * to have a bigIncrement/bigDecrement. After that, your job is
     * done, schedule the smaller (faster) timer from this point on.
     */
    protected void doTask() {
        try {
	sleep(BIG_SLEEP_TIME);
	informListener(true);
	smallTimer.request();
        } catch (InterruptedException e) {
	informListener(false);
        }
    }

    } // class BigTimer


    /**
     * The second stage of timers. This timer keeps rescheduling itself
     * everytime it wakes up. In between this, it sends a notification
     * to the listener to do a big Increment/Decrement.
     */
    private class SmallTimer extends Timer {

    protected void doTask() {
        try {
	// loop forever and keep rescheduling yourself
	while (true) {
	  sleep(SMALL_SLEEP_TIME);
	  informListener(true);
	    }
        } catch (InterruptedException e) {}
    } // doTask method

    } // class SmallTimer

    /**
     * A mouse listener to detect when a button has been
     * pressed/released. One instance of this is bound to the plus
     * button and the other instance to the minus button.
     */
    private class DCMouseListener extends MouseAdapter {
    private boolean plusOrMinus;

    /**
     * Constructor for DCMouseListener.
     * @param plusOrMinus true if this is a listener for the plus
     *     button, false if it is for the minus button.
     */
    public DCMouseListener(boolean plusOrMinus) {
        this.plusOrMinus = plusOrMinus;
    }

    /**
     * Kicks in when the mouse is pressed.
     */
    public void mousePressed(MouseEvent e) {
        incrementFlag = plusOrMinus;
        DCPanel.this.startAction();
    }

    /**
     * Kicks in when the mouse is released.
     */
    public void mouseReleased(MouseEvent e) {
        incrementFlag = plusOrMinus;
        DCPanel.this.stopAction();
        }
    } 

    /**
     * The button used by this DCPanel.
     */  
    private class DCButton extends Button {
    public DCButton(String text) {
        super(text);
        if (text.equals("+"))
           addMouseListener(new DCMouseListener(true));
        else
        addMouseListener(new DCMouseListener(false));
    }

    /**
     * Make the button non-focus traversable so that it cannot be
     * tabbed in to.
     */
    public boolean isFocusTraversable() {
        return false;
    }

    } // DCButton


    /**
     * Test method for DCPanel class to see appearance.
     */
    public static void main(String args[]) {
    Frame f = new Frame("Testing DCPanel");
    f.add(new DCPanel());
    f.setBounds(new Rectangle(100, 100, 100, 100));
    f.setVisible(true);
    }
  
}