minor
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / LoginDialog.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.ui.dialog;
12
13 import org.eclipse.jface.dialogs.Dialog;
14 import org.eclipse.jface.dialogs.MessageDialog;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.custom.CLabel;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Shell;
21 import org.eclipse.swt.widgets.Text;
22
23 import eu.etaxonomy.taxeditor.Messages;
24 import eu.etaxonomy.taxeditor.model.AbstractUtility;
25 import eu.etaxonomy.taxeditor.store.CdmStore;
26
27 /**
28 * TODO wrap in a LoginModule
29 * see: http://java.sun.com/j2se/1.5.0/docs/api/javax/security/auth/spi/LoginModule.html
30 *
31 * @author n.hoffmann
32 * @created 16.06.2009
33 * @version 1.0
34 */
35 public class LoginDialog extends Dialog {
36
37 private static Text text_password;
38 private static Text text_username;
39
40 private final String title;
41
42 public LoginDialog(Shell parentShell) {
43 super(parentShell);
44 title = Messages.LoginDialog_LOGIN;
45 }
46
47
48 /** {@inheritDoc} */
49 @Override
50 protected Control createDialogArea(Composite parent) {
51 Composite composite = (Composite) super.createDialogArea(parent);
52 //add controls to composite as necessary
53
54
55 // Label for the heading
56 final CLabel titleLabel = new CLabel(composite, SWT.NONE);
57 titleLabel.setText(Messages.LoginDialog_USER_LOGIN);
58
59 // Label for the username
60 final CLabel userNameLabel = new CLabel(composite, SWT.NONE);
61 userNameLabel.setText(Messages.LoginDialog_USER_NAME);
62
63 // Textfield for the username
64 text_username = new Text(composite, SWT.BORDER);
65 text_username.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
66
67 // Label for the password
68 final CLabel passwordLabel = new CLabel(composite, SWT.NONE);
69 passwordLabel.setText(Messages.LoginDialog_PASSWORD);
70
71 // Textfield for the password
72 text_password = new Text(composite, SWT.PASSWORD | SWT.BORDER);
73 text_password.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
74
75
76 return composite;
77 }
78
79 /** {@inheritDoc} */
80 @Override
81 protected void configureShell(Shell shell) {
82 super.configureShell(shell);
83 if (title != null) {
84 shell.setText(title);
85 }
86 }
87
88 /** {@inheritDoc} */
89 @Override
90 protected void okPressed() {
91 String username = text_username.getText();
92 String password = text_password.getText();
93
94 boolean result = CdmStore.getLoginManager().authenticate(username, password);
95
96 if(result){
97 super.okPressed();
98 }
99
100 }
101
102 @Override
103 protected void cancelPressed() {
104 if(CdmStore.getLoginManager().getAuthenticatedUser() != null){
105 super.cancelPressed();
106 }else{
107 // if there is no active user and the current user chooses to cancel, we close the connection
108 boolean result = MessageDialog.openConfirm(getShell(), Messages.LoginDialog_REALLY_CANCEL, Messages.LoginDialog_CANCEL_MESSAGE);
109
110 if(result){
111 CdmStore.close(AbstractUtility.getMonitor());
112 super.cancelPressed();
113 }
114 }
115 }
116 }