]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/dialogs/DefaultLoginDialog.java
Update license headers
[lgpl/argeo-commons.git] / security / plugins / org.argeo.security.ui / src / main / java / org / argeo / security / ui / dialogs / DefaultLoginDialog.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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.eclipse.swt.SWT;
25 import org.eclipse.swt.events.ModifyEvent;
26 import org.eclipse.swt.events.ModifyListener;
27 import org.eclipse.swt.graphics.Point;
28 import org.eclipse.swt.graphics.Rectangle;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Display;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Shell;
36 import org.eclipse.swt.widgets.Text;
37
38 /** Default authentication dialog, to be used as {@link CallbackHandler}. */
39 public class DefaultLoginDialog extends AbstractLoginDialog {
40
41 public DefaultLoginDialog() {
42 this(Display.getCurrent().getActiveShell());
43 }
44
45 protected DefaultLoginDialog(Shell parentShell) {
46 super(parentShell);
47 }
48
49 protected Point getInitialSize() {
50 return new Point(300, 180);
51 }
52
53 @Override
54 protected Control createContents(Composite parent) {
55 Control control = super.createContents(parent);
56 parent.pack();
57 // Move the dialog to the center of the top level shell.
58 Rectangle shellBounds = Display.getCurrent().getBounds();
59 Point dialogSize = parent.getSize();
60 int x = shellBounds.x + (shellBounds.width - dialogSize.x) / 2;
61 int y = shellBounds.y + (shellBounds.height - dialogSize.y) / 2;
62 parent.setLocation(x, y);
63 return control;
64 }
65
66 protected Control createDialogArea(Composite parent) {
67 Composite dialogarea = (Composite) super.createDialogArea(parent);
68 Composite composite = new Composite(dialogarea, SWT.NONE);
69 composite.setLayout(new GridLayout(2, false));
70 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
71 createCallbackHandlers(composite);
72 // parent.pack();
73 return composite;
74 }
75
76 private void createCallbackHandlers(Composite composite) {
77 Callback[] callbacks = getCallbacks();
78 for (int i = 0; i < callbacks.length; i++) {
79 Callback callback = callbacks[i];
80 if (callback instanceof TextOutputCallback) {
81 createLabelTextoutputHandler(composite,
82 (TextOutputCallback) callback);
83 } else if (callback instanceof NameCallback) {
84 createNameHandler(composite, (NameCallback) callback);
85 } else if (callback instanceof PasswordCallback) {
86 createPasswordHandler(composite, (PasswordCallback) callback);
87 }
88 }
89 }
90
91 private void createPasswordHandler(Composite composite,
92 final PasswordCallback callback) {
93 Label label = new Label(composite, SWT.NONE);
94 label.setText(callback.getPrompt());
95 final Text passwordText = new Text(composite, SWT.SINGLE | SWT.LEAD
96 | SWT.PASSWORD | SWT.BORDER);
97 passwordText
98 .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
99 passwordText.addModifyListener(new ModifyListener() {
100
101 public void modifyText(ModifyEvent event) {
102 // FIXME use getTextChars() in Eclipse 3.7
103 callback.setPassword(passwordText.getText().toCharArray());
104 }
105 });
106 }
107
108 private void createNameHandler(Composite composite,
109 final NameCallback callback) {
110 Label label = new Label(composite, SWT.NONE);
111 label.setText(callback.getPrompt());
112 final Text text = new Text(composite, SWT.SINGLE | SWT.LEAD
113 | SWT.BORDER);
114 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
115 text.addModifyListener(new ModifyListener() {
116
117 public void modifyText(ModifyEvent event) {
118 callback.setName(text.getText());
119 }
120 });
121 }
122
123 private void createLabelTextoutputHandler(Composite composite,
124 final TextOutputCallback callback) {
125 Label label = new Label(composite, SWT.NONE);
126 label.setText(callback.getMessage());
127 GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
128 data.horizontalSpan = 2;
129 label.setLayoutData(data);
130 // TODO: find a way to pass this information
131 // int messageType = callback.getMessageType();
132 // int dialogMessageType = IMessageProvider.NONE;
133 // switch (messageType) {
134 // case TextOutputCallback.INFORMATION:
135 // dialogMessageType = IMessageProvider.INFORMATION;
136 // break;
137 // case TextOutputCallback.WARNING:
138 // dialogMessageType = IMessageProvider.WARNING;
139 // break;
140 // case TextOutputCallback.ERROR:
141 // dialogMessageType = IMessageProvider.ERROR;
142 // break;
143 // }
144 // setMessage(callback.getMessage(), dialogMessageType);
145 }
146
147 public void internalHandle() {
148 }
149 }