KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Ostermiller > util > RandPassApplet


1 /*
2  * Generate random passwords.
3  * Copyright (C) 2003 Stephen Ostermiller
4  * http://ostermiller.org/contact.pl?regarding=Random+Password+Generator+Applet
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * See COPYING.TXT for details.
17  */

18
19 package com.Ostermiller.util;
20
21 import javax.swing.*;
22 import java.awt.*;
23 import java.awt.event.*;
24 import com.Ostermiller.util.*;
25
26 /**
27  * An applet that will let the user generate random passwords.
28  *
29  * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
30  * @since ostermillerutils 1.02.00
31  */

32 public class RandPassApplet extends JApplet {
33
34     private JTextArea display = new JTextArea();
35     private JButton clearButton = new JButton("Clear");
36     private JButton generateButton = new JButton("Generate");
37     private char[] passwordAlphabet = RandPass.NONCONFUSING_ALPHABET;
38     private JTextField alphabetField = new JTextField(new String JavaDoc(passwordAlphabet));
39     private char[] passwordFirstAlphabet = new char[0];
40     private JTextField alphabetFirstField = new JTextField(new String JavaDoc(passwordFirstAlphabet));
41     private char[] passwordLastAlphabet = new char[0];
42     private JTextField alphabetLastField = new JTextField(new String JavaDoc(passwordLastAlphabet));
43     private int passwordLength = 8;
44     private JTextField passwordLengthField = new JTextField("" + passwordLength);
45     private RandPass randPass = new RandPass(passwordAlphabet);
46     private GridLayout preferencesPanelLayout = new GridLayout(4,2);
47     private JPanel preferencesPanel = new JPanel(preferencesPanelLayout);
48
49     /**
50      * Start the applet (to be called by the appletviewer)
51      *
52      * @since ostermillerutils 1.02.00
53      */

54     public void init() {
55         getContentPane().removeAll();
56         JMenuBar menuBar = new JMenuBar();
57         JMenu editMenu = new JMenu("Edit");
58         editMenu.setMnemonic(KeyEvent.VK_E);
59         editMenu.getAccessibleContext().setAccessibleDescription(
60             "Change how passwords are generated."
61         );
62         JMenuItem prefsMenuItem = new JMenuItem("Preferences...", KeyEvent.VK_P);
63         prefsMenuItem.getAccessibleContext().setAccessibleDescription(
64             "Set password length and content."
65         );
66         preferencesPanelLayout.setHgap(5);
67         Dimension d;
68         d = passwordLengthField.getPreferredSize();
69         d.setSize(70, d.getHeight());
70         passwordLengthField.setPreferredSize(d);
71         d = alphabetField.getPreferredSize();
72         d.setSize(70, d.getHeight());
73         alphabetField.setPreferredSize(d);
74         d = alphabetFirstField.getPreferredSize();
75         d.setSize(70, d.getHeight());
76         alphabetFirstField.setPreferredSize(d);
77         d = alphabetLastField.getPreferredSize();
78         d.setSize(70, d.getHeight());
79         alphabetLastField.setPreferredSize(d);
80         preferencesPanel.add(new JLabel("Length:"));
81         preferencesPanel.add(passwordLengthField);
82         preferencesPanel.add(new JLabel("Alphabet:"));
83         preferencesPanel.add(alphabetField);
84         preferencesPanel.add(new JLabel("First Character Alphabet:"));
85         preferencesPanel.add(alphabetFirstField);
86         preferencesPanel.add(new JLabel("Last Character Alphabet:"));
87         preferencesPanel.add(alphabetLastField);
88         prefsMenuItem.addActionListener(new ActionListener(){
89             public void actionPerformed(ActionEvent e){
90                 passwordLengthField.setText("" + passwordLength);
91                 alphabetField.setText(new String JavaDoc(passwordAlphabet));
92                 alphabetFirstField.setText(new String JavaDoc(passwordFirstAlphabet));
93                 alphabetLastField.setText(new String JavaDoc(passwordLastAlphabet));
94                 int preferencesResult = JOptionPane.showConfirmDialog(
95                     RandPassApplet.this,
96                     preferencesPanel,
97                     "Preferences",
98                     JOptionPane.OK_CANCEL_OPTION,
99                     JOptionPane.QUESTION_MESSAGE
100                 );
101                 if (preferencesResult == JOptionPane.OK_OPTION){
102                     try {
103                         int length = Integer.parseInt(passwordLengthField.getText());
104                         if (length >= 3 && length <=100){
105                             passwordLength = length;
106                         }
107                     } catch (NumberFormatException JavaDoc x){
108                     }
109                     String JavaDoc alphabetString = alphabetField.getText();
110                     passwordAlphabet = new char[alphabetString.length()];
111                     alphabetString.getChars(0, alphabetString.length(), passwordAlphabet, 0);
112                     randPass.setAlphabet(passwordAlphabet);
113                     String JavaDoc alphabetFirstString = alphabetFirstField.getText();
114                     passwordFirstAlphabet = new char[alphabetFirstString.length()];
115                     alphabetFirstString.getChars(0, alphabetFirstString.length(), passwordFirstAlphabet, 0);
116                     randPass.setFirstAlphabet(passwordFirstAlphabet);
117                     String JavaDoc alphabetLastString = alphabetLastField.getText();
118                     passwordLastAlphabet = new char[alphabetLastString.length()];
119                     alphabetLastString.getChars(0, alphabetLastString.length(), passwordLastAlphabet, 0);
120                     randPass.setLastAlphabet(passwordLastAlphabet);
121                 }
122             }
123         });
124         editMenu.add(prefsMenuItem);
125         menuBar.add(editMenu);
126         setJMenuBar(menuBar);
127
128         display.setEditable(false);
129         display.setFont(new Font("Monospaced", Font.PLAIN, 12));
130         getContentPane().setLayout(new BorderLayout());
131         getContentPane().add(new JScrollPane(display), BorderLayout.CENTER);
132         JPanel buttonPanel = new JPanel(new FlowLayout());
133         clearButton.addActionListener(new ActionListener(){
134             public void actionPerformed(ActionEvent e){
135                 display.setText("");
136             }
137         });
138         buttonPanel.add(clearButton);
139         generateButton.addActionListener(new ActionListener(){
140             public void actionPerformed(ActionEvent e){
141                 display.append(randPass.getPass(passwordLength) + '\n');
142             }
143         });
144         buttonPanel.add(generateButton);
145         getContentPane().add(buttonPanel, BorderLayout.SOUTH);
146         display.setText(randPass.getPass(passwordLength) + '\n');
147     }
148
149     /**
150      * Get information such as the name of this applet, the author of
151      * this applet, and a description of this applet.
152      *
153      * @return a string with the information about this applet.
154      *
155      * @since ostermillerutils 1.02.00
156      */

157     public String JavaDoc getAppletInfo() {
158         return (
159             "Title: Random Password Generator\n" +
160             "Author: Stephen Ostermiller\n" +
161             "http://ostermiller.org/contact.pl?regarding=Random+Password+Generator+Applet\n" +
162             "Generates secure random passwords."
163         );
164     }
165 }
166
Popular Tags