This project has retired. For details please refer to its
Attic page.
LoginDialog xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.chemistry.opencmis.workbench;
20
21 import java.awt.BorderLayout;
22 import java.awt.Component;
23 import java.awt.Container;
24 import java.awt.Cursor;
25 import java.awt.Dimension;
26 import java.awt.Font;
27 import java.awt.Frame;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.awt.event.ItemEvent;
31 import java.awt.event.ItemListener;
32 import java.util.Collections;
33 import java.util.Comparator;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37
38 import javax.swing.BorderFactory;
39 import javax.swing.Box;
40 import javax.swing.BoxLayout;
41 import javax.swing.ButtonGroup;
42 import javax.swing.JButton;
43 import javax.swing.JComboBox;
44 import javax.swing.JDialog;
45 import javax.swing.JFrame;
46 import javax.swing.JLabel;
47 import javax.swing.JList;
48 import javax.swing.JPanel;
49 import javax.swing.JPasswordField;
50 import javax.swing.JRadioButton;
51 import javax.swing.JScrollPane;
52 import javax.swing.JTabbedPane;
53 import javax.swing.JTextArea;
54 import javax.swing.JTextField;
55 import javax.swing.ListCellRenderer;
56 import javax.swing.Spring;
57 import javax.swing.SpringLayout;
58 import javax.swing.UIManager;
59 import javax.swing.event.ChangeEvent;
60 import javax.swing.event.ChangeListener;
61
62 import org.apache.chemistry.opencmis.client.api.Repository;
63 import org.apache.chemistry.opencmis.commons.enums.BindingType;
64 import org.apache.chemistry.opencmis.workbench.ClientHelper.FileEntry;
65 import org.apache.chemistry.opencmis.workbench.model.ClientSession;
66
67 public class LoginDialog extends JDialog {
68
69 public static final String SYSPROP_URL = ClientSession.WORKBENCH_PREFIX + "url";
70 public static final String SYSPROP_BINDING = ClientSession.WORKBENCH_PREFIX + "binding";
71 public static final String SYSPROP_AUTHENTICATION = ClientSession.WORKBENCH_PREFIX + "authentication";
72 public static final String SYSPROP_COMPRESSION = ClientSession.WORKBENCH_PREFIX + "compression";
73 public static final String SYSPROP_CLIENTCOMPRESSION = ClientSession.WORKBENCH_PREFIX + "clientcompression";
74 public static final String SYSPROP_COOKIES = ClientSession.WORKBENCH_PREFIX + "cookies";
75 public static final String SYSPROP_USER = ClientSession.WORKBENCH_PREFIX + "user";
76 public static final String SYSPROP_PASSWORD = ClientSession.WORKBENCH_PREFIX + "password";
77
78 private static final String CONFIGS_FOLDER = "/configs/";
79 private static final String CONFIGS_LIBRARY = "config-library.properties";
80
81 private static final long serialVersionUID = 1L;
82
83 private JTabbedPane loginTabs;
84 private JTextField urlField;
85 private JRadioButton bindingAtomButton;
86 private JRadioButton bindingWebServicesButton;
87 private JRadioButton bindingBrowserButton;
88 private JTextField usernameField;
89 private JPasswordField passwordField;
90 private JRadioButton authenticationNoneButton;
91 private JRadioButton authenticationStandardButton;
92 private JRadioButton authenticationNTLMButton;
93 private JRadioButton compressionOnButton;
94 private JRadioButton compressionOffButton;
95 private JRadioButton clientCompressionOnButton;
96 private JRadioButton clientCompressionOffButton;
97 private JRadioButton cookiesOnButton;
98 private JRadioButton cookiesOffButton;
99 private JTextArea sessionParameterTextArea;
100 private JButton loadRepositoryButton;
101 private JButton loginButton;
102 private JComboBox repositoryBox;
103
104 private List<FileEntry> sessionConfigurations;
105
106 private boolean expertLogin = false;
107
108 private boolean canceled = true;
109
110 private ClientSession clientSession;
111
112 public LoginDialog(Frame owner) {
113 super(owner, "Login", true);
114 createGUI();
115 }
116
117 private void createGUI() {
118 setMinimumSize(new Dimension(700, 500));
119 setPreferredSize(new Dimension(700, 500));
120
121 Container pane = getContentPane();
122 pane.setLayout(new BorderLayout());
123
124 loginTabs = new JTabbedPane();
125 add(loginTabs, BorderLayout.CENTER);
126
127
128 JPanel basicPanel = new JPanel(new SpringLayout());
129
130 urlField = createTextField(basicPanel, "URL:");
131 urlField.setText(System.getProperty(SYSPROP_URL, ""));
132
133 createBindingButtons(basicPanel);
134
135 usernameField = createTextField(basicPanel, "Username:");
136 usernameField.setText(System.getProperty(SYSPROP_USER, ""));
137
138 passwordField = createPasswordField(basicPanel, "Password:");
139 passwordField.setText(System.getProperty(SYSPROP_PASSWORD, ""));
140
141 createAuthenticationButtons(basicPanel);
142
143 createCompressionButtons(basicPanel);
144
145 createClientCompressionButtons(basicPanel);
146
147 createCookieButtons(basicPanel);
148
149 makeCompactGrid(basicPanel, 8, 2, 5, 10, 5, 5);
150
151 loginTabs.addTab("Basic", basicPanel);
152
153
154 final JPanel expertPanel = new JPanel(new BorderLayout());
155 expertPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
156
157 sessionConfigurations = ClientHelper.readFileProperties(CONFIGS_FOLDER + CONFIGS_LIBRARY, CONFIGS_FOLDER);
158
159 final JComboBox configs = new JComboBox();
160 configs.setMaximumRowCount(20);
161
162 configs.addItem(new FileEntry("", null));
163 if (sessionConfigurations != null) {
164 for (FileEntry fe : sessionConfigurations) {
165 configs.addItem(fe);
166 }
167 }
168 expertPanel.add(configs, BorderLayout.PAGE_START);
169
170 sessionParameterTextArea = new JTextArea();
171 sessionParameterTextArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
172 expertPanel.add(new JScrollPane(sessionParameterTextArea), BorderLayout.CENTER);
173
174 loginTabs.addTab("Expert", expertPanel);
175
176
177 JPanel buttonPanel = new JPanel();
178 buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.PAGE_AXIS));
179 buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
180 add(buttonPanel, BorderLayout.PAGE_END);
181
182 loadRepositoryButton = createButton("Load Repositories");
183 buttonPanel.add(loadRepositoryButton);
184 getRootPane().setDefaultButton(loadRepositoryButton);
185
186 createRepositoryBox(buttonPanel);
187
188 loginButton = createButton("Login");
189 buttonPanel.add(loginButton);
190 loginButton.setEnabled(false);
191
192
193 loadRepositoryButton.addActionListener(new ActionListener() {
194 public void actionPerformed(ActionEvent e) {
195 repositoryBox.removeAllItems();
196
197 try {
198 setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
199 createClientSession();
200
201 List<Repository> repositories = clientSession.getRepositories();
202
203 Collections.sort(repositories, new Comparator<Repository>() {
204 @Override
205 public int compare(Repository r1, Repository r2) {
206 if (r1 == null || r1.getName() == null) {
207 return 1;
208 }
209
210 if (r2 == null || r2.getName() == null) {
211 return -1;
212 }
213
214 return r1.getName().compareTo(r2.getName());
215 }
216 });
217
218 if (repositories.size() > 0) {
219
220 for (Repository repository : repositories) {
221 repositoryBox.addItem(repository);
222 }
223
224 repositoryBox.setEnabled(true);
225 loginButton.setEnabled(true);
226 getRootPane().setDefaultButton(loginButton);
227 } else {
228 repositoryBox.setEnabled(false);
229 loginButton.setEnabled(false);
230 getRootPane().setDefaultButton(loadRepositoryButton);
231 }
232 } catch (Exception ex) {
233 repositoryBox.setEnabled(false);
234 loginButton.setEnabled(false);
235 getRootPane().setDefaultButton(loadRepositoryButton);
236
237 ClientHelper.showError(getOwner(), ex);
238 } finally {
239 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
240 }
241 }
242 });
243
244 loginButton.addActionListener(new ActionListener() {
245 public void actionPerformed(ActionEvent e) {
246 try {
247 setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
248
249 clientSession.createSession(repositoryBox.getSelectedIndex());
250 canceled = false;
251 hideDialog();
252 } catch (Exception ex) {
253 repositoryBox.setEnabled(false);
254 loginButton.setEnabled(false);
255 getRootPane().setDefaultButton(loadRepositoryButton);
256
257 ClientHelper.showError(getOwner(), ex);
258
259 } finally {
260 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
261 getRootPane().setDefaultButton(loadRepositoryButton);
262 }
263 }
264 });
265
266 loginTabs.addChangeListener(new ChangeListener() {
267 public void stateChanged(ChangeEvent e) {
268 expertLogin = (loginTabs.getSelectedComponent() == expertPanel);
269
270 if (expertLogin) {
271 configs.setSelectedIndex(0);
272
273 StringBuilder sb = new StringBuilder();
274 for (Map.Entry<String, String> parameter : createBasicSessionParameters().entrySet()) {
275 sb.append(parameter.getKey());
276 sb.append("=");
277 sb.append(parameter.getValue());
278 sb.append("\n");
279 }
280
281 sessionParameterTextArea.setText(sb.toString());
282 sessionParameterTextArea.setCaretPosition(0);
283 }
284 }
285 });
286
287 configs.addItemListener(new ItemListener() {
288 public void itemStateChanged(ItemEvent e) {
289 FileEntry fe = (FileEntry) e.getItem();
290
291 sessionParameterTextArea.setText(ClientHelper.readFileAndRemoveHeader(fe.getFile()));
292 sessionParameterTextArea.setCaretPosition(0);
293 }
294 });
295
296 ClientHelper.installEscapeBinding(this, getRootPane(), false);
297
298 setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
299 pack();
300 setLocationRelativeTo(null);
301 }
302
303 private JTextField createTextField(Container pane, String label) {
304 JTextField textField = new JTextField(60);
305 JLabel textLabel = new JLabel(label, JLabel.TRAILING);
306 textLabel.setLabelFor(textField);
307
308 pane.add(textLabel);
309 pane.add(textField);
310
311 return textField;
312 }
313
314 private JPasswordField createPasswordField(Container pane, String label) {
315 JPasswordField textField = new JPasswordField(60);
316 JLabel textLabel = new JLabel(label, JLabel.TRAILING);
317 textLabel.setLabelFor(textField);
318
319 pane.add(textLabel);
320 pane.add(textField);
321
322 return textField;
323 }
324
325 private void createBindingButtons(Container pane) {
326 JPanel bindingContainer = new JPanel();
327 bindingContainer.setLayout(new BoxLayout(bindingContainer, BoxLayout.LINE_AXIS));
328 char bc = System.getProperty(SYSPROP_BINDING, "atom").toLowerCase().charAt(0);
329 boolean atom = (bc == 'a');
330 boolean ws = (bc == 'w');
331 boolean browser = (bc == 'b');
332 bindingAtomButton = new JRadioButton("AtomPub", atom);
333 bindingWebServicesButton = new JRadioButton("Web Services", ws);
334 bindingBrowserButton = new JRadioButton("Browser (experimental)", browser);
335 ButtonGroup bindingGroup = new ButtonGroup();
336 bindingGroup.add(bindingAtomButton);
337 bindingGroup.add(bindingWebServicesButton);
338 bindingGroup.add(bindingBrowserButton);
339 bindingContainer.add(bindingAtomButton);
340 bindingContainer.add(Box.createRigidArea(new Dimension(10, 0)));
341 bindingContainer.add(bindingWebServicesButton);
342 bindingContainer.add(Box.createRigidArea(new Dimension(10, 0)));
343 bindingContainer.add(bindingBrowserButton);
344 JLabel bindingLabel = new JLabel("Binding:", JLabel.TRAILING);
345
346 pane.add(bindingLabel);
347 pane.add(bindingContainer);
348 }
349
350 private void createAuthenticationButtons(Container pane) {
351 JPanel authenticationContainer = new JPanel();
352 authenticationContainer.setLayout(new BoxLayout(authenticationContainer, BoxLayout.LINE_AXIS));
353 boolean standard = (System.getProperty(SYSPROP_AUTHENTICATION, "standard").toLowerCase().equals("standard"));
354 boolean ntlm = (System.getProperty(SYSPROP_AUTHENTICATION, "").toLowerCase().equals("ntlm"));
355 boolean none = !standard && !ntlm;
356 authenticationNoneButton = new JRadioButton("None", none);
357 authenticationStandardButton = new JRadioButton("Standard", standard);
358 authenticationNTLMButton = new JRadioButton("NTLM", ntlm);
359 ButtonGroup authenticationGroup = new ButtonGroup();
360 authenticationGroup.add(authenticationNoneButton);
361 authenticationGroup.add(authenticationStandardButton);
362 authenticationGroup.add(authenticationNTLMButton);
363 authenticationContainer.add(authenticationNoneButton);
364 authenticationContainer.add(Box.createRigidArea(new Dimension(10, 0)));
365 authenticationContainer.add(authenticationStandardButton);
366 authenticationContainer.add(Box.createRigidArea(new Dimension(10, 0)));
367 authenticationContainer.add(authenticationNTLMButton);
368 JLabel authenticatioLabel = new JLabel("Authentication:", JLabel.TRAILING);
369
370 pane.add(authenticatioLabel);
371 pane.add(authenticationContainer);
372 }
373
374 private void createCompressionButtons(Container pane) {
375 JPanel compressionContainer = new JPanel();
376 compressionContainer.setLayout(new BoxLayout(compressionContainer, BoxLayout.LINE_AXIS));
377 boolean compression = !(System.getProperty(SYSPROP_COMPRESSION, "on").equalsIgnoreCase("off"));
378 compressionOnButton = new JRadioButton("On", compression);
379 compressionOffButton = new JRadioButton("Off", !compression);
380 ButtonGroup compressionGroup = new ButtonGroup();
381 compressionGroup.add(compressionOnButton);
382 compressionGroup.add(compressionOffButton);
383 compressionContainer.add(compressionOnButton);
384 compressionContainer.add(Box.createRigidArea(new Dimension(10, 0)));
385 compressionContainer.add(compressionOffButton);
386 JLabel compressionLabel = new JLabel("Compression:", JLabel.TRAILING);
387
388 pane.add(compressionLabel);
389 pane.add(compressionContainer);
390 }
391
392 private void createClientCompressionButtons(Container pane) {
393 JPanel clientCompressionContainer = new JPanel();
394 clientCompressionContainer.setLayout(new BoxLayout(clientCompressionContainer, BoxLayout.LINE_AXIS));
395 boolean clientCompression = (System.getProperty(SYSPROP_CLIENTCOMPRESSION, "off").equalsIgnoreCase("on"));
396 clientCompressionOnButton = new JRadioButton("On", clientCompression);
397 clientCompressionOffButton = new JRadioButton("Off", !clientCompression);
398 ButtonGroup clientCompressionGroup = new ButtonGroup();
399 clientCompressionGroup.add(clientCompressionOnButton);
400 clientCompressionGroup.add(clientCompressionOffButton);
401 clientCompressionContainer.add(clientCompressionOnButton);
402 clientCompressionContainer.add(Box.createRigidArea(new Dimension(10, 0)));
403 clientCompressionContainer.add(clientCompressionOffButton);
404 JLabel clientCompressionLabel = new JLabel("Client Compression:", JLabel.TRAILING);
405
406 pane.add(clientCompressionLabel);
407 pane.add(clientCompressionContainer);
408 }
409
410 private void createCookieButtons(Container pane) {
411 JPanel cookiesContainer = new JPanel();
412 cookiesContainer.setLayout(new BoxLayout(cookiesContainer, BoxLayout.LINE_AXIS));
413 boolean cookies = (System.getProperty(SYSPROP_COOKIES, "on").equalsIgnoreCase("on"));
414 cookiesOnButton = new JRadioButton("On", cookies);
415 cookiesOffButton = new JRadioButton("Off", !cookies);
416 ButtonGroup cookiesGroup = new ButtonGroup();
417 cookiesGroup.add(cookiesOnButton);
418 cookiesGroup.add(cookiesOffButton);
419 cookiesContainer.add(cookiesOnButton);
420 cookiesContainer.add(Box.createRigidArea(new Dimension(10, 0)));
421 cookiesContainer.add(cookiesOffButton);
422 JLabel cookiesLabel = new JLabel("Cookies:", JLabel.TRAILING);
423
424 pane.add(cookiesLabel);
425 pane.add(cookiesContainer);
426 }
427
428 private JButton createButton(String title) {
429 JButton button = new JButton(title);
430 button.setPreferredSize(new Dimension(Short.MAX_VALUE, 30));
431 button.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
432 button.setAlignmentX(Component.CENTER_ALIGNMENT);
433
434 return button;
435 }
436
437 private void createRepositoryBox(Container pane) {
438 repositoryBox = new JComboBox();
439 repositoryBox.setEnabled(false);
440 repositoryBox.setRenderer(new RepositoryRenderer());
441 repositoryBox.setPreferredSize(new Dimension(Short.MAX_VALUE, 60));
442 repositoryBox.setAlignmentX(Component.CENTER_ALIGNMENT);
443
444 pane.add(repositoryBox);
445 }
446
447 private SpringLayout.Constraints getConstraintsForCell(int row, int col, Container parent, int cols) {
448 SpringLayout layout = (SpringLayout) parent.getLayout();
449 Component c = parent.getComponent(row * cols + col);
450 return layout.getConstraints(c);
451 }
452
453 private void makeCompactGrid(Container parent, int rows, int cols, int initialX, int initialY, int xPad, int yPad) {
454 SpringLayout layout = (SpringLayout) parent.getLayout();
455
456 Spring x = Spring.constant(initialX);
457 for (int c = 0; c < cols; c++) {
458 Spring width = Spring.constant(0);
459 for (int r = 0; r < rows; r++) {
460 width = Spring.max(width, getConstraintsForCell(r, c, parent, cols).getWidth());
461 }
462 for (int r = 0; r < rows; r++) {
463 SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent, cols);
464 constraints.setX(x);
465 constraints.setWidth(width);
466 }
467 x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
468 }
469
470 Spring y = Spring.constant(initialY);
471 for (int r = 0; r < rows; r++) {
472 Spring height = Spring.constant(0);
473 for (int c = 0; c < cols; c++) {
474 height = Spring.max(height, getConstraintsForCell(r, c, parent, cols).getHeight());
475 }
476 for (int c = 0; c < cols; c++) {
477 SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent, cols);
478 constraints.setY(y);
479 constraints.setHeight(height);
480 }
481 y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
482 }
483
484 layout.getConstraints(parent).setConstraint(SpringLayout.EAST, x);
485 }
486
487 private Map<String, String> createBasicSessionParameters() {
488 String url = urlField.getText();
489
490 BindingType binding = BindingType.ATOMPUB;
491 if (bindingWebServicesButton.isSelected()) {
492 binding = BindingType.WEBSERVICES;
493 } else if (bindingBrowserButton.isSelected()) {
494 binding = BindingType.BROWSER;
495 }
496
497 String username = usernameField.getText();
498 String password = new String(passwordField.getPassword());
499
500 ClientSession.Authentication authentication = ClientSession.Authentication.NONE;
501 if (authenticationStandardButton.isSelected()) {
502 authentication = ClientSession.Authentication.STANDARD;
503 } else if (authenticationNTLMButton.isSelected()) {
504 authentication = ClientSession.Authentication.NTLM;
505 }
506
507 return ClientSession.createSessionParameters(url, binding, username, password, authentication,
508 compressionOnButton.isSelected(), clientCompressionOnButton.isSelected(), cookiesOnButton.isSelected());
509 }
510
511 private Map<String, String> createExpertSessionParameters() {
512 Map<String, String> result = new HashMap<String, String>();
513
514 for (String line : sessionParameterTextArea.getText().split("\n")) {
515 line = line.trim();
516 if (line.startsWith("#") || (line.length() == 0)) {
517 continue;
518 }
519
520 int x = line.indexOf('=');
521 if (x < 0) {
522 result.put(line.trim(), "");
523 } else {
524 result.put(line.substring(0, x).trim(), line.substring(x + 1).trim());
525 }
526 }
527
528 return result;
529 }
530
531 public void createClientSession() {
532 if (expertLogin) {
533 clientSession = new ClientSession(createExpertSessionParameters());
534 } else {
535 clientSession = new ClientSession(createBasicSessionParameters());
536 }
537 }
538
539 public void showDialog() {
540 clientSession = null;
541 canceled = true;
542
543 repositoryBox.removeAllItems();
544 repositoryBox.setEnabled(false);
545 loginButton.setEnabled(false);
546 getRootPane().setDefaultButton(loadRepositoryButton);
547
548 setLocationRelativeTo(getOwner());
549 setVisible(true);
550 }
551
552 public void hideDialog() {
553 setVisible(false);
554 }
555
556 public ClientSession getClientSession() {
557 return clientSession;
558 }
559
560 public boolean isCanceled() {
561 return canceled;
562 }
563
564 static class RepositoryRenderer extends JPanel implements ListCellRenderer {
565 private static final long serialVersionUID = 1L;
566
567 private final JLabel nameLabel;
568 private final JLabel idLabel;
569 private final JLabel descriptionLabel;
570
571 public RepositoryRenderer() {
572 super();
573 setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
574 setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
575
576 Font labelFont = UIManager.getFont("Label.font");
577
578 nameLabel = new JLabel();
579 nameLabel.setFont(labelFont.deriveFont(Font.BOLD));
580 add(nameLabel);
581
582 idLabel = new JLabel();
583 add(idLabel);
584
585 descriptionLabel = new JLabel();
586 add(descriptionLabel);
587 }
588
589 @Override
590 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
591 boolean cellHasFocus) {
592 Repository repository = (Repository) value;
593
594 if (isSelected) {
595 setBackground(list.getSelectionBackground());
596 setForeground(list.getSelectionForeground());
597 } else {
598 setBackground(list.getBackground());
599 setForeground(list.getForeground());
600 }
601
602 if (repository == null) {
603 nameLabel.setText("");
604 idLabel.setText("");
605 descriptionLabel.setText("");
606 } else {
607 nameLabel.setText(repository.getName());
608 idLabel.setText(repository.getId());
609 descriptionLabel.setText(repository.getDescription());
610 }
611
612 return this;
613 }
614 }
615 }