KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > 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.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 /**
30  * A dialog for keyboard-interactive authentication for the ssh2 connection.
31  */

32 public class KeyboardInteractiveDialog extends TrayDialog {
33   // widgets
34
private Text[] texts;
35   protected Image keyLockImage;
36   protected Button allowCachingButton;
37   protected Text usernameField;
38
39   protected String JavaDoc userName;
40   protected String JavaDoc domain;
41   protected String JavaDoc destination;
42   protected String JavaDoc name;
43   protected String JavaDoc instruction;
44   protected String JavaDoc lang;
45   protected String JavaDoc[] prompt;
46   protected boolean[] echo;
47   private String JavaDoc message;
48   private String JavaDoc[] result;
49   protected boolean allowCaching=false;
50   private boolean cachingDialog=false;
51   
52   private boolean isPasswordAuth=false;
53
54
55   /**
56    * Creates a new KeyboardInteractiveDialog.
57    *
58    * @param parentShell the parent shell
59    * @param destication the location
60    * @param name the name
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                    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 JavaDoc[] { destination+(name!=null && name.length()>0 ? ": "+name : "") }); //NON-NLS-1$ //$NON-NLS-1$ //$NON-NLS-2$
85

86     if(KeyboardInteractiveDialog.isPasswordAuth(prompt)){
87         isPasswordAuth=true;
88       }
89   }
90   /**
91    * @see Window#configureShell
92    */

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

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

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 JavaDoc[] { 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   /**
232    * Creates the three widgets that represent the user name entry area.
233    *
234    * @param parent the parent of the widgets
235    */

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

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

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

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

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

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

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

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