]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui/src/org/argeo/security/ui/dialogs/DefaultLoginDialog.java
New project conventions (builds)
[lgpl/argeo-commons.git] / org.argeo.security.ui / src / org / argeo / security / ui / dialogs / DefaultLoginDialog.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.security.ui.dialogs;
17
18 import javax.security.auth.callback.Callback;
19 import javax.security.auth.callback.CallbackHandler;
20 import javax.security.auth.callback.NameCallback;
21 import javax.security.auth.callback.PasswordCallback;
22 import javax.security.auth.callback.TextOutputCallback;
23
24 import org.argeo.security.ui.SecurityUiPlugin;
25 import org.argeo.util.LocaleCallback;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.ModifyEvent;
28 import org.eclipse.swt.events.ModifyListener;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.events.SelectionListener;
31 import org.eclipse.swt.graphics.Point;
32 import org.eclipse.swt.graphics.Rectangle;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Combo;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Control;
38 import org.eclipse.swt.widgets.Display;
39 import org.eclipse.swt.widgets.Label;
40 import org.eclipse.swt.widgets.Shell;
41 import org.eclipse.swt.widgets.Text;
42
43 /** Default authentication dialog, to be used as {@link CallbackHandler}. */
44 public class DefaultLoginDialog extends AbstractLoginDialog {
45 public DefaultLoginDialog() {
46 this(SecurityUiPlugin.display.get().getActiveShell());
47 }
48
49 public DefaultLoginDialog(Shell parentShell) {
50 super(parentShell);
51 }
52
53 protected Point getInitialSize() {
54 return new Point(350, 180);
55 }
56
57 @Override
58 protected Control createContents(Composite parent) {
59 Control control = super.createContents(parent);
60 parent.pack();
61
62 // Move the dialog to the center of the top level shell.
63 Rectangle shellBounds;
64 if (Display.getCurrent().getActiveShell() != null) // RCP
65 shellBounds = Display.getCurrent().getActiveShell().getBounds();
66 else
67 shellBounds = Display.getCurrent().getBounds();// RAP
68 Point dialogSize = parent.getSize();
69 int x = shellBounds.x + (shellBounds.width - dialogSize.x) / 2;
70 int y = shellBounds.y + (shellBounds.height - dialogSize.y) / 2;
71 parent.setLocation(x, y);
72 return control;
73 }
74
75 protected Control createDialogArea(Composite parent) {
76 Composite dialogarea = (Composite) super.createDialogArea(parent);
77 Composite composite = new Composite(dialogarea, SWT.NONE);
78 composite.setLayout(new GridLayout(2, false));
79 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
80 createCallbackHandlers(composite);
81 // parent.pack();
82 return composite;
83 }
84
85 private void createCallbackHandlers(Composite composite) {
86 Callback[] callbacks = getCallbacks();
87 for (int i = 0; i < callbacks.length; i++) {
88 Callback callback = callbacks[i];
89 if (callback instanceof TextOutputCallback) {
90 createLabelTextoutputHandler(composite,
91 (TextOutputCallback) callback);
92 } else if (callback instanceof NameCallback) {
93 createNameHandler(composite, (NameCallback) callback);
94 } else if (callback instanceof PasswordCallback) {
95 createPasswordHandler(composite, (PasswordCallback) callback);
96 } else if (callback instanceof LocaleCallback) {
97 createLocaleHandler(composite, (LocaleCallback) callback);
98 }
99 }
100 }
101
102 private void createPasswordHandler(Composite composite,
103 final PasswordCallback callback) {
104 Label label = new Label(composite, SWT.NONE);
105 label.setText(callback.getPrompt());
106 final Text passwordText = new Text(composite, SWT.SINGLE | SWT.LEAD
107 | SWT.PASSWORD | SWT.BORDER);
108 passwordText
109 .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
110 passwordText.addModifyListener(new ModifyListener() {
111
112 public void modifyText(ModifyEvent event) {
113 // FIXME use getTextChars() in Eclipse 3.7
114 callback.setPassword(passwordText.getText().toCharArray());
115 }
116 });
117 }
118
119 private void createLocaleHandler(Composite composite,
120 final LocaleCallback callback) {
121 String[] labels = callback.getSupportedLocalesLabels();
122 if (labels.length == 0)
123 return;
124 Label label = new Label(composite, SWT.NONE);
125 label.setText(callback.getPrompt());
126
127 final Combo combo = new Combo(composite, SWT.READ_ONLY);
128 combo.setItems(labels);
129 combo.select(callback.getDefaultIndex());
130 combo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
131 combo.addSelectionListener(new SelectionListener() {
132 @Override
133 public void widgetSelected(SelectionEvent e) {
134 callback.setSelectedIndex(combo.getSelectionIndex());
135 }
136
137 @Override
138 public void widgetDefaultSelected(SelectionEvent e) {
139 }
140 });
141 }
142
143 private void createNameHandler(Composite composite,
144 final NameCallback callback) {
145 Label label = new Label(composite, SWT.NONE);
146 label.setText(callback.getPrompt());
147 final Text text = new Text(composite, SWT.SINGLE | SWT.LEAD
148 | SWT.BORDER);
149 if (callback.getDefaultName() != null) {
150 // set default value, if provided
151 text.setText(callback.getDefaultName());
152 callback.setName(callback.getDefaultName());
153 }
154 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
155 text.addModifyListener(new ModifyListener() {
156
157 public void modifyText(ModifyEvent event) {
158 callback.setName(text.getText());
159 }
160 });
161 }
162
163 private void createLabelTextoutputHandler(Composite composite,
164 final TextOutputCallback callback) {
165 Label label = new Label(composite, SWT.NONE);
166 label.setText(callback.getMessage());
167 GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
168 data.horizontalSpan = 2;
169 label.setLayoutData(data);
170 // TODO: find a way to pass this information
171 // int messageType = callback.getMessageType();
172 // int dialogMessageType = IMessageProvider.NONE;
173 // switch (messageType) {
174 // case TextOutputCallback.INFORMATION:
175 // dialogMessageType = IMessageProvider.INFORMATION;
176 // break;
177 // case TextOutputCallback.WARNING:
178 // dialogMessageType = IMessageProvider.WARNING;
179 // break;
180 // case TextOutputCallback.ERROR:
181 // dialogMessageType = IMessageProvider.ERROR;
182 // break;
183 // }
184 // setMessage(callback.getMessage(), dialogMessageType);
185 }
186
187 public void internalHandle() {
188 }
189 }