KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
20
21 import java.awt.*;
22 import java.lang.ref.*;
23
24 import javax.swing.*;
25
26 /**
27  * A key manager which enables users to enter passwords or select key files
28  * as keys using a Swing GUI.
29  * This key manager is used by default unless the JVM is running in headless
30  * mode!
31  * <p>
32  * If a password is entered, then the run time type of the key is a char array,
33  * holding each password character.
34  * If a key file is selected, the file size must be 512 bytes or more, of
35  * which only the first 512 bytes are used as a byte array.
36  * <p>
37  * If this JVM is run in headless mode, all prompting is disabled.
38  * <p>
39  * Note that class loading and instantiation may happen in a JVM shutdown hook,
40  * so class initializers and constructors must behave accordingly.
41  * In particular, it's not permitted to construct or use a Swing GUI there.
42  * <p>
43  * This class is thread safe.
44  *
45  * @author Christian Schlichtherle
46  * @version @version@
47  * @since TrueZIP 6.0
48  */

49 public class PromptingKeyManager
50         extends de.schlichtherle.key.PromptingKeyManager {
51
52     /**
53      * Constructs a new <code>PromptingKeyManager</code>.
54      * This instance maps the following key provider UI types using
55      * {@link de.schlichtherle.key.PromptingKeyManager#mapPromptingKeyProviderUIType}:
56      * <table border="2" cellpadding="4">
57      * <tr>
58      * <th>uiClassID</th>
59      * <th>uiClass</th>
60      * </tr>
61      * <tr>
62      * <td>"PromptingKeyProvider"</td>
63      * <td>PromptingKeyProviderUI.class</td>
64      * </tr>
65      * <tr>
66      * <td>"PromptingAesKeyProvider"</td>
67      * <td>PromptingAesKeyProviderUI.class</td>
68      * </tr>
69      * </table>
70      */

71     public PromptingKeyManager() {
72         mapPromptingKeyProviderUIType(
73                 "PromptingKeyProvider",
74                 PromptingKeyProviderUI.class);
75         mapPromptingKeyProviderUIType(
76                 "PromptingAesKeyProvider",
77                 PromptingAesKeyProviderUI.class);
78     }
79
80     /**
81      * Returns a window which is suitable to be the parent of the dialog
82      * used to prompt the user for a key.
83      * If no parent window has been set explicitly, then the last focused
84      * window is used.
85      * If no window received the focus yet or is already made eligible for
86      * finalization, then any showing window is used.
87      * <p>
88      * In all cases, the first showing parent window which is found by
89      * searching the containment hierarchy upwards is preferrably returned.
90      * <p>
91      * As a last resort, if no window is showing, then {@link JOptionPane}'s
92      * root frame is used.
93      *
94      * @see #setParentWindow(Window)
95      */

96     public static Window getParentWindow() {
97         return WindowUtils.getParentWindow();
98     }
99
100     /**
101      * Sets the parent window of the dialog used for prompting the user for
102      * a key.
103      * The window is stored in a weak reference in order to allow it to get
104      * garbage collected if no thread is holding a strong reference to it
105      * from a root object.
106      *
107      * @param w The parent window to use for key prompting or
108      * <code>null</code> if a default window shall be used.
109      * @see #getParentWindow()
110      * @deprecated You shouldn't use this method any more, but rely on the
111      * implementation in <code>getParentWindow()</code>.
112      */

113     public static void setParentWindow(final Window w) {
114         WindowUtils.setParentWindow(w);
115     }
116
117     //
118
// Instance stuff:
119
//
120

121     protected boolean isPromptingImpl() {
122         return super.isPromptingImpl() && !GraphicsEnvironment.isHeadless();
123     }
124
125     /**
126      * If this JVM is running in headless mode, then this method throws
127      * a {@link KeyPromptingDisabledException} with a {@link HeadlessException}
128      * as its cause.
129      */

130     protected void ensurePromptingImpl()
131     throws KeyPromptingDisabledException {
132         if (GraphicsEnvironment.isHeadless())
133             throw new KeyPromptingDisabledException(new HeadlessException());
134         super.ensurePromptingImpl();
135     }
136 }
137
Popular Tags