1
2
3
4
5 package net.java.truecommons.key.swing;
6
7 import net.java.truecommons.key.swing.io.FileComboBoxBrowser;
8
9 import javax.annotation.Nullable;
10 import javax.swing.*;
11 import javax.swing.filechooser.FileSystemView;
12 import javax.swing.text.Document;
13 import javax.swing.text.JTextComponent;
14 import java.awt.*;
15 import java.awt.event.WindowEvent;
16 import java.awt.event.WindowFocusListener;
17 import java.io.File;
18 import java.util.ResourceBundle;
19
20
21
22
23
24
25
26
27 public final class AuthenticationPanel extends JPanel {
28
29 private static final long serialVersionUID = 0L;
30
31 private static final ResourceBundle resources = ResourceBundle
32 .getBundle(AuthenticationPanel.class.getName());
33
34
35 static final int AUTH_PASSWD = 0;
36
37
38 static final int AUTH_KEY_FILE = 1;
39
40 private final FileComboBoxBrowser fcbb;
41
42 public AuthenticationPanel() {
43
44
45 fcbb = new FileComboBoxBrowser(keyFile);
46 initComponents();
47 ((JTextField) keyFile.getEditor().getEditorComponent()).setColumns(30);
48 }
49
50
51
52
53
54
55
56
57 public FileSystemView getFileSystemView() {
58 return fcbb.getFileSystemView();
59 }
60
61
62
63
64
65 public void setFileSystemView(@Nullable FileSystemView fsv) {
66 fcbb.setFileSystemView(fsv);
67 }
68
69
70
71
72
73
74
75
76
77 public File getDirectory() { return fcbb.getDirectory(); }
78
79
80
81
82
83 public void setDirectory(@Nullable File dir) {
84 fcbb.setDirectory(dir);
85 }
86
87
88
89
90
91
92
93
94 public void setPasswdPanel(final JPanel passwdPanel) {
95 passwdPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
96 final String title = resources.getString("tab.passwd");
97 if (title.equals(tabs.getTitleAt(AUTH_PASSWD)))
98 tabs.removeTabAt(AUTH_PASSWD);
99 tabs.insertTab(title, null, passwdPanel, null, AUTH_PASSWD);
100 tabs.setSelectedIndex(AUTH_PASSWD);
101 revalidate();
102 }
103
104 Document getKeyFileDocument() {
105 return ((JTextComponent) keyFile.getEditor().getEditorComponent()).getDocument();
106 }
107
108
109
110
111
112
113 File getKeyFile() {
114 String path = (String) keyFile.getSelectedItem();
115 File file = new File(path);
116 return file.isAbsolute() ? file : new File(getDirectory(), path);
117 }
118
119 private void setKeyFile(final File file) {
120 String newPath = file.getPath();
121 {
122 final String dir = getDirectory().getPath();
123 if (newPath.startsWith(dir))
124 newPath = newPath.substring(dir.length() + 1);
125 }
126 final String oldPath = (String) keyFile.getSelectedItem();
127 if (newPath.equals(oldPath)) return;
128 keyFile.setSelectedItem(newPath);
129 }
130
131
132
133
134
135
136 int getAuthenticationMethod() {
137 final int method = tabs.getSelectedIndex();
138 switch (method) {
139 case AUTH_PASSWD:
140 assert resources.getString("tab.passwd").equals(tabs.getTitleAt(method));
141 break;
142 case AUTH_KEY_FILE:
143 assert resources.getString("tab.keyFile").equals(tabs.getTitleAt(method));
144 break;
145 default:
146 throw new AssertionError("Unsupported authentication method!");
147 }
148 return method;
149 }
150
151
152
153
154
155
156
157 private void initComponents() {
158 java.awt.GridBagConstraints gridBagConstraints;
159
160 final net.java.truecommons.key.swing.util.EnhancedPanel keyFilePanel = new net.java.truecommons.key.swing.util.EnhancedPanel();
161 final javax.swing.JLabel keyFileLabel = new javax.swing.JLabel();
162 final javax.swing.JButton keyFileChooser = new javax.swing.JButton();
163
164 setLayout(new java.awt.GridBagLayout());
165
166 keyFilePanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
167 keyFilePanel.addPanelListener(new net.java.truecommons.key.swing.util.PanelListener() {
168 public void ancestorWindowShown(net.java.truecommons.key.swing.util.PanelEvent evt) {
169 keyFilePanelAncestorWindowShown(evt);
170 }
171 public void ancestorWindowHidden(net.java.truecommons.key.swing.util.PanelEvent evt) {
172 }
173 });
174 keyFilePanel.setLayout(new java.awt.GridBagLayout());
175
176 keyFileLabel.setDisplayedMnemonic(resources.getString("keyFile").charAt(0));
177 keyFileLabel.setLabelFor(keyFile);
178 keyFileLabel.setText(resources.getString("keyFile"));
179 gridBagConstraints = new java.awt.GridBagConstraints();
180 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
181 gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
182 keyFilePanel.add(keyFileLabel, gridBagConstraints);
183
184 keyFile.setEditable(true);
185 gridBagConstraints = new java.awt.GridBagConstraints();
186 gridBagConstraints.gridx = 0;
187 gridBagConstraints.gridy = 1;
188 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
189 gridBagConstraints.weightx = 1.0;
190 keyFilePanel.add(keyFile, gridBagConstraints);
191
192 keyFileChooser.setIcon(UIManager.getIcon("FileView.directoryIcon"));
193 keyFileChooser.setToolTipText(resources.getString("selectKeyFile.toolTip"));
194 keyFileChooser.setName("keyFileChooser");
195 keyFileChooser.addActionListener(new java.awt.event.ActionListener() {
196 public void actionPerformed(java.awt.event.ActionEvent evt) {
197 keyFileChooserActionPerformed(evt);
198 }
199 });
200 gridBagConstraints = new java.awt.GridBagConstraints();
201 gridBagConstraints.gridx = 1;
202 gridBagConstraints.gridy = 1;
203 gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
204 keyFilePanel.add(keyFileChooser, gridBagConstraints);
205
206 tabs.addTab(resources.getString("tab.keyFile"), keyFilePanel);
207
208 gridBagConstraints = new java.awt.GridBagConstraints();
209 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
210 gridBagConstraints.weightx = 1.0;
211 gridBagConstraints.weighty = 1.0;
212 add(tabs, gridBagConstraints);
213 }
214
215 private void keyFileChooserActionPerformed(java.awt.event.ActionEvent evt) {
216 final JFileChooser fc = newFileChooser();
217 if (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(this))
218 setKeyFile(fc.getSelectedFile());
219 }
220
221 private JFileChooser newFileChooser() {
222 final JFileChooser fc = new JFileChooser(getDirectory());
223 fc.setDialogTitle(resources.getString("fileChooser.title"));
224 fc.setFileHidingEnabled(false);
225 return fc;
226 }
227
228 private void keyFilePanelAncestorWindowShown(net.java.truecommons.key.swing.util.PanelEvent evt) {
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258 final Window window = evt.getSource().getAncestorWindow();
259 window.addWindowFocusListener(new WindowFocusListener() {
260 @Override
261 public void windowGainedFocus(WindowEvent e) {
262 window.removeWindowFocusListener(this);
263 EventQueue.invokeLater(new Runnable() {
264 @Override
265 public void run() {
266 if (keyFile.requestFocusInWindow())
267 ((JTextComponent) keyFile.getEditor().getEditorComponent()).selectAll();
268 }
269 });
270 }
271
272 @Override
273 public void windowLostFocus(WindowEvent e) {
274 }
275 });
276 }
277
278
279 private final javax.swing.JComboBox<String> keyFile = new javax.swing.JComboBox<String>();
280 private final javax.swing.JTabbedPane tabs = new javax.swing.JTabbedPane();
281
282 }