source: trunk/src/org/expeditee/items/widgets/Password.java@ 1102

Last change on this file since 1102 was 1102, checked in by davidb, 6 years ago

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

File size: 2.3 KB
Line 
1/**
2 * Password.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.items.widgets;
20
21import java.awt.GridLayout;
22
23import javax.swing.JCheckBox;
24import javax.swing.JPanel;
25import javax.swing.JPasswordField;
26
27import org.expeditee.items.Text;
28
29/**
30 * Allows entering password data in non-visible form
31 * TODO: encrypt this before storing it
32 * @author jts21
33 */
34public class Password extends SwingWidget {
35
36 private JPasswordField passwordField;
37 private JCheckBox savePassword;
38
39 public Password(Text source, String[] args) {
40 super(source, new JPanel(new GridLayout(0, 1)), 200, 200, 60, 60);
41 JPanel p = (JPanel) super._swingComponent;
42 this.passwordField = new JPasswordField();
43 this.savePassword = new JCheckBox("Save password?");
44 p.add(passwordField);
45 p.add(savePassword);
46 if(args != null && args.length > 0) {
47 String[] password = args[0].split(":");
48 StringBuilder s = new StringBuilder();
49 for(String str : password) {
50 char c = (char) Byte.parseByte(str);
51 s.append(c);
52 }
53 this.passwordField.setText(s.toString());
54 this.savePassword.setSelected(true);
55 }
56 }
57
58 @Override
59 protected String[] getArgs() {
60 if(!savePassword.isSelected()) {
61 return null;
62 }
63 char[] password = this.passwordField.getPassword();
64 StringBuilder s = new StringBuilder();
65 for(char b : password) {
66 s.append((byte)b + ":");
67 }
68 return new String[] { s.toString() };
69 }
70
71 public String getPassword() {
72 return new String(this.passwordField.getPassword());
73 }
74
75 public void setPassword(String password) {
76 this.passwordField.setText(password);
77 }
78
79}
Note: See TracBrowser for help on using the repository browser.