View Javadoc
1 package org.neuclear.commons.crypto.signers; 2 3 /* 4 * The NeuClear Project and it's libraries are 5 * (c) 2002-2004 Antilles Software Ventures SA 6 * For more information see: http://neuclear.org 7 * 8 * This library is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public 10 * License as published by the Free Software Foundation; either 11 * version 2.1 of the License, or (at your option) any later version. 12 * 13 * This library is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with this library; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 */ 22 23 import com.jgoodies.forms.builder.ButtonBarBuilder; 24 import com.jgoodies.forms.builder.PanelBuilder; 25 import com.jgoodies.forms.layout.CellConstraints; 26 import com.jgoodies.forms.layout.FormLayout; 27 import com.jgoodies.plaf.Options; 28 import org.neuclear.commons.crypto.CryptoTools; 29 import org.neuclear.commons.crypto.passphraseagents.GuiDialogAgent; 30 import org.neuclear.commons.crypto.passphraseagents.InteractiveAgent; 31 import org.neuclear.commons.crypto.passphraseagents.UserCancellationException; 32 import org.neuclear.commons.crypto.passphraseagents.icons.IconTools; 33 import org.neuclear.commons.crypto.passphraseagents.swing.JKSFilter; 34 import org.neuclear.commons.crypto.passphraseagents.swing.MessageLabel; 35 import org.neuclear.commons.crypto.passphraseagents.swing.NewPassphraseDialog; 36 import org.neuclear.commons.swing.Messages; 37 import org.neuclear.commons.swing.WaitForInput; 38 39 import javax.swing.*; 40 import java.awt.*; 41 import java.awt.event.*; 42 import java.io.File; 43 import java.io.FileNotFoundException; 44 import java.util.prefs.Preferences; 45 46 /*** 47 * User: pelleb 48 * Date: May 14, 2004 49 * Time: 12:11:53 PM 50 */ 51 public class OpenSignerDialog extends JDialog { 52 public OpenSignerDialog(Frame frame, InteractiveAgent agent) throws HeadlessException { 53 super(frame, TITLE, true); 54 try { 55 if (UIManager.getSystemLookAndFeelClassName().equals("apple.laf.AquaLookAndFeel")) 56 System.setProperty("com.apple.laf.useScreenMenuBar", "true"); 57 else { 58 UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel"); 59 UIManager.put(Options.USE_SYSTEM_FONTS_APP_KEY, Boolean.TRUE); 60 } 61 } catch (Exception e) { 62 // Likely PlasticXP is not in the class path; ignore. 63 } 64 65 this.agent = agent; 66 prefs = Preferences.userNodeForPackage(PersonalSigner.class); 67 filename = prefs.get(KEYSTORE, CryptoTools.DEFAULT_KEYSTORE); 68 69 setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); 70 find = new JButton(IconTools.getOpen()); 71 ok = new JButton(Messages.getOK("openaccounts")); 72 ok.setIcon(IconTools.getOK()); 73 ok.setEnabled(false); 74 cancel = new JButton(Messages.getText("cancel")); 75 cancel.setIcon(IconTools.getCancel()); 76 filefield = new JTextField(filename); 77 passphrase = new JPasswordField(); 78 79 message = new MessageLabel(); 80 banner = new com.l2fprod.common.swing.BannerPanel(); 81 banner.setIcon(IconTools.getLogo()); 82 banner.setTitle(Messages.getTitle("openaccounts")); 83 banner.setSubtitle(Messages.getDescription("openaccounts")); 84 Container contents = getContentPane(); 85 // contents.setLayout(new BorderLayout()); 86 // contents.add(banner,BorderLayout.NORTH); 87 contents.add(buildPanel()); 88 pack(); 89 setResizable(false); 90 91 final ActionListener cancelOrClose = new ActionListener() { 92 public void actionPerformed(final ActionEvent actionEvent) { 93 synchronized (passphrase) { 94 passphrase.setText(""); 95 hide(); 96 runner.cancel(); 97 } 98 99 } 100 }; 101 cancel.addActionListener(cancelOrClose); 102 103 final ActionListener action = new ActionListener() { 104 public void actionPerformed(final ActionEvent actionEvent) { 105 synchronized (passphrase) { 106 if (validateForm()) { 107 runner.execute(); 108 } 109 } 110 111 } 112 }; 113 114 ok.addActionListener(action); 115 passphrase.addActionListener(action); 116 final KeyListener validate = new KeyListener() { 117 public void keyPressed(KeyEvent e) { 118 119 } 120 121 public void keyReleased(KeyEvent e) { 122 ok.setEnabled(validateForm()); 123 if (e.getSource().equals(filefield)) 124 updateOKText(); 125 } 126 127 public void keyTyped(KeyEvent e) { 128 129 } 130 }; 131 passphrase.addKeyListener(validate); 132 filefield.addKeyListener(validate); 133 ((JComponent) contents).registerKeyboardAction(cancelOrClose, 134 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), 135 JComponent.WHEN_IN_FOCUSED_WINDOW); 136 137 addWindowListener(new WindowAdapter() { 138 public void windowClosing(WindowEvent e) { 139 runner.cancel(); 140 } 141 }); 142 find.addActionListener(new ActionListener() { 143 public void actionPerformed(ActionEvent event) { 144 prepFileChooser(filefield.getText(), "Select Accounts File"); 145 int result = fc.showOpenDialog(dia); 146 if (result == JFileChooser.APPROVE_OPTION) 147 filefield.setText(fc.getSelectedFile().getAbsolutePath()); 148 passphrase.requestFocus(); 149 } 150 151 }); 152 153 154 } 155 156 private void updateOKText() { 157 File file = new File(filefield.getText()); 158 if (file.exists()) 159 ok.setText(Messages.getOK("openaccounts")); 160 else 161 ok.setText(Messages.getComponentText("openaccounts", "create.title")); 162 } 163 164 private boolean validateForm() { 165 return (passphrase.getPassword().length > 0); 166 } 167 168 private void prepFileChooser(String def, String title) { 169 File file = new File(def); 170 if (fc == null) { 171 fc = new JFileChooser(); 172 fc.setFileFilter(new JKSFilter()); 173 } 174 175 fc.setCurrentDirectory(file.getParentFile()); 176 fc.setSelectedFile(file); 177 fc.setDialogTitle(title); 178 } 179 180 private Component buildPanel() { 181 FormLayout layout = new FormLayout("right:pref, 3dlu, pref:grow,1dlu,pref ", 182 "pref,3dlu,pref,3dlu,pref, 3dlu,pref, 5dlu:grow, pref"); 183 PanelBuilder builder = new PanelBuilder(layout); 184 CellConstraints cc = new CellConstraints(); 185 186 builder.setDefaultDialogBorder(); 187 188 builder.add(banner, cc.xyw(1, 1, 5)); 189 builder.addLabel(Messages.getText("accountsfile"), cc.xy(1, 3)).setLabelFor(filefield); 190 builder.add(filefield, cc.xy(3, 3)); 191 builder.add(find, cc.xy(5, 3)); 192 193 final JLabel label = builder.addLabel(Messages.getText("passphrase"), cc.xy(1, 5)); 194 label.setLabelFor(passphrase); 195 label.setIcon(IconTools.getPassword()); 196 builder.add(passphrase, cc.xyw(3, 5, 3)); 197 builder.add(message, cc.xyw(1, 7, 5)); 198 199 200 ButtonBarBuilder bb = new ButtonBarBuilder(); 201 bb.addGlue(); 202 bb.addUnrelatedGap(); 203 bb.addGridded(ok); 204 bb.addGridded(cancel); 205 builder.add(bb.getPanel(), cc.xyw(1, 9, 5)); 206 207 return builder.getPanel(); 208 } 209 210 private void open() { 211 pack(); 212 filefield.setText(filename); 213 updateOKText(); 214 message.clear(); 215 com.l2fprod.common.swing.UIUtilities.centerOnScreen(dia); 216 passphrase.requestFocus(); 217 show(); 218 toFront(); 219 } 220 221 public BrowsableSigner openSigner() throws UserCancellationException { 222 WaitForInput wait = new DialogRunner(); 223 new Thread(wait).start(); 224 225 return (BrowsableSigner) wait.getResult(); 226 } 227 228 public void save(JCESigner signer, boolean force) throws UserCancellationException { 229 if (!force) { 230 prepFileChooser(filename, Messages.getText("selectaccountsfile")); 231 int result = fc.showSaveDialog(dia); 232 if (result == JFileChooser.APPROVE_OPTION) 233 filename = fc.getSelectedFile().getAbsolutePath(); 234 else 235 throw new UserCancellationException(filename); 236 if (npd == null) 237 npd = new NewPassphraseDialog(); 238 WaitForInput wait = npd.createGetNewPassphraseTask(filename); 239 new Thread(wait).start(); 240 storedPassphrase = (char[]) wait.getResult(); 241 } 242 try { 243 signer.save(filename, storedPassphrase); 244 } catch (FileNotFoundException e) { 245 save(signer, false); 246 } 247 } 248 249 private final com.l2fprod.common.swing.BannerPanel banner; 250 private final JTextField filefield; 251 private final JPasswordField passphrase; 252 private char storedPassphrase[] = null; 253 private final JButton find; 254 private final JButton ok; 255 private final JButton cancel; 256 private final MessageLabel message; 257 private JFileChooser fc; 258 private WaitForInput runner; 259 private NewPassphraseDialog npd; 260 261 private String filename; 262 private Preferences prefs; 263 264 private final InteractiveAgent agent; 265 private Dialog dia = this; 266 public static final String TITLE = "Open Accounts File"; 267 private static final String KEYSTORE = "KEYSTORE"; 268 269 class DialogRunner extends WaitForInput { 270 public DialogRunner() { 271 } 272 273 public void run() { 274 runner = this; 275 open(); 276 // if (incorrect) 277 278 279 } 280 281 public void execute() { 282 final char[] phrase = passphrase.getPassword(); 283 try { 284 BrowsableSigner signer = new JCESigner(filefield.getText(), "JKS", "SUN", agent, phrase); 285 prefs.put(KEYSTORE, filefield.getText()); 286 filename = filefield.getText(); 287 hide(); 288 storedPassphrase = passphrase.getPassword(); 289 passphrase.setText(""); 290 setResult(signer); 291 } catch (InvalidPassphraseException e) { 292 message.invalidPassphrase(); 293 return; 294 } 295 } 296 297 298 } 299 300 public static void main(String args[]) { 301 JFrame frame = new JFrame("test"); 302 303 OpenSignerDialog dialog = new OpenSignerDialog(frame, new GuiDialogAgent()); 304 try { 305 BrowsableSigner signer = dialog.openSigner(); 306 } catch (UserCancellationException e) { 307 System.out.println("User Cancelled"); 308 } 309 System.exit(0); 310 } 311 312 313 } 314

This page was automatically generated by Maven