KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jsch > internal > ui > KeyboardInteractiveDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * Atsuhiko Yamanaka, JCraft,Inc. - initial API and implementation.
10  * IBM Corporation - ongoing maintenance
11  *******************************************************************************/

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 /**
29  * A dialog for keyboard-interactive authentication for the ssh2 connection.
30  */

31 public class KeyboardInteractiveDialog extends TrayDialog {
32   // widgets
33
private Text[] texts;
34   protected Image keyLockImage;
35   protected Button allowCachingButton;
36   protected Text usernameField;
37
38   protected String JavaDoc userName;
39   protected String JavaDoc domain;
40   protected String JavaDoc destination;
41   protected String JavaDoc name;
42   protected String JavaDoc instruction;
43   protected String JavaDoc lang;
44   protected String JavaDoc[] prompt;
45   protected boolean[] echo;
46   private String JavaDoc message;
47   private String JavaDoc[] result;
48   protected boolean allowCaching=false;
49   
50   private boolean isPasswordAuth=false;
51
52
53   /**
54    * Creates a new KeyboardInteractiveDialog.
55    *
56    * @param parentShell the parent shell
57    * @param location
58    * @param destination
59    * @param name the name
60    * @param userName
61    * @param instruction the instruction
62    * @param prompt the titles for text fields
63    * @param echo '*' should be used or not
64    */

65   public KeyboardInteractiveDialog(Shell parentShell,
66                    String JavaDoc location,
67                    String JavaDoc destination,
68                    String JavaDoc name,
69                    String JavaDoc userName,
70                    String JavaDoc instruction,
71                    String JavaDoc[] 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 JavaDoc[] { 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   /**
92    * @see Window#configureShell
93    */

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         // set F1 help
103
PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell,
104                 IHelpContextIds.KEYBOARD_INTERACTIVE_DIALOG);
105     }
106   /**
107      * @see Window#create
108      */

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   /**
122      * @see Dialog#createDialogArea
123      */

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   /**
230    * Creates the three widgets that represent the user name entry area.
231    *
232    * @param parent the parent of the widgets
233    */

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   /**
245      * Creates the widgets that represent the entry area.
246      *
247      * @param parent
248      * the parent of the widgets
249      */

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   /**
266    * Returns the entered values, or null
267    * if the user canceled.
268    *
269    * @return the entered values
270    */

271   public String JavaDoc[] getResult() {
272     return result;
273   }
274   
275   /**
276    * Returns <code>true</code> if the save password checkbox was selected.
277    * @return <code>true</code> if the save password checkbox was selected and <code>false</code>
278    * otherwise.
279    */

280   public boolean getAllowCaching(){
281     return allowCaching;
282   }
283   
284   /**
285    * Notifies that the ok button of this dialog has been pressed.
286    * <p>
287    * The default implementation of this framework method sets
288    * this dialog's return code to <code>Window.OK</code>
289    * and closes the dialog. Subclasses may override.
290    * </p>
291    */

292   protected void okPressed() {
293     result=new String JavaDoc[prompt.length];
294     for(int i=0; i<texts.length; i++){
295       result[i]=texts[i].getText();
296     }
297     super.okPressed();
298   }
299   /**
300    * Notifies that the cancel button of this dialog has been pressed.
301    * <p>
302    * The default implementation of this framework method sets
303    * this dialog's return code to <code>Window.CANCEL</code>
304    * and closes the dialog. Subclasses may override.
305    * </p>
306    */

307   protected void cancelPressed() {
308     result=null;
309     super.cancelPressed();
310   }
311     
312   /* (non-Javadoc)
313    * @see org.eclipse.jface.dialogs.Dialog#close()
314    */

315   public boolean close(){
316     if(keyLockImage!=null){
317       keyLockImage.dispose();
318     }
319     return super.close();
320   }
321   
322   /**
323    * Guesses if this dialog is used for password authentication.
324    * @param prompt prompts for keyboard-interactive authentication method.
325    * @return <code>true</code> if this dialog is used for password authentication.
326    */

327   static boolean isPasswordAuth(String JavaDoc[] prompt) {
328         return prompt != null && prompt.length == 1
329                 && prompt[0].trim().equalsIgnoreCase("password:"); //$NON-NLS-1$
330
}
331 }
332
Popular Tags