X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=security%2Fplugins%2Forg.argeo.security.ui%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fsecurity%2Fui%2Fdialogs%2FDefaultLoginDialog.java;h=97340892d0c62eba0cfdc230bbe82d44fbba996d;hb=00474cf92c05359177aba1768bd2ef95a310afaf;hp=d00e961fb3acd4a221c3071e13f26cbd3ad40638;hpb=a7a5f4db586128a9bb2c171ee819eb3eb19f80aa;p=lgpl%2Fargeo-commons.git diff --git a/security/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/dialogs/DefaultLoginDialog.java b/security/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/dialogs/DefaultLoginDialog.java index d00e961fb..97340892d 100644 --- a/security/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/dialogs/DefaultLoginDialog.java +++ b/security/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/dialogs/DefaultLoginDialog.java @@ -1,17 +1,37 @@ +/* + * Copyright (C) 2007-2012 Mathieu Baudier + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.argeo.security.ui.dialogs; import javax.security.auth.callback.Callback; +import javax.security.auth.callback.CallbackHandler; import javax.security.auth.callback.NameCallback; import javax.security.auth.callback.PasswordCallback; import javax.security.auth.callback.TextOutputCallback; -import org.eclipse.jface.dialogs.IMessageProvider; +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; @@ -19,10 +39,11 @@ import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; +/** Default authentication dialog, to be used as {@link CallbackHandler}. */ public class DefaultLoginDialog extends AbstractLoginDialog { public DefaultLoginDialog() { - this(Display.getDefault().getActiveShell()); + this(Display.getCurrent().getActiveShell()); } protected DefaultLoginDialog(Shell parentShell) { @@ -30,18 +51,34 @@ public class DefaultLoginDialog extends AbstractLoginDialog { } protected Point getInitialSize() { - return new Point(300, 250); + 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; + 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; + parent.setLocation(x, y); + return control; } protected Control createDialogArea(Composite parent) { Composite dialogarea = (Composite) super.createDialogArea(parent); - // dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, - // true)); Composite composite = new Composite(dialogarea, SWT.NONE); composite.setLayout(new GridLayout(2, false)); composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); createCallbackHandlers(composite); - parent.pack(); + // parent.pack(); return composite; } @@ -50,12 +87,14 @@ public class DefaultLoginDialog extends AbstractLoginDialog { for (int i = 0; i < callbacks.length; i++) { Callback callback = callbacks[i]; if (callback instanceof TextOutputCallback) { - createTextoutputHandler(composite, + createLabelTextoutputHandler(composite, (TextOutputCallback) callback); } else if (callback instanceof NameCallback) { createNameHandler(composite, (NameCallback) callback); } else if (callback instanceof PasswordCallback) { createPasswordHandler(composite, (PasswordCallback) callback); + } else if (callback instanceof LocaleCallback) { + createLocaleHandler(composite, (LocaleCallback) callback); } } } @@ -71,17 +110,47 @@ public class DefaultLoginDialog extends AbstractLoginDialog { passwordText.addModifyListener(new ModifyListener() { public void modifyText(ModifyEvent event) { + // FIXME use getTextChars() in Eclipse 3.7 callback.setPassword(passwordText.getText().toCharArray()); } }); } + 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() { @@ -91,22 +160,28 @@ public class DefaultLoginDialog extends AbstractLoginDialog { }); } - private void createTextoutputHandler(Composite composite, - TextOutputCallback callback) { - int messageType = callback.getMessageType(); - int dialogMessageType = IMessageProvider.NONE; - switch (messageType) { - case TextOutputCallback.INFORMATION: - dialogMessageType = IMessageProvider.INFORMATION; - break; - case TextOutputCallback.WARNING: - dialogMessageType = IMessageProvider.WARNING; - break; - case TextOutputCallback.ERROR: - dialogMessageType = IMessageProvider.ERROR; - break; - } - setMessage(callback.getMessage(), dialogMessageType); + private void createLabelTextoutputHandler(Composite composite, + final TextOutputCallback callback) { + Label label = new Label(composite, SWT.NONE); + label.setText(callback.getMessage()); + GridData data = new GridData(SWT.FILL, SWT.FILL, true, true); + data.horizontalSpan = 2; + label.setLayoutData(data); + // TODO: find a way to pass this information + // int messageType = callback.getMessageType(); + // int dialogMessageType = IMessageProvider.NONE; + // switch (messageType) { + // case TextOutputCallback.INFORMATION: + // dialogMessageType = IMessageProvider.INFORMATION; + // break; + // case TextOutputCallback.WARNING: + // dialogMessageType = IMessageProvider.WARNING; + // break; + // case TextOutputCallback.ERROR: + // dialogMessageType = IMessageProvider.ERROR; + // break; + // } + // setMessage(callback.getMessage(), dialogMessageType); } public void internalHandle() {