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
.util
.LocaleCallback
;
25 import org
.eclipse
.swt
.SWT
;
26 import org
.eclipse
.swt
.events
.ModifyEvent
;
27 import org
.eclipse
.swt
.events
.ModifyListener
;
28 import org
.eclipse
.swt
.events
.SelectionEvent
;
29 import org
.eclipse
.swt
.events
.SelectionListener
;
30 import org
.eclipse
.swt
.graphics
.Point
;
31 import org
.eclipse
.swt
.graphics
.Rectangle
;
32 import org
.eclipse
.swt
.layout
.GridData
;
33 import org
.eclipse
.swt
.layout
.GridLayout
;
34 import org
.eclipse
.swt
.widgets
.Combo
;
35 import org
.eclipse
.swt
.widgets
.Composite
;
36 import org
.eclipse
.swt
.widgets
.Control
;
37 import org
.eclipse
.swt
.widgets
.Display
;
38 import org
.eclipse
.swt
.widgets
.Label
;
39 import org
.eclipse
.swt
.widgets
.Shell
;
40 import org
.eclipse
.swt
.widgets
.Text
;
42 /** Default authentication dialog, to be used as {@link CallbackHandler}. */
43 public class DefaultLoginDialog
extends AbstractLoginDialog
{
45 public DefaultLoginDialog() {
46 this(Display
.getCurrent().getActiveShell());
49 protected DefaultLoginDialog(Shell parentShell
) {
53 protected Point
getInitialSize() {
54 return new Point(350, 180);
58 protected Control
createContents(Composite parent
) {
59 Control control
= super.createContents(parent
);
62 // Move the dialog to the center of the top level shell.
63 Rectangle shellBounds
;
64 if (Display
.getCurrent().getActiveShell() != null) // RCP
65 shellBounds
= Display
.getCurrent().getActiveShell().getBounds();
67 shellBounds
= Display
.getCurrent().getBounds();// RAP
68 Point dialogSize
= parent
.getSize();
69 int x
= shellBounds
.x
+ (shellBounds
.width
- dialogSize
.x
) / 2;
70 int y
= shellBounds
.y
+ (shellBounds
.height
- dialogSize
.y
) / 2;
71 parent
.setLocation(x
, y
);
75 protected Control
createDialogArea(Composite parent
) {
76 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
77 Composite composite
= new Composite(dialogarea
, SWT
.NONE
);
78 composite
.setLayout(new GridLayout(2, false));
79 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
80 createCallbackHandlers(composite
);
85 private void createCallbackHandlers(Composite composite
) {
86 Callback
[] callbacks
= getCallbacks();
87 for (int i
= 0; i
< callbacks
.length
; i
++) {
88 Callback callback
= callbacks
[i
];
89 if (callback
instanceof TextOutputCallback
) {
90 createLabelTextoutputHandler(composite
,
91 (TextOutputCallback
) callback
);
92 } else if (callback
instanceof NameCallback
) {
93 createNameHandler(composite
, (NameCallback
) callback
);
94 } else if (callback
instanceof PasswordCallback
) {
95 createPasswordHandler(composite
, (PasswordCallback
) callback
);
96 } else if (callback
instanceof LocaleCallback
) {
97 createLocaleHandler(composite
, (LocaleCallback
) callback
);
102 private void createPasswordHandler(Composite composite
,
103 final PasswordCallback callback
) {
104 Label label
= new Label(composite
, SWT
.NONE
);
105 label
.setText(callback
.getPrompt());
106 final Text passwordText
= new Text(composite
, SWT
.SINGLE
| SWT
.LEAD
107 | SWT
.PASSWORD
| SWT
.BORDER
);
109 .setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
110 passwordText
.addModifyListener(new ModifyListener() {
112 public void modifyText(ModifyEvent event
) {
113 // FIXME use getTextChars() in Eclipse 3.7
114 callback
.setPassword(passwordText
.getText().toCharArray());
119 private void createLocaleHandler(Composite composite
,
120 final LocaleCallback callback
) {
121 String
[] labels
= callback
.getSupportedLocalesLabels();
122 if (labels
.length
== 0)
124 Label label
= new Label(composite
, SWT
.NONE
);
125 label
.setText(callback
.getPrompt());
127 final Combo combo
= new Combo(composite
, SWT
.READ_ONLY
);
128 combo
.setItems(labels
);
129 combo
.select(callback
.getDefaultIndex());
130 combo
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
131 combo
.addSelectionListener(new SelectionListener() {
133 public void widgetSelected(SelectionEvent e
) {
134 callback
.setSelectedIndex(combo
.getSelectionIndex());
138 public void widgetDefaultSelected(SelectionEvent e
) {
143 private void createNameHandler(Composite composite
,
144 final NameCallback callback
) {
145 Label label
= new Label(composite
, SWT
.NONE
);
146 label
.setText(callback
.getPrompt());
147 final Text text
= new Text(composite
, SWT
.SINGLE
| SWT
.LEAD
149 if (callback
.getDefaultName() != null) {
150 // set default value, if provided
151 text
.setText(callback
.getDefaultName());
152 callback
.setName(callback
.getDefaultName());
154 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
155 text
.addModifyListener(new ModifyListener() {
157 public void modifyText(ModifyEvent event
) {
158 callback
.setName(text
.getText());
163 private void createLabelTextoutputHandler(Composite composite
,
164 final TextOutputCallback callback
) {
165 Label label
= new Label(composite
, SWT
.NONE
);
166 label
.setText(callback
.getMessage());
167 GridData data
= new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
168 data
.horizontalSpan
= 2;
169 label
.setLayoutData(data
);
170 // TODO: find a way to pass this information
171 // int messageType = callback.getMessageType();
172 // int dialogMessageType = IMessageProvider.NONE;
173 // switch (messageType) {
174 // case TextOutputCallback.INFORMATION:
175 // dialogMessageType = IMessageProvider.INFORMATION;
177 // case TextOutputCallback.WARNING:
178 // dialogMessageType = IMessageProvider.WARNING;
180 // case TextOutputCallback.ERROR:
181 // dialogMessageType = IMessageProvider.ERROR;
184 // setMessage(callback.getMessage(), dialogMessageType);
187 public void internalHandle() {