]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui/src/org/argeo/security/ui/dialogs/DefaultLoginDialog.java
Remove dependency with workbench
[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.core.BundleContextCallback;
25 import org.argeo.security.ui.SecurityUiPlugin;
26 import org.argeo.util.LocaleCallback;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.ModifyEvent;
29 import org.eclipse.swt.events.ModifyListener;
30 import org.eclipse.swt.events.SelectionEvent;
31 import org.eclipse.swt.events.SelectionListener;
32 import org.eclipse.swt.graphics.Point;
33 import org.eclipse.swt.graphics.Rectangle;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Combo;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.swt.widgets.Display;
40 import org.eclipse.swt.widgets.Label;
41 import org.eclipse.swt.widgets.Shell;
42 import org.eclipse.swt.widgets.Text;
43
44 /** Default authentication dialog, to be used as {@link CallbackHandler}. */
45 public class DefaultLoginDialog extends AbstractLoginDialog {
46 public DefaultLoginDialog() {
47 this(SecurityUiPlugin.display.get().getActiveShell());
48 }
49
50 public DefaultLoginDialog(Shell parentShell) {
51 super(parentShell);
52 }
53
54 protected Point getInitialSize() {
55 return new Point(350, 180);
56 }
57
58 @Override
59 protected Control createContents(Composite parent) {
60 Control control = super.createContents(parent);
61 parent.pack();
62
63 // Move the dialog to the center of the top level shell.
64 Rectangle shellBounds;
65 if (Display.getCurrent().getActiveShell() != null) // RCP
66 shellBounds = Display.getCurrent().getActiveShell().getBounds();
67 else
68 shellBounds = Display.getCurrent().getBounds();// RAP
69 Point dialogSize = parent.getSize();
70 int x = shellBounds.x + (shellBounds.width - dialogSize.x) / 2;
71 int y = shellBounds.y + (shellBounds.height - dialogSize.y) / 2;
72 parent.setLocation(x, y);
73 return control;
74 }
75
76 protected Control createDialogArea(Composite parent) {
77 Composite dialogarea = (Composite) super.createDialogArea(parent);
78 Composite composite = new Composite(dialogarea, SWT.NONE);
79 composite.setLayout(new GridLayout(2, false));
80 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
81 createCallbackHandlers(composite);
82 // parent.pack();
83 return composite;
84 }
85
86 private void createCallbackHandlers(Composite composite) {
87 Callback[] callbacks = getCallbacks();
88 for (int i = 0; i < callbacks.length; i++) {
89 Callback callback = callbacks[i];
90 if (callback instanceof TextOutputCallback) {
91 createLabelTextoutputHandler(composite,
92 (TextOutputCallback) callback);
93 } else if (callback instanceof NameCallback) {
94 createNameHandler(composite, (NameCallback) callback);
95 } else if (callback instanceof PasswordCallback) {
96 createPasswordHandler(composite, (PasswordCallback) callback);
97 } else if (callback instanceof LocaleCallback) {
98 createLocaleHandler(composite, (LocaleCallback) callback);
99 } else if (callback instanceof BundleContextCallback) {
100 ((BundleContextCallback) callback)
101 .setBundleContext(SecurityUiPlugin.getBundleContext());
102 }
103 }
104 }
105
106 private void createPasswordHandler(Composite composite,
107 final PasswordCallback callback) {
108 Label label = new Label(composite, SWT.NONE);
109 label.setText(callback.getPrompt());
110 final Text passwordText = new Text(composite, SWT.SINGLE | SWT.LEAD
111 | SWT.PASSWORD | SWT.BORDER);
112 passwordText
113 .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
114 passwordText.addModifyListener(new ModifyListener() {
115
116 public void modifyText(ModifyEvent event) {
117 // FIXME use getTextChars() in Eclipse 3.7
118 callback.setPassword(passwordText.getText().toCharArray());
119 }
120 });
121 }
122
123 private void createLocaleHandler(Composite composite,
124 final LocaleCallback callback) {
125 String[] labels = callback.getSupportedLocalesLabels();
126 if (labels.length == 0)
127 return;
128 Label label = new Label(composite, SWT.NONE);
129 label.setText(callback.getPrompt());
130
131 final Combo combo = new Combo(composite, SWT.READ_ONLY);
132 combo.setItems(labels);
133 combo.select(callback.getDefaultIndex());
134 combo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
135 combo.addSelectionListener(new SelectionListener() {
136 @Override
137 public void widgetSelected(SelectionEvent e) {
138 callback.setSelectedIndex(combo.getSelectionIndex());
139 }
140
141 @Override
142 public void widgetDefaultSelected(SelectionEvent e) {
143 }
144 });
145 }
146
147 private void createNameHandler(Composite composite,
148 final NameCallback callback) {
149 Label label = new Label(composite, SWT.NONE);
150 label.setText(callback.getPrompt());
151 final Text text = new Text(composite, SWT.SINGLE | SWT.LEAD
152 | SWT.BORDER);
153 if (callback.getDefaultName() != null) {
154 // set default value, if provided
155 text.setText(callback.getDefaultName());
156 callback.setName(callback.getDefaultName());
157 }
158 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
159 text.addModifyListener(new ModifyListener() {
160
161 public void modifyText(ModifyEvent event) {
162 callback.setName(text.getText());
163 }
164 });
165 }
166
167 private void createLabelTextoutputHandler(Composite composite,
168 final TextOutputCallback callback) {
169 Label label = new Label(composite, SWT.NONE);
170 label.setText(callback.getMessage());
171 GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
172 data.horizontalSpan = 2;
173 label.setLayoutData(data);
174 // TODO: find a way to pass this information
175 // int messageType = callback.getMessageType();
176 // int dialogMessageType = IMessageProvider.NONE;
177 // switch (messageType) {
178 // case TextOutputCallback.INFORMATION:
179 // dialogMessageType = IMessageProvider.INFORMATION;
180 // break;
181 // case TextOutputCallback.WARNING:
182 // dialogMessageType = IMessageProvider.WARNING;
183 // break;
184 // case TextOutputCallback.ERROR:
185 // dialogMessageType = IMessageProvider.ERROR;
186 // break;
187 // }
188 // setMessage(callback.getMessage(), dialogMessageType);
189 }
190
191 public void internalHandle() {
192 }
193 }