001: /*
002: * Copyright (C) 2003-2007 Stephen Ostermiller
003: * http://ostermiller.org/contact.pl?regarding=Random+Password+Generator+Applet
004: *
005: * This program is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * See COPYING.TXT for details.
016: */
017:
018: package com.Ostermiller.util;
019:
020: import javax.swing.*;
021: import java.awt.*;
022: import java.awt.event.*;
023:
024: /**
025: * An applet that will let the user generate random passwords.
026: *
027: * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
028: * @since ostermillerutils 1.02.00
029: */
030: public class RandPassApplet extends JApplet {
031:
032: private static final long serialVersionUID = 1838771003552061341L;
033: private JTextArea display = new JTextArea();
034: private JButton clearButton = new JButton("Clear");
035: private JButton generateButton = new JButton("Generate");
036: private char[] passwordAlphabet = RandPass.NONCONFUSING_ALPHABET;
037: private JTextField alphabetField = new JTextField(new String(
038: passwordAlphabet));
039: private char[] passwordFirstAlphabet = new char[0];
040: private JTextField alphabetFirstField = new JTextField(new String(
041: passwordFirstAlphabet));
042: private char[] passwordLastAlphabet = new char[0];
043: private JTextField alphabetLastField = new JTextField(new String(
044: passwordLastAlphabet));
045: private int passwordLength = 8;
046: private JTextField passwordLengthField = new JTextField(""
047: + passwordLength);
048: private RandPass randPass = new RandPass(passwordAlphabet);
049: private GridLayout preferencesPanelLayout = new GridLayout(4, 2);
050: private JPanel preferencesPanel = new JPanel(preferencesPanelLayout);
051:
052: /**
053: * Start the applet (to be called by the applet viewer)
054: *
055: * @since ostermillerutils 1.02.00
056: */
057: @Override
058: public void init() {
059: getContentPane().removeAll();
060: JMenuBar menuBar = new JMenuBar();
061: JMenu editMenu = new JMenu("Edit");
062: editMenu.setMnemonic(KeyEvent.VK_E);
063: editMenu.getAccessibleContext().setAccessibleDescription(
064: "Change how passwords are generated.");
065: JMenuItem prefsMenuItem = new JMenuItem("Preferences...",
066: KeyEvent.VK_P);
067: prefsMenuItem.getAccessibleContext().setAccessibleDescription(
068: "Set password length and content.");
069: preferencesPanelLayout.setHgap(5);
070: Dimension d;
071: d = passwordLengthField.getPreferredSize();
072: d.setSize(70, d.getHeight());
073: passwordLengthField.setPreferredSize(d);
074: d = alphabetField.getPreferredSize();
075: d.setSize(70, d.getHeight());
076: alphabetField.setPreferredSize(d);
077: d = alphabetFirstField.getPreferredSize();
078: d.setSize(70, d.getHeight());
079: alphabetFirstField.setPreferredSize(d);
080: d = alphabetLastField.getPreferredSize();
081: d.setSize(70, d.getHeight());
082: alphabetLastField.setPreferredSize(d);
083: preferencesPanel.add(new JLabel("Length:"));
084: preferencesPanel.add(passwordLengthField);
085: preferencesPanel.add(new JLabel("Alphabet:"));
086: preferencesPanel.add(alphabetField);
087: preferencesPanel.add(new JLabel("First Character Alphabet:"));
088: preferencesPanel.add(alphabetFirstField);
089: preferencesPanel.add(new JLabel("Last Character Alphabet:"));
090: preferencesPanel.add(alphabetLastField);
091: prefsMenuItem.addActionListener(new ActionListener() {
092: public void actionPerformed(ActionEvent e) {
093: passwordLengthField.setText("" + passwordLength);
094: alphabetField.setText(new String(passwordAlphabet));
095: alphabetFirstField.setText(new String(
096: passwordFirstAlphabet));
097: alphabetLastField.setText(new String(
098: passwordLastAlphabet));
099: int preferencesResult = JOptionPane.showConfirmDialog(
100: RandPassApplet.this , preferencesPanel,
101: "Preferences", JOptionPane.OK_CANCEL_OPTION,
102: JOptionPane.QUESTION_MESSAGE);
103: if (preferencesResult == JOptionPane.OK_OPTION) {
104: try {
105: int length = Integer
106: .parseInt(passwordLengthField.getText());
107: if (length >= 3 && length <= 100) {
108: passwordLength = length;
109: }
110: } catch (NumberFormatException x) {
111: // Password length could not be set
112: }
113: String alphabetString = alphabetField.getText();
114: passwordAlphabet = new char[alphabetString.length()];
115: alphabetString.getChars(0, alphabetString.length(),
116: passwordAlphabet, 0);
117: randPass.setAlphabet(passwordAlphabet);
118: String alphabetFirstString = alphabetFirstField
119: .getText();
120: passwordFirstAlphabet = new char[alphabetFirstString
121: .length()];
122: alphabetFirstString.getChars(0, alphabetFirstString
123: .length(), passwordFirstAlphabet, 0);
124: randPass.setFirstAlphabet(passwordFirstAlphabet);
125: String alphabetLastString = alphabetLastField
126: .getText();
127: passwordLastAlphabet = new char[alphabetLastString
128: .length()];
129: alphabetLastString.getChars(0, alphabetLastString
130: .length(), passwordLastAlphabet, 0);
131: randPass.setLastAlphabet(passwordLastAlphabet);
132: }
133: }
134: });
135: editMenu.add(prefsMenuItem);
136: menuBar.add(editMenu);
137: setJMenuBar(menuBar);
138:
139: display.setEditable(false);
140: display.setFont(new Font("Monospaced", Font.PLAIN, 12));
141: getContentPane().setLayout(new BorderLayout());
142: getContentPane().add(new JScrollPane(display),
143: BorderLayout.CENTER);
144: JPanel buttonPanel = new JPanel(new FlowLayout());
145: clearButton.addActionListener(new ActionListener() {
146: public void actionPerformed(ActionEvent e) {
147: display.setText("");
148: }
149: });
150: buttonPanel.add(clearButton);
151: generateButton.addActionListener(new ActionListener() {
152: public void actionPerformed(ActionEvent e) {
153: display.append(randPass.getPass(passwordLength) + '\n');
154: }
155: });
156: buttonPanel.add(generateButton);
157: getContentPane().add(buttonPanel, BorderLayout.SOUTH);
158: display.setText(randPass.getPass(passwordLength) + '\n');
159: }
160:
161: /**
162: * Get information such as the name of this applet, the author of
163: * this applet, and a description of this applet.
164: *
165: * @return a string with the information about this applet.
166: *
167: * @since ostermillerutils 1.02.00
168: */
169: @Override
170: public String getAppletInfo() {
171: return ("Title: Random Password Generator\n"
172: + "Author: Stephen Ostermiller\n"
173: + "http://ostermiller.org/contact.pl?regarding=Random+Password+Generator+Applet\n"
174: + "Generates secure random passwords.");
175: }
176: }
|