| 1 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 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 (passwordAlphabet)); 39 private char[] passwordFirstAlphabet = new char[0]; 40 private JTextField alphabetFirstField = new JTextField(new String (passwordFirstAlphabet)); 41 private char[] passwordLastAlphabet = new char[0]; 42 private JTextField alphabetLastField = new JTextField(new String (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 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 (passwordAlphabet)); 92 alphabetFirstField.setText(new String (passwordFirstAlphabet)); 93 alphabetLastField.setText(new String (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 x){ 108 } 109 String alphabetString = alphabetField.getText(); 110 passwordAlphabet = new char[alphabetString.length()]; 111 alphabetString.getChars(0, alphabetString.length(), passwordAlphabet, 0); 112 randPass.setAlphabet(passwordAlphabet); 113 String alphabetFirstString = alphabetFirstField.getText(); 114 passwordFirstAlphabet = new char[alphabetFirstString.length()]; 115 alphabetFirstString.getChars(0, alphabetFirstString.length(), passwordFirstAlphabet, 0); 116 randPass.setFirstAlphabet(passwordFirstAlphabet); 117 String 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 157 public String 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 |