1 16 17 package de.schlichtherle.key.passwd.swing; 18 19 import de.schlichtherle.io.*; 20 import de.schlichtherle.io.swing.*; 21 22 import java.awt.*; 23 import java.awt.event.*; 24 import java.lang.ref.*; 25 import java.util.*; 26 27 import javax.swing.*; 28 import javax.swing.text.*; 29 30 38 public class AuthenticationPanel extends JPanel { 39 40 private static final String CLASS_NAME 41 = "de/schlichtherle/key/passwd/swing/AuthenticationPanel".replace('/', '.'); private static final ResourceBundle resources 43 = ResourceBundle.getBundle(CLASS_NAME); 44 private static final File BASE_DIR = new File(".", ArchiveDetector.NULL); 45 46 private static SoftReference fileChooser; 47 48 49 public static final int AUTH_PASSWD = 0; 50 51 52 public static final int AUTH_KEY_FILE = 1; 53 54 58 public AuthenticationPanel() { 59 initComponents(); 60 61 new FileComboBoxBrowser(keyFile).setDirectory(BASE_DIR); 64 ((JTextField) keyFile.getEditor().getEditorComponent()).setColumns(30); 65 } 66 67 73 public void setPasswdPanel(JPanel passwdPanel) { 74 if (passwdPanel == null) 75 throw new NullPointerException (); 76 77 passwdPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 78 final String title = resources.getString("tab.passwd"); 79 if (title.equals(tabs.getTitleAt(AUTH_PASSWD))) 80 tabs.removeTabAt(AUTH_PASSWD); 81 tabs.insertTab(title, null, passwdPanel, null, AUTH_PASSWD); tabs.setSelectedIndex(AUTH_PASSWD); 83 revalidate(); 84 } 85 86 Document getKeyFileDocument() { 87 return ((JTextComponent) keyFile.getEditor().getEditorComponent()).getDocument(); 88 } 89 90 96 public String getKeyFilePath() { 97 return (String ) keyFile.getSelectedItem(); 98 } 99 100 private void setKeyFilePath(final String path) { 101 final String oldPath = (String ) keyFile.getSelectedItem(); 102 if (path == oldPath || path != null && path.equals(oldPath)) 103 return; 104 105 keyFile.setSelectedItem(path); 106 109 } 110 111 116 public int getAuthenticationMethod() { 117 final int method = tabs.getSelectedIndex(); 118 switch (method) { 119 case AUTH_PASSWD: 120 assert resources.getString("tab.passwd").equals(tabs.getTitleAt(method)); 121 break; 122 123 case AUTH_KEY_FILE: 124 assert resources.getString("tab.keyFile").equals(tabs.getTitleAt(method)); 125 break; 126 127 default: 128 throw new AssertionError ("Unsupported authentication method!"); 129 } 130 return method; 131 } 132 133 143 static javax.swing.JFileChooser getFileChooser() { 144 final SoftReference ref = fileChooser; javax.swing.JFileChooser fc = ref != null ? (javax.swing.JFileChooser ) ref.get() : null; 146 if (fc == null) { 147 fc = new CustomFileChooser(); 148 fileChooser = new SoftReference(fc); 149 } 150 return fc; 151 } 152 153 158 private void initComponents() { 160 java.awt.GridBagConstraints gridBagConstraints; 161 162 tabs = new javax.swing.JTabbedPane (); 163 final javax.swing.JLabel keyFileLabel = new javax.swing.JLabel (); 164 165 setLayout(new java.awt.GridBagLayout ()); 166 167 keyFilePanel.setLayout(new java.awt.GridBagLayout ()); 168 169 keyFilePanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10)); 170 keyFilePanel.addPanelListener(new de.schlichtherle.swing.event.PanelListener() { 171 public void ancestorWindowShown(de.schlichtherle.swing.event.PanelEvent evt) { 172 keyFilePanelAncestorWindowShown(evt); 173 } 174 public void ancestorWindowHidden(de.schlichtherle.swing.event.PanelEvent evt) { 175 } 176 }); 177 178 keyFileLabel.setDisplayedMnemonic(resources.getString("keyFile").charAt(0)); 179 keyFileLabel.setLabelFor(keyFile); 180 keyFileLabel.setText(resources.getString("keyFile")); gridBagConstraints = new java.awt.GridBagConstraints (); 182 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 183 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 184 keyFilePanel.add(keyFileLabel, gridBagConstraints); 185 186 keyFile.setEditable(true); 187 gridBagConstraints = new java.awt.GridBagConstraints (); 188 gridBagConstraints.gridx = 0; 189 gridBagConstraints.gridy = 1; 190 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 191 gridBagConstraints.weightx = 1.0; 192 keyFilePanel.add(keyFile, gridBagConstraints); 193 194 keyFileChooser.setIcon(UIManager.getIcon("FileView.directoryIcon")); 195 keyFileChooser.setToolTipText(resources.getString("selectKeyFile.toolTip")); keyFileChooser.setName("keyFileChooser"); 197 keyFileChooser.addActionListener(new java.awt.event.ActionListener () { 198 public void actionPerformed(java.awt.event.ActionEvent evt) { 199 keyFileChooserActionPerformed(evt); 200 } 201 }); 202 203 gridBagConstraints = new java.awt.GridBagConstraints (); 204 gridBagConstraints.gridx = 1; 205 gridBagConstraints.gridy = 1; 206 gridBagConstraints.insets = new java.awt.Insets (0, 10, 0, 0); 207 keyFilePanel.add(keyFileChooser, gridBagConstraints); 208 209 tabs.addTab(resources.getString("tab.keyFile"), keyFilePanel); 211 gridBagConstraints = new java.awt.GridBagConstraints (); 212 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 213 gridBagConstraints.weightx = 1.0; 214 gridBagConstraints.weighty = 1.0; 215 add(tabs, gridBagConstraints); 216 217 } 219 private void keyFileChooserActionPerformed(java.awt.event.ActionEvent evt) { final javax.swing.JFileChooser fc = getFileChooser(); 221 if (fc.showOpenDialog(this) == javax.swing.JFileChooser.APPROVE_OPTION) { 222 final File file = new File(fc.getSelectedFile(), ArchiveDetector.NULL); 223 final String baseDirPath = BASE_DIR.getCanOrAbsPath(); 224 String keyFilePath = file.getCanOrAbsPath(); 225 if (keyFilePath.startsWith(baseDirPath)) { 226 assert keyFilePath.charAt(baseDirPath.length()) == File.separatorChar; 227 keyFilePath = keyFilePath.substring(baseDirPath.length() + 1); } 229 setKeyFilePath(keyFilePath); 230 } 231 } 233 private void keyFilePanelAncestorWindowShown(de.schlichtherle.swing.event.PanelEvent evt) { final Window window = evt.getAncestorWindow(); 265 window.addWindowFocusListener(new WindowFocusListener() { 266 public void windowGainedFocus(WindowEvent e) { 267 window.removeWindowFocusListener(this); 268 EventQueue.invokeLater(new Runnable () { 269 public void run() { 270 if (keyFile.requestFocusInWindow()) 271 ((JTextComponent) keyFile.getEditor().getEditorComponent()).selectAll(); 272 } 273 }); 274 } 275 276 public void windowLostFocus(WindowEvent e) { 277 } 278 }); 279 } 281 private final javax.swing.JComboBox keyFile = new javax.swing.JComboBox (); 283 private final javax.swing.JButton keyFileChooser = new javax.swing.JButton (); 284 private final de.schlichtherle.swing.EnhancedPanel keyFilePanel = new de.schlichtherle.swing.EnhancedPanel(); 285 private javax.swing.JTabbedPane tabs; 286 288 private static class CustomFileChooser extends javax.swing.JFileChooser { 289 private static java.io.File lastCurrentDir = BASE_DIR; 290 291 public CustomFileChooser() { 292 super(lastCurrentDir); 293 294 setDialogTitle(resources.getString("fileChooser.title")); 295 setFileHidingEnabled(false); 296 } 297 298 public void setCurrentDirectory(java.io.File dir) { 299 super.setCurrentDirectory(dir); 300 lastCurrentDir = dir; 301 } 302 303 public java.io.File getCurrentDirectory() { 304 return lastCurrentDir = super.getCurrentDirectory(); 305 } 306 } 307 } 308 | Popular Tags |