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