package org.eclipse.update.internal.ui.security;
import org.eclipse.jface.dialogs.*;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.*;
import org.eclipse.update.internal.ui.*;
public class UserValidationDialog extends Dialog {
protected Text usernameField;
protected Text passwordField;
protected String host;
protected String message;
protected Authentication userAuthentication = null;
public static Authentication getAuthentication(final String host,
final String message) {
class UIOperation implements Runnable {
public Authentication authentication;
public void run() {
authentication = UserValidationDialog.askForAuthentication(
host, message);
}
}
UIOperation uio = new UIOperation();
if (Display.getCurrent() != null) {
uio.run();
} else {
Display.getDefault().syncExec(uio);
}
return uio.authentication;
}
protected static Authentication askForAuthentication(String host,
String message) {
UserValidationDialog ui = new UserValidationDialog(null, host, message);
ui.open();
return ui.getAuthentication();
}
protected UserValidationDialog(Shell parentShell, String host,
String message) {
super(parentShell);
this.host = host;
this.message = message;
setBlockOnOpen(true);
}
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setText(UpdateUIMessages.UserVerificationDialog_PasswordRequired);
}
public void create() {
super.create();
usernameField.selectAll();
usernameField.setFocus();
}
protected Control createDialogArea(Composite parent) {
Composite main = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout();
layout.numColumns = 3;
main.setLayout(layout);
main.setLayoutData(new GridData(GridData.FILL_BOTH));
Label label = new Label(main, SWT.WRAP);
String text = UpdateUIMessages.UserVerificationDialog_ConnectTo + host;
text += "\n\n" + message; label.setText(text);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
data.horizontalSpan = 3;
label.setLayoutData(data);
createUsernameFields(main);
createPasswordFields(main);
PlatformUI.getWorkbench().getHelpSystem().setHelp(main,
"org.eclipse.update.ui.UserValidationDialog"); return main;
}
protected void createPasswordFields(Composite parent) {
new Label(parent, SWT.NONE).setText(UpdateUIMessages.UserVerificationDialog_Password);
passwordField = new Text(parent, SWT.BORDER | SWT.PASSWORD);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH);
passwordField.setLayoutData(data);
new Label(parent, SWT.NONE); }
protected void createUsernameFields(Composite parent) {
new Label(parent, SWT.NONE).setText(UpdateUIMessages.UserVerificationDialog_UserName);
usernameField = new Text(parent, SWT.BORDER);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH);
usernameField.setLayoutData(data);
new Label(parent, SWT.NONE); }
public Authentication getAuthentication() {
return userAuthentication;
}
protected void okPressed() {
userAuthentication = new Authentication(usernameField.getText(),
passwordField.getText());
super.okPressed();
}
}