/*
- * Copyright (C) 2007-2012 Mathieu Baudier
+ * Copyright (C) 2007-2012 Argeo GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.TextOutputCallback;
+import org.argeo.security.ui.SecurityUiPlugin;
+import org.argeo.util.LocaleCallback;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
/** Default authentication dialog, to be used as {@link CallbackHandler}. */
public class DefaultLoginDialog extends AbstractLoginDialog {
-
public DefaultLoginDialog() {
- this(Display.getCurrent().getActiveShell());
+ this(SecurityUiPlugin.display.get().getActiveShell());
}
- protected DefaultLoginDialog(Shell parentShell) {
+ public DefaultLoginDialog(Shell parentShell) {
super(parentShell);
}
protected Point getInitialSize() {
- return new Point(300, 180);
+ return new Point(350, 180);
}
@Override
protected Control createContents(Composite parent) {
Control control = super.createContents(parent);
parent.pack();
+
// Move the dialog to the center of the top level shell.
- Rectangle shellBounds = Display.getCurrent().getBounds();
+ Rectangle shellBounds;
+ if (Display.getCurrent().getActiveShell() != null) // RCP
+ shellBounds = Display.getCurrent().getActiveShell().getBounds();
+ else
+ shellBounds = Display.getCurrent().getBounds();// RAP
Point dialogSize = parent.getSize();
int x = shellBounds.x + (shellBounds.width - dialogSize.x) / 2;
int y = shellBounds.y + (shellBounds.height - dialogSize.y) / 2;
createNameHandler(composite, (NameCallback) callback);
} else if (callback instanceof PasswordCallback) {
createPasswordHandler(composite, (PasswordCallback) callback);
+ } else if (callback instanceof LocaleCallback) {
+ createLocaleHandler(composite, (LocaleCallback) callback);
}
}
}
});
}
+ private void createLocaleHandler(Composite composite,
+ final LocaleCallback callback) {
+ String[] labels = callback.getSupportedLocalesLabels();
+ if (labels.length == 0)
+ return;
+ Label label = new Label(composite, SWT.NONE);
+ label.setText(callback.getPrompt());
+
+ final Combo combo = new Combo(composite, SWT.READ_ONLY);
+ combo.setItems(labels);
+ combo.select(callback.getDefaultIndex());
+ combo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+ combo.addSelectionListener(new SelectionListener() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ callback.setSelectedIndex(combo.getSelectionIndex());
+ }
+
+ @Override
+ public void widgetDefaultSelected(SelectionEvent e) {
+ }
+ });
+ }
+
private void createNameHandler(Composite composite,
final NameCallback callback) {
Label label = new Label(composite, SWT.NONE);
label.setText(callback.getPrompt());
final Text text = new Text(composite, SWT.SINGLE | SWT.LEAD
| SWT.BORDER);
+ if (callback.getDefaultName() != null) {
+ // set default value, if provided
+ text.setText(callback.getDefaultName());
+ callback.setName(callback.getDefaultName());
+ }
text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
text.addModifyListener(new ModifyListener() {