KickJava   Java API By Example, From Geeks To Geeks.

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


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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jsch.internal.ui;
12
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.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.ui.PlatformUI;
26
27 /**
28  * A dialog for prompting for a user name and password
29  */

30 public class UserValidationDialog extends TrayDialog {
31     // widgets
32
protected Text usernameField;
33     protected Text passwordField;
34     protected Button allowCachingButton;
35
36     protected String JavaDoc domain;
37     protected String JavaDoc defaultUsername;
38     protected String JavaDoc password = null;
39     protected boolean allowCaching = false;
40     protected Image keyLockImage;
41     
42     // whether or not the user name can be changed
43
protected boolean isUsernameMutable = true;
44     protected String JavaDoc username = null;
45     protected String JavaDoc message = null;
46
47     /**
48      * Creates a new UserValidationDialog.
49      *
50      * @param parentShell the parent shell
51      * @param location the location
52      * @param defaultName the default user name
53      * @param message a message to display to the user
54      */

55     public UserValidationDialog(Shell parentShell, String JavaDoc location, String JavaDoc defaultName, String JavaDoc message) {
56         super(parentShell);
57         setShellStyle(getShellStyle() | SWT.RESIZE);
58         this.defaultUsername = defaultName;
59         this.domain = location;
60         this.message = message;
61     }
62     /**
63      * @see Window#configureShell
64      */

65     protected void configureShell(Shell newShell) {
66         super.configureShell(newShell);
67         newShell.setText(Messages.UserValidationDialog_0);
68         // set F1 help
69
PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IHelpContextIds.USER_VALIDATION_DIALOG);
70     }
71     /**
72      * @see Window#create
73      */

74     public void create() {
75         super.create();
76         // add some default values
77
usernameField.setText(defaultUsername);
78     
79         if (isUsernameMutable) {
80             // give focus to user name field
81
usernameField.selectAll();
82             usernameField.setFocus();
83         } else {
84             usernameField.setEditable(false);
85             passwordField.setFocus();
86         }
87     }
88     
89     /**
90      * @see Dialog#createDialogArea
91      */

92     protected Control createDialogArea(Composite parent) {
93         Composite top = new Composite(parent, SWT.NONE);
94         GridLayout layout = new GridLayout();
95         layout.numColumns = 2;
96         
97         top.setLayout(layout);
98         top.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
99     
100         Composite imageComposite = new Composite(top, SWT.NONE);
101         layout = new GridLayout();
102         imageComposite.setLayout(layout);
103         imageComposite.setLayoutData(new GridData(GridData.FILL_VERTICAL));
104
105         Composite main = new Composite(top, SWT.NONE);
106         layout = new GridLayout();
107         layout.numColumns = 3;
108         main.setLayout(layout);
109         main.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
110         
111         Label imageLabel = new Label(imageComposite, SWT.NONE);
112         keyLockImage = ImageDescriptor.createFromURL(JSchUIPlugin.getPlugin().getImageUrl(IUIConstants.IMG_KEY_LOCK)).createImage();
113         imageLabel.setImage(keyLockImage);
114         GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
115         imageLabel.setLayoutData(data);
116         
117         if (message != null) {
118             Label messageLabel = new Label(main, SWT.WRAP);
119             messageLabel.setText(message);
120             data = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
121             data.horizontalSpan = 3;
122             data.widthHint = 300;
123             messageLabel.setLayoutData(data);
124         }
125         if (domain != null) {
126             Label d = new Label(main, SWT.WRAP);
127             d.setText(Messages.UserValidationDialog_1);
128             data = new GridData();
129             d.setLayoutData(data);
130             Label label = new Label(main, SWT.WRAP);
131             label.setText(domain);
132             data = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
133             data.horizontalSpan = 2;
134             data.widthHint = 300;
135             label.setLayoutData(data);
136         }
137         createUsernameFields(main);
138         createPasswordFields(main);
139         
140         if(domain != null) {
141             allowCachingButton = new Button(main, SWT.CHECK);
142             allowCachingButton.setText(Messages.UserValidationDialog_2);
143             data = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
144             data.horizontalSpan = 3;
145             allowCachingButton.setLayoutData(data);
146             allowCachingButton.addSelectionListener(new SelectionAdapter() {
147                 public void widgetSelected(SelectionEvent e) {
148                     allowCaching = allowCachingButton.getSelection();
149                 }
150             });
151             Composite warningComposite = new Composite(main, SWT.NONE);
152             layout = new GridLayout();
153             layout.numColumns = 2;
154             layout.marginHeight = 0;
155             layout.marginHeight = 0;
156             warningComposite.setLayout(layout);
157             data = new GridData(GridData.FILL_HORIZONTAL);
158             data.horizontalSpan = 3;
159             warningComposite.setLayoutData(data);
160             Label warningLabel = new Label(warningComposite, SWT.NONE);
161             warningLabel.setImage(getImage(DLG_IMG_MESSAGE_WARNING));
162             warningLabel.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_BEGINNING));
163             Label warningText = new Label(warningComposite, SWT.WRAP);
164             warningText.setText(Messages.UserValidationDialog_3);
165             data = new GridData(GridData.FILL_HORIZONTAL);
166             data.widthHint = 300;
167             warningText.setLayoutData(data);
168         }
169         
170         Dialog.applyDialogFont(parent);
171         
172         return main;
173     }
174     
175     /**
176      * Creates the three widgets that represent the password entry area.
177      *
178      * @param parent the parent of the widgets
179      */

180     protected void createPasswordFields(Composite parent) {
181         new Label(parent, SWT.NONE).setText(Messages.UserValidationDialog_4);
182         
183         passwordField = new Text(parent, SWT.BORDER | SWT.PASSWORD);
184         GridData data = new GridData(GridData.FILL_HORIZONTAL);
185         data.horizontalSpan = 2;
186         data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH);
187         passwordField.setLayoutData(data);
188     }
189     /**
190      * Creates the three widgets that represent the user name entry area.
191      *
192      * @param parent the parent of the widgets
193      */

194     protected void createUsernameFields(Composite parent) {
195         new Label(parent, SWT.NONE).setText(Messages.UserValidationDialog_5);
196         
197         usernameField = new Text(parent, SWT.BORDER);
198         GridData data = new GridData(GridData.FILL_HORIZONTAL);
199         data.horizontalSpan = 2;
200         data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH);
201         usernameField.setLayoutData(data);
202     }
203     
204     /**
205      * Returns the password entered by the user, or null
206      * if the user canceled.
207      *
208      * @return the entered password
209      */

210     public String JavaDoc getPassword() {
211         return password;
212     }
213     
214     /**
215      * Returns the user name entered by the user, or null
216      * if the user canceled.
217      *
218      * @return the entered user name
219      */

220     public String JavaDoc getUsername() {
221         return username;
222     }
223     
224     /**
225      * Returns <code>true</code> if the save password checkbox was selected.
226      * @return <code>true</code> if the save password checkbox was selected and <code>false</code>
227      * otherwise.
228      */

229     public boolean getAllowCaching() {
230         return allowCaching;
231     }
232     
233     /**
234      * Notifies that the ok button of this dialog has been pressed.
235      * <p>
236      * The default implementation of this framework method sets
237      * this dialog's return code to <code>Window.OK</code>
238      * and closes the dialog. Subclasses may override.
239      * </p>
240      */

241     protected void okPressed() {
242         password = passwordField.getText();
243         username = usernameField.getText();
244     
245         super.okPressed();
246     }
247     
248     protected void cancelPressed(){
249       password = null;
250       username = null;
251       super.cancelPressed();
252     }
253     
254     /**
255      * Sets whether or not the user name field should be mutable.
256      * This method must be called before create(), otherwise it
257      * will be ignored.
258      *
259      * @param value whether the user name is mutable
260      */

261     public void setUsernameMutable(boolean value) {
262         isUsernameMutable = value;
263     }
264     
265     /* (non-Javadoc)
266      * @see org.eclipse.jface.dialogs.Dialog#close()
267      */

268     public boolean close() {
269         if(keyLockImage != null) {
270             keyLockImage.dispose();
271         }
272         return super.close();
273     }
274 }
275
Popular Tags