1 11 package org.eclipse.ui.internal.net.auth; 12 13 import org.eclipse.jface.dialogs.Dialog; 14 import org.eclipse.jface.dialogs.IDialogConstants; 15 import org.eclipse.osgi.util.NLS; 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.layout.GridData; 18 import org.eclipse.swt.layout.GridLayout; 19 import org.eclipse.swt.widgets.*; 20 import org.eclipse.ui.PlatformUI; 21 import org.eclipse.ui.internal.net.NetUIMessages; 22 23 26 public class UserValidationDialog extends Dialog { 27 protected Text usernameField; 28 protected Text passwordField; 29 30 protected String host; 31 protected String message; 32 protected Authentication userAuthentication = null; 33 41 public static Authentication getAuthentication(final String host, 42 final String message) { 43 class UIOperation implements Runnable { 44 public Authentication authentication; 45 public void run() { 46 authentication = UserValidationDialog.askForAuthentication( 47 host, message); 48 } 49 } 50 51 UIOperation uio = new UIOperation(); 52 if (Display.getCurrent() != null) { 53 uio.run(); 54 } else { 55 Display.getDefault().syncExec(uio); 56 } 57 return uio.authentication; 58 } 59 65 protected static Authentication askForAuthentication(String host, 66 String message) { 67 UserValidationDialog ui = new UserValidationDialog(null, host, message); 68 ui.open(); 69 return ui.getAuthentication(); 70 } 71 77 protected UserValidationDialog(Shell parentShell, String host, 78 String message) { 79 super(parentShell); 80 this.host = host; 81 this.message = message; 82 setBlockOnOpen(true); 83 } 84 86 protected void configureShell(Shell newShell) { 87 super.configureShell(newShell); 88 newShell.setText(NetUIMessages.UserValidationDialog_0); 89 } 90 92 public void create() { 93 super.create(); 94 usernameField.selectAll(); 96 usernameField.setFocus(); 97 } 98 100 protected Control createDialogArea(Composite parent) { 101 Composite main = new Composite(parent, SWT.NONE); 102 GridLayout layout = new GridLayout(); 103 layout.numColumns = 3; 104 main.setLayout(layout); 105 main.setLayoutData(new GridData(GridData.FILL_BOTH)); 106 107 Label label = new Label(main, SWT.WRAP); 108 String text = NLS.bind(NetUIMessages.UserValidationDialog_1, host); 109 text += "\n\n" + message; label.setText(text); 111 GridData data = new GridData(GridData.FILL_HORIZONTAL); 112 data.horizontalSpan = 3; 113 label.setLayoutData(data); 114 115 createUsernameFields(main); 116 createPasswordFields(main); 117 PlatformUI.getWorkbench().getHelpSystem().setHelp(main, 118 "org.eclipse.update.ui.UserValidationDialog"); return main; 120 } 121 124 protected void createPasswordFields(Composite parent) { 125 new Label(parent, SWT.NONE).setText(NetUIMessages.UserValidationDialog_2); 126 127 passwordField = new Text(parent, SWT.BORDER | SWT.PASSWORD); 128 GridData data = new GridData(GridData.FILL_HORIZONTAL); 129 data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH); 130 passwordField.setLayoutData(data); 131 132 new Label(parent, SWT.NONE); } 134 137 protected void createUsernameFields(Composite parent) { 138 new Label(parent, SWT.NONE).setText(NetUIMessages.UserValidationDialog_3); 139 140 usernameField = new Text(parent, SWT.BORDER); 141 GridData data = new GridData(GridData.FILL_HORIZONTAL); 142 data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH); 143 usernameField.setLayoutData(data); 144 145 new Label(parent, SWT.NONE); } 147 152 public Authentication getAuthentication() { 153 return userAuthentication; 154 } 155 158 protected void okPressed() { 159 userAuthentication = new Authentication(usernameField.getText(), 160 passwordField.getText()); 161 super.okPressed(); 162 } 163 164 } 165 | Popular Tags |