1 12 package org.eclipse.jsch.internal.ui; 13 14 import org.eclipse.jface.dialogs.*; 15 import org.eclipse.jface.dialogs.Dialog; 16 import org.eclipse.jface.resource.ImageDescriptor; 17 import org.eclipse.jface.window.Window; 18 import org.eclipse.osgi.util.NLS; 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.events.SelectionAdapter; 21 import org.eclipse.swt.events.SelectionEvent; 22 import org.eclipse.swt.graphics.Image; 23 import org.eclipse.swt.layout.GridData; 24 import org.eclipse.swt.layout.GridLayout; 25 import org.eclipse.swt.widgets.*; 26 import org.eclipse.ui.PlatformUI; 27 28 31 public class KeyboardInteractiveDialog extends TrayDialog { 32 private Text[] texts; 34 protected Image keyLockImage; 35 protected Button allowCachingButton; 36 protected Text usernameField; 37 38 protected String userName; 39 protected String domain; 40 protected String destination; 41 protected String name; 42 protected String instruction; 43 protected String lang; 44 protected String [] prompt; 45 protected boolean[] echo; 46 private String message; 47 private String [] result; 48 protected boolean allowCaching=false; 49 50 private boolean isPasswordAuth=false; 51 52 53 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 super(parentShell); 74 this.domain=location; 75 this.destination=destination; 76 this.name=name; 77 this.userName=userName; 78 this.instruction=instruction; 79 this.prompt=prompt; 80 this.echo=echo; 81 if (name!=null && name.length()>0) { 82 this.message=NLS.bind(Messages.KeyboardInteractiveDialog_0, new String [] { destination, name }); 83 } else { 84 this.message=NLS.bind(Messages.KeyboardInteractiveDialog_1, destination); 85 } 86 87 if(KeyboardInteractiveDialog.isPasswordAuth(prompt)){ 88 isPasswordAuth=true; 89 } 90 } 91 94 protected void configureShell(Shell newShell) { 95 super.configureShell(newShell); 96 if (isPasswordAuth) { 97 newShell.setText(Messages.KeyboardInteractiveDialog_2); 98 } else { 99 newShell.setText(message); 100 } 101 102 PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, 104 IHelpContextIds.KEYBOARD_INTERACTIVE_DIALOG); 105 } 106 109 public void create() { 110 super.create(); 111 112 if (isPasswordAuth && usernameField != null && userName != null) { 113 usernameField.setText(userName); 114 usernameField.setEditable(false); 115 } 116 117 if (texts.length > 0) { 118 texts[0].setFocus(); 119 } 120 } 121 124 protected Control createDialogArea(Composite parent) { 125 Composite top = new Composite(parent, SWT.NONE); 126 GridLayout layout = new GridLayout(); 127 layout.numColumns = 2; 128 129 top.setLayout(layout); 130 top.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 131 132 Composite imageComposite = new Composite(top, SWT.NONE); 133 layout = new GridLayout(); 134 imageComposite.setLayout(layout); 135 imageComposite.setLayoutData(new GridData(GridData.FILL_VERTICAL)); 136 137 Composite main = new Composite(top, SWT.NONE); 138 layout = new GridLayout(); 139 layout.numColumns = 3; 140 main.setLayout(layout); 141 main.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 142 143 Label imageLabel = new Label(imageComposite, SWT.NONE); 144 keyLockImage = ImageDescriptor.createFromURL(JSchUIPlugin.getPlugin().getImageUrl(IUIConstants.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(Messages.KeyboardInteractiveDialog_3); 163 data = new GridData(); 164 d.setLayoutData(data); 165 Label label = new Label(main, SWT.WRAP); 166 167 label.setText(domain); 168 169 data = new GridData(GridData.FILL_HORIZONTAL 170 | GridData.GRAB_HORIZONTAL); 171 data.horizontalSpan = 2; 172 data.widthHint = 300; 173 label.setLayoutData(data); 174 } 175 176 if (instruction != null && instruction.length() > 0) { 177 Label label = new Label(main, SWT.WRAP); 178 label.setText(instruction); 179 data = new GridData(GridData.FILL_HORIZONTAL 180 | GridData.GRAB_HORIZONTAL); 181 data.horizontalSpan = 3; 182 data.widthHint = 300; 183 label.setLayoutData(data); 184 } 185 186 if (isPasswordAuth) { 187 createUsernameFields(main); 188 } 189 190 createPasswordFields(main); 191 192 if (isPasswordAuth & domain != null) { 193 allowCachingButton = new Button(main, SWT.CHECK); 194 allowCachingButton.setText(Messages.KeyboardInteractiveDialog_4); 195 data = new GridData(GridData.FILL_HORIZONTAL 196 | GridData.GRAB_HORIZONTAL); 197 data.horizontalSpan = 3; 198 allowCachingButton.setLayoutData(data); 199 allowCachingButton.addSelectionListener(new SelectionAdapter() { 200 public void widgetSelected(SelectionEvent e) { 201 allowCaching = allowCachingButton.getSelection(); 202 } 203 }); 204 Composite warningComposite = new Composite(main, SWT.NONE); 205 layout = new GridLayout(); 206 layout.numColumns = 2; 207 layout.marginHeight = 0; 208 layout.marginHeight = 0; 209 warningComposite.setLayout(layout); 210 data = new GridData(GridData.FILL_HORIZONTAL); 211 data.horizontalSpan = 3; 212 warningComposite.setLayoutData(data); 213 Label warningLabel = new Label(warningComposite, SWT.NONE); 214 warningLabel.setImage(getImage(DLG_IMG_MESSAGE_WARNING)); 215 warningLabel.setLayoutData(new GridData( 216 GridData.VERTICAL_ALIGN_BEGINNING 217 | GridData.HORIZONTAL_ALIGN_BEGINNING)); 218 Label warningText = new Label(warningComposite, SWT.WRAP); 219 warningText.setText(Messages.KeyboardInteractiveDialog_5); 220 data = new GridData(GridData.FILL_HORIZONTAL); 221 data.widthHint = 300; 222 warningText.setLayoutData(data); 223 } 224 225 Dialog.applyDialogFont(parent); 226 return main; 227 } 228 229 234 protected void createUsernameFields(Composite parent){ 235 new Label(parent, SWT.NONE).setText(Messages.KeyboardInteractiveDialog_6); 236 237 usernameField=new Text(parent, SWT.BORDER); 238 GridData data=new GridData(GridData.FILL_HORIZONTAL); 239 data.horizontalSpan=2; 240 data.widthHint=convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH); 241 usernameField.setLayoutData(data); 242 } 243 244 250 protected void createPasswordFields(Composite parent) { 251 texts=new Text[prompt.length]; 252 253 for(int i=0; i<prompt.length; i++){ 254 new Label(parent, SWT.NONE).setText(prompt[i]); 255 texts[i]=new Text(parent, SWT.BORDER|SWT.PASSWORD); 256 GridData data=new GridData(GridData.FILL_HORIZONTAL); 257 data.horizontalSpan=2; 258 data.widthHint=convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH); 259 texts[i].setLayoutData(data); 260 if(!echo[i]){ 261 texts[i].setEchoChar('*'); 262 } 263 } 264 } 265 271 public String [] getResult() { 272 return result; 273 } 274 275 280 public boolean getAllowCaching(){ 281 return allowCaching; 282 } 283 284 292 protected void okPressed() { 293 result=new String [prompt.length]; 294 for(int i=0; i<texts.length; i++){ 295 result[i]=texts[i].getText(); 296 } 297 super.okPressed(); 298 } 299 307 protected void cancelPressed() { 308 result=null; 309 super.cancelPressed(); 310 } 311 312 315 public boolean close(){ 316 if(keyLockImage!=null){ 317 keyLockImage.dispose(); 318 } 319 return super.close(); 320 } 321 322 327 static boolean isPasswordAuth(String [] prompt) { 328 return prompt != null && prompt.length == 1 329 && prompt[0].trim().equalsIgnoreCase("password:"); } 331 } 332 | Popular Tags |