Download this file

PasswordGeneratorDialog.java    162 lines (130 with data), 5.1 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
package cologne.eck.all_peas.gui;
/*
* Peafactory - Production of Password Encryption Archives
* Copyright (C) 2015 Axel von dem Bruch
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
* This library 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.
* See: http://www.gnu.org/licenses/gpl-2.0.html
* You should have received a copy of the GNU General Public License
* along with this library.
*/
/**
* Dialog to create random passwords.
*/
//import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.accessibility.AccessibleContext;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextField;
//import javax.swing.border.LineBorder;
import cologne.eck.all_peas.data.PeaProperties;
import cologne.eck.peafactory.crypto.RandomStuff;
@SuppressWarnings("serial")
public final class PasswordGeneratorDialog extends JDialog
implements ActionListener, WindowListener {
private JTextField randomField;
public PasswordGeneratorDialog (Window owner) {
super(owner);
this.setTitle(PeaProperties.getBundle().getString("create_random_password"));//"create new random password");
this.setAlwaysOnTop(true);
this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
this.addWindowListener(this);
JPanel randomPane = (JPanel) this.getContentPane();
randomPane.setBorder(PeaBorderFactory.createBorder(false));//.setBorder(new LineBorder(Color.GRAY,2));
randomPane.setLayout(new BoxLayout(randomPane, BoxLayout.Y_AXIS));
JPanel displayPanel = new JPanel();
displayPanel.setLayout(new BoxLayout(displayPanel, BoxLayout.Y_AXIS));
randomField = new JTextField();
randomField.setFont(new Font(Font.SERIF, Font.PLAIN, 14));
randomField.setPreferredSize(new Dimension(450, 30));
displayPanel.add(randomField);
JButton randomButton = new JButton(PeaProperties.getBundle().getString("create_password"));//"new password");
randomButton.setActionCommand("createRandomPassword");
randomButton.addActionListener(this);
displayPanel.add(randomButton);
randomPane.add(displayPanel);
JPanel copyPanel = new JPanel();
copyPanel.setLayout(new BoxLayout(copyPanel, BoxLayout.X_AXIS));
JButton copyButton = new JButton(PeaProperties.getBundle().getString("copy"));
copyButton.addActionListener(this);
copyButton.setActionCommand("copy");
copyPanel.add(Box.createHorizontalGlue());
copyPanel.add(copyButton);
randomPane.add(copyPanel);
if (owner != null) {
this.setLocation((int)owner.getLocationOnScreen().getX() + 50,
(int)owner.getLocationOnScreen().getY() + 50);
} else {
this.setLocation(100,100);
}
this.setResizable(false);
this.pack();
}
@Override
public void actionPerformed(ActionEvent ape) {
String command = ape.getActionCommand();
if (command.equals("createRandomPassword")) {
// creates a random password with security properties of at least a 256 bit key: 50 characters
char[] rand = new RandomStuff().createNewRandomChars();
randomField.setText(new String(rand));
} else if (command.equals("copy")) {
StringSelection stringSelection = new StringSelection (randomField.getText());
Clipboard clpbrd = Toolkit.getDefaultToolkit ().getSystemClipboard ();
clpbrd.setContents (stringSelection, null);
randomField.setText("");
}
}
public static void main(String[] args){
PasswordGeneratorDialog p = new PasswordGeneratorDialog(null);
p.setVisible(true);
}
@Override
public void windowActivated(WindowEvent arg0) {}
@Override
public void windowClosed(WindowEvent arg0) {
//System.out.println("closed");
}
@Override
public void windowClosing(WindowEvent arg0) {
// clear the field:
randomField.setText("");
this.dispose();
}
@Override
public void windowDeactivated(WindowEvent arg0) {}
@Override
public void windowDeiconified(WindowEvent arg0) {}
@Override
public void windowIconified(WindowEvent arg0) {}
@Override
public void windowOpened(WindowEvent arg0) {}
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessiblePasswordGeneratorDialog();
}
return accessibleContext;
}
protected class AccessiblePasswordGeneratorDialog extends AccessibleJDialog {
//Inherit everything, override nothing.
}
}