1 12 package org.eclipse.team.internal.ccvs.ui; 13 14 import org.eclipse.jface.dialogs.*; 15 import org.eclipse.jface.dialogs.Dialog; 16 import org.eclipse.jface.window.Window; 17 import org.eclipse.osgi.util.NLS; 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.events.SelectionAdapter; 20 import org.eclipse.swt.events.SelectionEvent; 21 import org.eclipse.swt.graphics.Image; 22 import org.eclipse.swt.layout.GridData; 23 import org.eclipse.swt.layout.GridLayout; 24 import org.eclipse.swt.widgets.*; 25 import org.eclipse.team.internal.ui.ITeamUIImages; 26 import org.eclipse.team.ui.TeamImages; 27 import org.eclipse.ui.PlatformUI; 28 29 32 public class KeyboardInteractiveDialog extends TrayDialog { 33 private Text[] texts; 35 protected Image keyLockImage; 36 protected Button allowCachingButton; 37 protected Text usernameField; 38 39 protected String userName; 40 protected String domain; 41 protected String destination; 42 protected String name; 43 protected String instruction; 44 protected String lang; 45 protected String [] prompt; 46 protected boolean[] echo; 47 private String message; 48 private String [] result; 49 protected boolean allowCaching=false; 50 private boolean cachingDialog=false; 51 52 private boolean isPasswordAuth=false; 53 54 55 65 public KeyboardInteractiveDialog(Shell parentShell, 66 String location, 67 String destination, 68 String name, 69 String userName, 70 String instruction, 71 String [] prompt, 72 boolean[] echo, 73 boolean cachingDialog){ 74 super(parentShell); 75 this.domain=location; 76 this.destination=destination; 77 this.name=name; 78 this.userName=userName; 79 this.instruction=instruction; 80 this.prompt=prompt; 81 this.echo=echo; 82 this.cachingDialog=cachingDialog; 83 84 this.message=NLS.bind(CVSUIMessages.KeyboradInteractiveDialog_message, new String [] { destination+(name!=null && name.length()>0 ? ": "+name : "") }); 86 if(KeyboardInteractiveDialog.isPasswordAuth(prompt)){ 87 isPasswordAuth=true; 88 } 89 } 90 93 protected void configureShell(Shell newShell) { 94 super.configureShell(newShell); 95 if (isPasswordAuth) { 96 newShell.setText(CVSUIMessages.UserValidationDialog_required); 97 } else { 98 newShell.setText(message); 99 } 100 101 PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, 103 IHelpContextIds.KEYBOARD_INTERACTIVE_DIALOG); 104 } 105 108 public void create() { 109 super.create(); 110 111 if (isPasswordAuth && usernameField != null && userName != null) { 112 usernameField.setText(userName); 113 usernameField.setEditable(false); 114 } 115 116 if (texts.length > 0) { 117 texts[0].setFocus(); 118 } 119 } 120 123 protected Control createDialogArea(Composite parent) { 124 Composite top = new Composite(parent, SWT.NONE); 125 GridLayout layout = new GridLayout(); 126 layout.numColumns = 2; 127 128 top.setLayout(layout); 129 top.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 130 131 Composite imageComposite = new Composite(top, SWT.NONE); 132 layout = new GridLayout(); 133 imageComposite.setLayout(layout); 134 imageComposite.setLayoutData(new GridData(GridData.FILL_VERTICAL)); 135 136 Composite main = new Composite(top, SWT.NONE); 137 layout = new GridLayout(); 138 layout.numColumns = 3; 139 main.setLayout(layout); 140 main.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 141 142 Label imageLabel = new Label(imageComposite, SWT.NONE); 143 keyLockImage = TeamImages 144 .getImageDescriptor(ITeamUIImages.IMG_KEY_LOCK).createImage(); 145 imageLabel.setImage(keyLockImage); 146 GridData data = new GridData(GridData.FILL_HORIZONTAL 147 | GridData.GRAB_HORIZONTAL); 148 imageLabel.setLayoutData(data); 149 150 if (message != null) { 151 Label messageLabel = new Label(main, SWT.WRAP); 152 messageLabel.setText(message); 153 data = new GridData(GridData.FILL_HORIZONTAL 154 | GridData.GRAB_HORIZONTAL); 155 data.horizontalSpan = 3; 156 data.widthHint = 300; 157 messageLabel.setLayoutData(data); 158 } 159 160 if (domain != null) { 161 Label d = new Label(main, SWT.WRAP); 162 d.setText(CVSUIMessages.UserValidationDialog_5); 163 data = new GridData(); 164 d.setLayoutData(data); 165 Label label = new Label(main, SWT.WRAP); 166 167 label.setText(NLS.bind( 168 CVSUIMessages.UserValidationDialog_labelUser, 169 new String [] { domain })); 170 171 data = new GridData(GridData.FILL_HORIZONTAL 172 | GridData.GRAB_HORIZONTAL); 173 data.horizontalSpan = 2; 174 data.widthHint = 300; 175 label.setLayoutData(data); 176 } 177 178 if (instruction != null && instruction.length() > 0) { 179 Label label = new Label(main, SWT.WRAP); 180 label.setText(instruction); 181 data = new GridData(GridData.FILL_HORIZONTAL 182 | GridData.GRAB_HORIZONTAL); 183 data.horizontalSpan = 3; 184 data.widthHint = 300; 185 label.setLayoutData(data); 186 } 187 188 if (isPasswordAuth) { 189 createUsernameFields(main); 190 } 191 192 createPasswordFields(main); 193 194 if (cachingDialog && isPasswordAuth) { 195 allowCachingButton = new Button(main, SWT.CHECK); 196 allowCachingButton.setText(CVSUIMessages.UserValidationDialog_6); 197 data = new GridData(GridData.FILL_HORIZONTAL 198 | GridData.GRAB_HORIZONTAL); 199 data.horizontalSpan = 3; 200 allowCachingButton.setLayoutData(data); 201 allowCachingButton.addSelectionListener(new SelectionAdapter() { 202 public void widgetSelected(SelectionEvent e) { 203 allowCaching = allowCachingButton.getSelection(); 204 } 205 }); 206 Composite warningComposite = new Composite(main, SWT.NONE); 207 layout = new GridLayout(); 208 layout.numColumns = 2; 209 layout.marginHeight = 0; 210 layout.marginHeight = 0; 211 warningComposite.setLayout(layout); 212 data = new GridData(GridData.FILL_HORIZONTAL); 213 data.horizontalSpan = 3; 214 warningComposite.setLayoutData(data); 215 Label warningLabel = new Label(warningComposite, SWT.NONE); 216 warningLabel.setImage(getImage(DLG_IMG_MESSAGE_WARNING)); 217 warningLabel.setLayoutData(new GridData( 218 GridData.VERTICAL_ALIGN_BEGINNING 219 | GridData.HORIZONTAL_ALIGN_BEGINNING)); 220 Label warningText = new Label(warningComposite, SWT.WRAP); 221 warningText.setText(CVSUIMessages.UserValidationDialog_7); 222 data = new GridData(GridData.FILL_HORIZONTAL); 223 data.widthHint = 300; 224 warningText.setLayoutData(data); 225 } 226 227 Dialog.applyDialogFont(parent); 228 return main; 229 } 230 231 236 protected void createUsernameFields(Composite parent){ 237 new Label(parent, SWT.NONE).setText(CVSUIMessages.UserValidationDialog_user); 238 239 usernameField=new Text(parent, SWT.BORDER); 240 GridData data=new GridData(GridData.FILL_HORIZONTAL); 241 data.horizontalSpan=2; 242 data.widthHint=convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH); 243 usernameField.setLayoutData(data); 244 } 245 246 252 protected void createPasswordFields(Composite parent) { 253 texts=new Text[prompt.length]; 254 255 for(int i=0; i<prompt.length; i++){ 256 new Label(parent, SWT.NONE).setText(prompt[i]); 257 texts[i]=new Text(parent, SWT.BORDER|SWT.PASSWORD); 258 GridData data=new GridData(GridData.FILL_HORIZONTAL); 259 data.horizontalSpan=2; 260 data.widthHint=convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH); 261 texts[i].setLayoutData(data); 262 if(!echo[i]){ 263 texts[i].setEchoChar('*'); 264 } 265 } 266 } 267 273 public String [] getResult() { 274 return result; 275 } 276 277 282 public boolean getAllowCaching(){ 283 return allowCaching; 284 } 285 286 294 protected void okPressed() { 295 result=new String [prompt.length]; 296 for(int i=0; i<texts.length; i++){ 297 result[i]=texts[i].getText(); 298 } 299 super.okPressed(); 300 } 301 309 protected void cancelPressed() { 310 result=null; 311 super.cancelPressed(); 312 } 313 314 317 public boolean close(){ 318 if(keyLockImage!=null){ 319 keyLockImage.dispose(); 320 } 321 return super.close(); 322 } 323 324 329 static boolean isPasswordAuth(String [] prompt) { 330 return prompt != null && prompt.length == 1 331 && prompt[0].trim().equalsIgnoreCase("password:"); } 333 } 334 | Popular Tags |