Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2009 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* If you wish your version of this file to be governed by only the CDDL |
25 |
* or only the GPL Version 2, indicate your decision by adding |
26 |
* "[Contributor] elects to include this software in this distribution |
27 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
28 |
* single choice of license, a recipient has the option to distribute |
29 |
* your version of this file under either the CDDL, the GPL Version 2 or |
30 |
* to extend the choice of license to its licensees as provided above. |
31 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
32 |
* Version 2 license, then the option applies only if the new code is |
33 |
* made subject to such option by the copyright holder. |
34 |
* |
35 |
* Contributor(s): |
36 |
* |
37 |
* Portions Copyrighted 2009 Sun Microsystems, Inc. |
38 |
*/ |
39 |
|
40 |
package org.netbeans.modules.keyring.fallback; |
41 |
|
42 |
import java.awt.event.ActionEvent; |
43 |
import java.awt.event.ActionListener; |
44 |
import java.util.Arrays; |
45 |
import javax.swing.JButton; |
46 |
import javax.swing.JPanel; |
47 |
import javax.swing.event.DocumentEvent; |
48 |
import javax.swing.event.DocumentListener; |
49 |
import org.openide.DialogDisplayer; |
50 |
import org.openide.NotificationLineSupport; |
51 |
import org.openide.NotifyDescriptor; |
52 |
|
53 |
class MasterPasswordPanel extends JPanel { |
54 |
|
55 |
public MasterPasswordPanel() { |
56 |
initComponents(); |
57 |
} |
58 |
|
59 |
/** |
60 |
* Shows this dialog. |
61 |
* @return master password, and if selected, new master password; or null if cancelled |
62 |
*/ |
63 |
public char[][] display() { |
64 |
// XXX I18N |
65 |
final JButton ok = new JButton("OK"); |
66 |
ok.setDefaultCapable(true); |
67 |
NotifyDescriptor d = new NotifyDescriptor(this, "Enter Master Password", NotifyDescriptor.OK_CANCEL_OPTION, NotifyDescriptor.PLAIN_MESSAGE, |
68 |
new Object[] {ok, NotifyDescriptor.CANCEL_OPTION}, ok); |
69 |
final NotificationLineSupport notification = d.createNotificationLineSupport(); |
70 |
final Runnable update = new Runnable() { |
71 |
public void run() { |
72 |
if (masterPasswordField.getPassword().length == 0) { |
73 |
notification.setInformationMessage("Enter password"); |
74 |
ok.setEnabled(false); |
75 |
return; |
76 |
} |
77 |
boolean changing = setNewBox.isSelected(); |
78 |
newLabel1.setEnabled(changing); |
79 |
newField1.setEnabled(changing); |
80 |
newLabel2.setEnabled(changing); |
81 |
newField2.setEnabled(changing); |
82 |
if (changing) { |
83 |
if (newField1.getPassword().length == 0) { |
84 |
notification.setInformationMessage("Enter new password"); |
85 |
ok.setEnabled(false); |
86 |
return; |
87 |
} |
88 |
if (!Arrays.equals(newField1.getPassword(), newField2.getPassword())) { |
89 |
notification.setInformationMessage("New passwords do not match"); |
90 |
ok.setEnabled(false); |
91 |
return; |
92 |
} |
93 |
} |
94 |
notification.clearMessages(); |
95 |
ok.setEnabled(true); |
96 |
} |
97 |
}; |
98 |
DocumentListener listener = new DocumentListener() { |
99 |
public void insertUpdate(DocumentEvent e) { |
100 |
update.run(); |
101 |
} |
102 |
public void removeUpdate(DocumentEvent e) { |
103 |
update.run(); |
104 |
} |
105 |
public void changedUpdate(DocumentEvent e) {} |
106 |
}; |
107 |
update.run(); |
108 |
masterPasswordField.getDocument().addDocumentListener(listener); |
109 |
newField1.getDocument().addDocumentListener(listener); |
110 |
newField2.getDocument().addDocumentListener(listener); |
111 |
setNewBox.addActionListener(new ActionListener() { |
112 |
public void actionPerformed(ActionEvent e) { |
113 |
update.run(); |
114 |
} |
115 |
}); |
116 |
if (DialogDisplayer.getDefault().notify(d) != ok) { |
117 |
return null; |
118 |
} |
119 |
char[] masterPassword = masterPasswordField.getPassword(); |
120 |
return setNewBox.isSelected() ? new char[][] {masterPassword, newField1.getPassword()} : new char[][] {masterPassword}; |
121 |
} |
122 |
|
123 |
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents |
124 |
private void initComponents() { |
125 |
|
126 |
masterPasswordLabel = new javax.swing.JLabel(); |
127 |
masterPasswordField = new javax.swing.JPasswordField(); |
128 |
setNewBox = new javax.swing.JCheckBox(); |
129 |
newLabel1 = new javax.swing.JLabel(); |
130 |
newField1 = new javax.swing.JPasswordField(); |
131 |
newLabel2 = new javax.swing.JLabel(); |
132 |
newField2 = new javax.swing.JPasswordField(); |
133 |
|
134 |
masterPasswordLabel.setLabelFor(masterPasswordField); |
135 |
org.openide.awt.Mnemonics.setLocalizedText(masterPasswordLabel, org.openide.util.NbBundle.getMessage(MasterPasswordPanel.class, "MasterPasswordPanel.masterPasswordLabel.text")); // NOI18N |
136 |
|
137 |
org.openide.awt.Mnemonics.setLocalizedText(setNewBox, org.openide.util.NbBundle.getMessage(MasterPasswordPanel.class, "MasterPasswordPanel.setNewBox.text")); // NOI18N |
138 |
|
139 |
newLabel1.setLabelFor(newField1); |
140 |
org.openide.awt.Mnemonics.setLocalizedText(newLabel1, org.openide.util.NbBundle.getMessage(MasterPasswordPanel.class, "MasterPasswordPanel.newLabel1.text")); // NOI18N |
141 |
newLabel1.setEnabled(false); |
142 |
|
143 |
newField1.setEnabled(false); |
144 |
|
145 |
newLabel2.setLabelFor(newField2); |
146 |
org.openide.awt.Mnemonics.setLocalizedText(newLabel2, org.openide.util.NbBundle.getMessage(MasterPasswordPanel.class, "MasterPasswordPanel.newLabel2.text")); // NOI18N |
147 |
newLabel2.setEnabled(false); |
148 |
|
149 |
newField2.setEnabled(false); |
150 |
|
151 |
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); |
152 |
this.setLayout(layout); |
153 |
layout.setHorizontalGroup( |
154 |
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) |
155 |
.add(layout.createSequentialGroup() |
156 |
.addContainerGap() |
157 |
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) |
158 |
.add(layout.createSequentialGroup() |
159 |
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) |
160 |
.add(newLabel1) |
161 |
.add(newLabel2) |
162 |
.add(masterPasswordLabel)) |
163 |
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) |
164 |
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) |
165 |
.add(masterPasswordField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE) |
166 |
.add(newField2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE) |
167 |
.add(newField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE))) |
168 |
.add(setNewBox)) |
169 |
.addContainerGap()) |
170 |
); |
171 |
layout.setVerticalGroup( |
172 |
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) |
173 |
.add(layout.createSequentialGroup() |
174 |
.addContainerGap() |
175 |
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) |
176 |
.add(masterPasswordLabel) |
177 |
.add(masterPasswordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) |
178 |
.add(18, 18, 18) |
179 |
.add(setNewBox) |
180 |
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) |
181 |
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) |
182 |
.add(newLabel1) |
183 |
.add(newField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) |
184 |
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) |
185 |
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) |
186 |
.add(newLabel2) |
187 |
.add(newField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) |
188 |
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) |
189 |
); |
190 |
}// </editor-fold>//GEN-END:initComponents |
191 |
|
192 |
// Variables declaration - do not modify//GEN-BEGIN:variables |
193 |
private javax.swing.JPasswordField masterPasswordField; |
194 |
private javax.swing.JLabel masterPasswordLabel; |
195 |
private javax.swing.JPasswordField newField1; |
196 |
private javax.swing.JPasswordField newField2; |
197 |
private javax.swing.JLabel newLabel1; |
198 |
private javax.swing.JLabel newLabel2; |
199 |
private javax.swing.JCheckBox setNewBox; |
200 |
// End of variables declaration//GEN-END:variables |
201 |
|
202 |
} |