-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathPassword.java
More file actions
196 lines (184 loc) · 7.03 KB
/
Password.java
File metadata and controls
196 lines (184 loc) · 7.03 KB
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
/*
* Open Source Physics software is free software as described near the bottom of this code file.
*
* For additional information and documentation on Open Source Physics please see:
* <http://www.opensourcephysics.org/>
*/
package org.opensourcephysics.controls;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
/**
* A dialog for verifying passwords with a single public static method verify().
*
* @author Douglas Brown
* @version 1.0
*/
public class Password extends JDialog {
// instance fields
private JLabel messageLabel;
private JPasswordField passwordField;
private String password;
private boolean pass;
/**
* Shows a dialog and verifies user entry of the password.
*
* @param password the password
* @param filename the name of the password-protected file (may be null).
* @return true if password is null, "", or correctly verified
*/
public static boolean verify(String password, String fileName) {
if((password==null)||password.equals("")) {//$NON-NLS-1$
return true;
}
Password dialog = new Password();
dialog.password = password;
if((fileName==null)||fileName.equals("")) { //$NON-NLS-1$
dialog.messageLabel.setText(ControlsRes.getString("Password.Message.Short")); //$NON-NLS-1$
} else {
dialog.messageLabel.setText(ControlsRes.getString("Password.Message.File") //$NON-NLS-1$
+" \""+XML.getName(fileName)+"\"."); //$NON-NLS-1$ //$NON-NLS-2$
}
dialog.pack();
// center on screen
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int x = (dim.width-dialog.getBounds().width)/2;
int y = (dim.height-dialog.getBounds().height)/2;
dialog.setLocation(x, y);
dialog.pass = false;
dialog.passwordField.setText(""); //$NON-NLS-1$
dialog.setVisible(true);
dialog.dispose();
return dialog.pass;
}
/**
* Private constructor.
*/
private Password() {
super((Frame) null, true); // modal with no owner
setTitle(ControlsRes.getString("Password.Title")); //$NON-NLS-1$
createGUI();
setResizable(false);
passwordField.requestFocusInWindow();
}
/**
* Creates the visible components.
*/
private void createGUI() {
// create input panel
GridBagLayout gridbag = new GridBagLayout();
JPanel inputPanel = new JPanel(gridbag);
// create components
messageLabel = new JLabel();
JLabel fieldLabel = new JLabel(ControlsRes.getString("Password.Label")); //$NON-NLS-1$
passwordField = new JPasswordField(20);
passwordField.setToolTipText(ControlsRes.getString("Password.Tooltip")); //$NON-NLS-1$
passwordField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String input = String.copyValueOf(passwordField.getPassword());
if((password!=null)&&!input.equals(password)) {
Toolkit.getDefaultToolkit().beep();
passwordField.setText(""); //$NON-NLS-1$
} else {
pass = true;
setVisible(false);
}
}
});
passwordField.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ESCAPE) {
passwordField.requestFocusInWindow();
setVisible(false);
}
}
});
// create buttons
JButton cancelButton = new JButton(ControlsRes.getString("Password.Button.Cancel")); //$NON-NLS-1$
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
passwordField.requestFocusInWindow();
setVisible(false);
}
});
JButton okButton = new JButton(ControlsRes.getString("Password.Button.Enter")); //$NON-NLS-1$
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String input = String.copyValueOf(passwordField.getPassword());
if((password!=null)&&!input.equals(password)) {
Toolkit.getDefaultToolkit().beep();
passwordField.setText(""); //$NON-NLS-1$
passwordField.requestFocusInWindow();
} else {
pass = true;
setVisible(false);
}
}
});
// input panel in center
Container contentPane = this.getContentPane();
contentPane.add(inputPanel, BorderLayout.CENTER);
GridBagConstraints c = new GridBagConstraints();
// add message to input panel
c.insets = new Insets(20, 15, 10, 15);
gridbag.setConstraints(messageLabel, c);
inputPanel.add(messageLabel);
// add label and password field to input panel
JPanel entry = new JPanel();
entry.add(fieldLabel);
entry.add(passwordField);
c.gridy = 1;
c.insets = new Insets(0, 10, 10, 10);
gridbag.setConstraints(entry, c);
inputPanel.add(entry);
// button pane at bottom
JPanel buttonPane = new JPanel();
contentPane.add(buttonPane, BorderLayout.SOUTH);
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 4, 4));
buttonPane.add(Box.createHorizontalGlue());
buttonPane.add(okButton);
buttonPane.add(Box.createRigidArea(new Dimension(4, 0)));
buttonPane.add(cancelButton);
}
}
/*
* Open Source Physics software is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License (GPL) as
* published by the Free Software Foundation; either version 2 of the License,
* or(at your option) any later version.
*
* Code that uses any portion of the code in the org.opensourcephysics package
* or any subpackage (subdirectory) of this package must must also be be
* released under the GNU GPL license.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
* Suite 330, Boston MA 02111-1307 USA or view the license online at
* http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2007 The Open Source Physics project
* http://www.opensourcephysics.org
*/