KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > 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.team.internal.ccvs.ui;
12
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 prompting for a user name and password
31  */

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

58     public UserValidationDialog(Shell parentShell, String JavaDoc location, String JavaDoc defaultName, String JavaDoc message) {
59         this(parentShell, location, defaultName, message, true);
60     }
61     
62     /**
63      * Creates a new UserValidationDialog.
64      *
65      * @param parentShell the parent shell
66      * @param location the location
67      * @param defaultName the default user name
68      * @param message a message to display to the user
69      * @param cachingCheckbox a flag to show the allowCachingButton
70      */

71     public UserValidationDialog(Shell parentShell, String JavaDoc location, String JavaDoc defaultName, String JavaDoc message, boolean cachingCheckbox) {
72         super(parentShell);
73         setShellStyle(getShellStyle() | SWT.RESIZE);
74         this.defaultUsername = defaultName;
75         this.domain = location;
76         this.message = message;
77         this.cachingCheckbox=cachingCheckbox;
78     }
79     
80     /**
81      * @see Window#configureShell
82      */

83     protected void configureShell(Shell newShell) {
84         super.configureShell(newShell);
85         newShell.setText(CVSUIMessages.UserValidationDialog_required);
86         // set F1 help
87
PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IHelpContextIds.USER_VALIDATION_DIALOG);
88     }
89     /**
90      * @see Window#create
91      */

92     public void create() {
93         super.create();
94         // add some default values
95
usernameField.setText(defaultUsername);
96     
97         if (isUsernameMutable) {
98             // give focus to user name field
99
usernameField.selectAll();
100             usernameField.setFocus();
101         } else {
102             usernameField.setEditable(false);
103             passwordField.setFocus();
104         }
105     }
106     
107     /**
108      * @see Dialog#createDialogArea
109      */

110     protected Control createDialogArea(Composite parent) {
111         Composite top = new Composite(parent, SWT.NONE);
112         GridLayout layout = new GridLayout();
113         layout.numColumns = 2;
114         
115         top.setLayout(layout);
116         top.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
117     
118         Composite imageComposite = new Composite(top, SWT.NONE);
119         layout = new GridLayout();
120         imageComposite.setLayout(layout);
121         imageComposite.setLayoutData(new GridData(GridData.FILL_VERTICAL));
122
123         Composite main = new Composite(top, SWT.NONE);
124         layout = new GridLayout();
125         layout.numColumns = 3;
126         main.setLayout(layout);
127         main.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
128         
129         Label imageLabel = new Label(imageComposite, SWT.NONE);
130         keyLockImage = TeamImages.getImageDescriptor(ITeamUIImages.IMG_KEY_LOCK).createImage();
131         imageLabel.setImage(keyLockImage);
132         GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
133         imageLabel.setLayoutData(data);
134         
135         if (message != null) {
136             Label messageLabel = new Label(main, SWT.WRAP);
137             messageLabel.setText(message);
138             data = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
139             data.horizontalSpan = 3;
140             data.widthHint = 300;
141             messageLabel.setLayoutData(data);
142         }
143         if (domain != null) {
144             Label d = new Label(main, SWT.WRAP);
145             d.setText(CVSUIMessages.UserValidationDialog_5);
146             data = new GridData();
147             d.setLayoutData(data);
148             Label label = new Label(main, SWT.WRAP);
149             if (isUsernameMutable) {
150                 label.setText(NLS.bind(CVSUIMessages.UserValidationDialog_labelUser, new String JavaDoc[] { domain }));
151             } else {
152                 label.setText(NLS.bind(CVSUIMessages.UserValidationDialog_labelPassword, (new Object JavaDoc[]{defaultUsername, domain})));
153             }
154             data = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
155             data.horizontalSpan = 2;
156             data.widthHint = 300;
157             label.setLayoutData(data);
158         }
159         createUsernameFields(main);
160         createPasswordFields(main);
161         
162         if(cachingCheckbox && domain != null) {
163             allowCachingButton = new Button(main, SWT.CHECK);
164             allowCachingButton.setText(CVSUIMessages.UserValidationDialog_6);
165             data = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
166             data.horizontalSpan = 3;
167             allowCachingButton.setLayoutData(data);
168             allowCachingButton.addSelectionListener(new SelectionAdapter() {
169                 public void widgetSelected(SelectionEvent e) {
170                     allowCaching = allowCachingButton.getSelection();
171                 }
172             });
173             Composite warningComposite = new Composite(main, SWT.NONE);
174             layout = new GridLayout();
175             layout.numColumns = 2;
176             layout.marginHeight = 0;
177             layout.marginHeight = 0;
178             warningComposite.setLayout(layout);
179             data = new GridData(GridData.FILL_HORIZONTAL);
180             data.horizontalSpan = 3;
181             warningComposite.setLayoutData(data);
182             Label warningLabel = new Label(warningComposite, SWT.NONE);
183             warningLabel.setImage(getImage(DLG_IMG_MESSAGE_WARNING));
184             warningLabel.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_BEGINNING));
185             Label warningText = new Label(warningComposite, SWT.WRAP);
186             warningText.setText(CVSUIMessages.UserValidationDialog_7);
187             data = new GridData(GridData.FILL_HORIZONTAL);
188             data.widthHint = 300;
189             warningText.setLayoutData(data);
190         }
191         
192         Dialog.applyDialogFont(parent);
193         
194         return main;
195     }
196     
197     /**
198      * Creates the three widgets that represent the password entry area.
199      *
200      * @param parent the parent of the widgets
201      */

202     protected void createPasswordFields(Composite parent) {
203         new Label(parent, SWT.NONE).setText(CVSUIMessages.UserValidationDialog_password);
204         
205         passwordField = new Text(parent, SWT.BORDER | SWT.PASSWORD);
206         GridData data = new GridData(GridData.FILL_HORIZONTAL);
207         data.horizontalSpan = 2;
208         data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH);
209         passwordField.setLayoutData(data);
210     }
211     /**
212      * Creates the three widgets that represent the user name entry area.
213      *
214      * @param parent the parent of the widgets
215      */

216     protected void createUsernameFields(Composite parent) {
217         new Label(parent, SWT.NONE).setText(CVSUIMessages.UserValidationDialog_user);
218         
219         usernameField = new Text(parent, SWT.BORDER);
220         GridData data = new GridData(GridData.FILL_HORIZONTAL);
221         data.horizontalSpan = 2;
222         data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH);
223         usernameField.setLayoutData(data);
224     }
225     
226     /**
227      * Returns the password entered by the user, or null
228      * if the user canceled.
229      *
230      * @return the entered password
231      */

232     public String JavaDoc getPassword() {
233         return password;
234     }
235     
236     /**
237      * Returns the user name entered by the user, or null
238      * if the user canceled.
239      *
240      * @return the entered user name
241      */

242     public String JavaDoc getUsername() {
243         return username;
244     }
245     
246     /**
247      * Returns <code>true</code> if the save password checkbox was selected.
248      * @return <code>true</code> if the save password checkbox was selected and <code>false</code>
249      * otherwise.
250      */

251     public boolean getAllowCaching() {
252         return allowCaching;
253     }
254     
255     /**
256      * Notifies that the ok button of this dialog has been pressed.
257      * <p>
258      * The default implementation of this framework method sets
259      * this dialog's return code to <code>Window.OK</code>
260      * and closes the dialog. Subclasses may override.
261      * </p>
262      */

263     protected void okPressed() {
264         password = passwordField.getText();
265         username = usernameField.getText();
266     
267         super.okPressed();
268     }
269     /**
270      * Sets whether or not the user name field should be mutable.
271      * This method must be called before create(), otherwise it
272      * will be ignored.
273      *
274      * @param value whether the user name is mutable
275      */

276     public void setUsernameMutable(boolean value) {
277         isUsernameMutable = value;
278     }
279     
280     /* (non-Javadoc)
281      * @see org.eclipse.jface.dialogs.Dialog#close()
282      */

283     public boolean close() {
284         if(keyLockImage != null) {
285             keyLockImage.dispose();
286         }
287         return super.close();
288     }
289 }
290
Popular Tags