KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > key > passwd > swing > BasicInvalidKeyFeedback


1 /*
2  * Copyright 2006 Schlichtherle IT Services
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package de.schlichtherle.key.passwd.swing;
18
19 import de.schlichtherle.key.KeyProvider;
20
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23
24 import javax.swing.JButton JavaDoc;
25 import javax.swing.JPanel JavaDoc;
26 import javax.swing.JRootPane JavaDoc;
27 import javax.swing.Timer JavaDoc;
28
29 /**
30  * Provides feedback by beeping using the default toolkit and disabling the
31  * default button in the root pane for three seconds
32  * when prompting for a key
33  * and the last input was invalid.
34  * <p>
35  * Note that the root pane is normally the root pane of an enclosing
36  * JOptionPane which has the OK button set as its default button.
37  * This is to inhibit the use of a GUI robot for exhaustive password searching.
38  * <p>
39  * If you would like to play a nice sound for feedback, you need to override
40  * the {@link #startSound} method.
41  * <p>
42  * <b>Warning:</b> Playing a <code>java.applet.AudioClip</code> on J2SE
43  * 1.4.2_12 causes a client application not to terminate until System.exit(0)
44  * is called explicitly - hence this feature is currently not implemented in
45  * this class!
46  * This issue is fixed in JSE 1.5.0_07 (and probably earlier versions).
47  *
48  * @author Christian Schlichtherle
49  * @version @version@
50  * @since TrueZIP 6.4
51  */

52 public abstract class BasicInvalidKeyFeedback extends BasicFeedback {
53
54     private int delay = KeyProvider.MIN_KEY_RETRY_DELAY;
55
56     /**
57      * Returns the delay for reenabling the default button in the root pane.
58      *
59      * @return The delay in milliseconds.
60      * Defaults to {@link KeyProvider#MIN_KEY_RETRY_DELAY}.
61      */

62     public int getDelay() {
63         return delay;
64     }
65
66     /**
67      * Sets the delay for reenabling the default button in the root pane.
68      *
69      * @param delay The delay in milliseconds.
70      */

71     public void setDelay(final int delay) {
72         this.delay = delay;
73     }
74
75     protected void startAnimation() {
76         startAnimation(getPanel(), getDelay());
77     }
78
79     static void startAnimation(final JPanel JavaDoc panel, final int delay) {
80         final JRootPane JavaDoc rp = panel.getRootPane();
81         final JButton JavaDoc b = rp.getDefaultButton();
82         if (b == null)
83             return;
84
85         b.setEnabled(false);
86
87         final Timer JavaDoc t = new Timer JavaDoc(delay, new ActionListener JavaDoc() {
88             public void actionPerformed(ActionEvent JavaDoc e) {
89                 b.setEnabled(true);
90             }
91         });
92         t.setRepeats(false);
93         t.start();
94     }
95 }
96
Popular Tags