2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.security
.ui
.dialogs
;
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
;
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
;
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());
50 public DefaultLoginDialog(Shell parentShell
) {
54 protected Point
getInitialSize() {
55 return new Point(350, 180);
59 protected Control
createContents(Composite parent
) {
60 Control control
= super.createContents(parent
);
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();
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
);
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
);
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());
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
);
113 .setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
114 passwordText
.addModifyListener(new ModifyListener() {
116 public void modifyText(ModifyEvent event
) {
117 // FIXME use getTextChars() in Eclipse 3.7
118 callback
.setPassword(passwordText
.getText().toCharArray());
123 private void createLocaleHandler(Composite composite
,
124 final LocaleCallback callback
) {
125 String
[] labels
= callback
.getSupportedLocalesLabels();
126 if (labels
.length
== 0)
128 Label label
= new Label(composite
, SWT
.NONE
);
129 label
.setText(callback
.getPrompt());
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() {
137 public void widgetSelected(SelectionEvent e
) {
138 callback
.setSelectedIndex(combo
.getSelectionIndex());
142 public void widgetDefaultSelected(SelectionEvent e
) {
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
153 if (callback
.getDefaultName() != null) {
154 // set default value, if provided
155 text
.setText(callback
.getDefaultName());
156 callback
.setName(callback
.getDefaultName());
158 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
159 text
.addModifyListener(new ModifyListener() {
161 public void modifyText(ModifyEvent event
) {
162 callback
.setName(text
.getText());
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;
181 // case TextOutputCallback.WARNING:
182 // dialogMessageType = IMessageProvider.WARNING;
184 // case TextOutputCallback.ERROR:
185 // dialogMessageType = IMessageProvider.ERROR;
188 // setMessage(callback.getMessage(), dialogMessageType);
191 public void internalHandle() {