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
.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
;
44 /** Default authentication dialog, to be used as {@link CallbackHandler}. */
45 public class DefaultLoginDialog
extends AbstractLoginDialog
{
46 private static final long serialVersionUID
= -8551827590693035734L;
48 public DefaultLoginDialog() {
49 this(SecurityUiPlugin
.display
.get().getActiveShell());
52 public DefaultLoginDialog(Shell parentShell
) {
56 protected Point
getInitialSize() {
57 return new Point(350, 180);
61 protected Control
createContents(Composite parent
) {
62 Control control
= super.createContents(parent
);
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();
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
);
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
);
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());
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
);
115 .setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
116 passwordText
.addModifyListener(new ModifyListener() {
117 private static final long serialVersionUID
= -7099363995047686732L;
119 public void modifyText(ModifyEvent event
) {
120 // FIXME use getTextChars() in Eclipse 3.7
121 callback
.setPassword(passwordText
.getText().toCharArray());
126 private void createLocaleHandler(Composite composite
,
127 final LocaleCallback callback
) {
128 String
[] labels
= callback
.getSupportedLocalesLabels();
129 if (labels
.length
== 0)
131 Label label
= new Label(composite
, SWT
.NONE
);
132 label
.setText(callback
.getPrompt());
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;
142 public void widgetSelected(SelectionEvent e
) {
143 callback
.setSelectedIndex(combo
.getSelectionIndex());
147 public void widgetDefaultSelected(SelectionEvent e
) {
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
158 if (callback
.getDefaultName() != null) {
159 // set default value, if provided
160 text
.setText(callback
.getDefaultName());
161 callback
.setName(callback
.getDefaultName());
163 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
164 text
.addModifyListener(new ModifyListener() {
165 private static final long serialVersionUID
= 7300032545287292973L;
167 public void modifyText(ModifyEvent event
) {
168 callback
.setName(text
.getText());
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;
187 // case TextOutputCallback.WARNING:
188 // dialogMessageType = IMessageProvider.WARNING;
190 // case TextOutputCallback.ERROR:
191 // dialogMessageType = IMessageProvider.ERROR;
194 // setMessage(callback.getMessage(), dialogMessageType);
197 public void internalHandle() {