Improved Templates i.e.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / preferences / UserValidationDialog.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpeclipse.wiki.preferences;
12
13 import org.eclipse.jface.dialogs.Dialog;
14 import org.eclipse.jface.dialogs.IDialogConstants;
15 import org.eclipse.jface.window.Window;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.swt.widgets.Text;
28
29 /**
30  * A dialog for prompting for a username and fPassword
31  */
32 public class UserValidationDialog extends Dialog {
33   // widgets
34   protected Text usernameField;
35
36   protected Text passwordField;
37
38   protected Button allowCachingButton;
39
40   protected String domain;
41
42   protected String defaultUsername;
43
44   protected String password = null;
45
46   protected boolean allowCaching = false;
47
48   protected Image keyLockImage;
49
50   // whether or not the username can be changed
51   protected boolean isUsernameMutable = true;
52
53   protected boolean showAllowCachingButton = true;
54
55   protected String username = null;
56
57   protected String message = null;
58
59   /**
60    * Creates a new UserValidationDialog.
61    * 
62    * @param parentShell
63    *          the parent shell
64    * @param location
65    *          the location
66    * @param defaultName
67    *          the default fUser name
68    * @param message
69    *          a mesage to display to the fUser
70    */
71   public UserValidationDialog(Shell parentShell, String location, String defaultName, String message) {
72     super(parentShell);
73     this.defaultUsername = defaultName;
74     this.domain = location;
75     this.message = message;
76   }
77
78   /**
79    * @see Window#configureShell
80    */
81   protected void configureShell(Shell newShell) {
82     super.configureShell(newShell);
83     newShell.setText(Messages.getString("UserValidationDialog.required")); //$NON-NLS-1$
84     // set F1 help
85     //          WorkbenchHelp.setHelp(newShell, IHelpContextIds.USER_VALIDATION_DIALOG);
86   }
87
88   /**
89    * @see Window#create
90    */
91   public void create() {
92     super.create();
93     // add some default values
94     usernameField.setText(defaultUsername);
95
96     if (isUsernameMutable) {
97       // give focus to username field
98       usernameField.selectAll();
99       usernameField.setFocus();
100     } else {
101       usernameField.setEditable(false);
102       passwordField.setFocus();
103     }
104   }
105
106   /**
107    * @see Dialog#createDialogArea
108    */
109   protected Control createDialogArea(Composite parent) {
110     Composite top = new Composite(parent, SWT.NONE);
111     GridLayout layout = new GridLayout();
112     layout.numColumns = 2;
113
114     top.setLayout(layout);
115     top.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
116
117     Composite imageComposite = new Composite(top, SWT.NONE);
118     layout = new GridLayout();
119     imageComposite.setLayout(layout);
120     imageComposite.setLayoutData(new GridData(GridData.FILL_VERTICAL));
121
122     Composite main = new Composite(top, SWT.NONE);
123     layout = new GridLayout();
124     layout.numColumns = 3;
125     main.setLayout(layout);
126     main.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
127
128     Label imageLabel = new Label(imageComposite, SWT.NONE);
129     keyLockImage = TeamImages.getImageDescriptor(ITeamUIImages.IMG_KEY_LOCK).createImage();
130     imageLabel.setImage(keyLockImage);
131     GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
132     imageLabel.setLayoutData(data);
133
134     if (message != null) {
135       Label messageLabel = new Label(main, SWT.WRAP);
136       messageLabel.setText(message);
137       data = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
138       data.horizontalSpan = 3;
139       data.widthHint = 300;
140       messageLabel.setLayoutData(data);
141     }
142     if (domain != null) {
143       Label d = new Label(main, SWT.WRAP);
144       d.setText(Messages.getString("UserValidationDialog.5")); //$NON-NLS-1$
145       data = new GridData();
146       d.setLayoutData(data);
147       Label label = new Label(main, SWT.WRAP);
148       if (isUsernameMutable) {
149         label.setText(Messages.bind("UserValidationDialog.labelUser", domain)); //$NON-NLS-1$
150       } else {
151         label.setText(Messages.bind("UserValidationDialog.labelPassword", new Object[] { defaultUsername, domain })); //$NON-NLS-1$
152       }
153       data = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
154       data.horizontalSpan = 2;
155       data.widthHint = 300;
156       label.setLayoutData(data);
157     }
158     createUsernameFields(main);
159     createPasswordFields(main);
160
161     if (domain != null && showAllowCachingButton) {
162       allowCachingButton = new Button(main, SWT.CHECK);
163       allowCachingButton.setText(Messages.getString("UserValidationDialog.6")); //$NON-NLS-1$
164       data = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
165       data.horizontalSpan = 3;
166       allowCachingButton.setLayoutData(data);
167       allowCachingButton.addSelectionListener(new SelectionAdapter() {
168         public void widgetSelected(SelectionEvent e) {
169           allowCaching = allowCachingButton.getSelection();
170         }
171       });
172       Composite warningComposite = new Composite(main, SWT.NONE);
173       layout = new GridLayout();
174       layout.numColumns = 2;
175       layout.marginHeight = 0;
176       layout.marginHeight = 0;
177       warningComposite.setLayout(layout);
178       data = new GridData(GridData.FILL_HORIZONTAL);
179       data.horizontalSpan = 3;
180       warningComposite.setLayoutData(data);
181       Label warningLabel = new Label(warningComposite, SWT.NONE);
182       warningLabel.setImage(getImage(DLG_IMG_MESSAGE_WARNING));
183       warningLabel.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_BEGINNING));
184       Label warningText = new Label(warningComposite, SWT.WRAP);
185       warningText.setText(Messages.getString("UserValidationDialog.7")); //$NON-NLS-1$
186       data = new GridData(GridData.FILL_HORIZONTAL);
187       data.widthHint = 300;
188       warningText.setLayoutData(data);
189     }
190
191     Dialog.applyDialogFont(parent);
192
193     return main;
194   }
195
196   /**
197    * Create a spacer.
198    */
199   protected void createSpacer(Composite top, int columnSpan, int vertSpan) {
200     Label l = new Label(top, SWT.NONE);
201     GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
202     data.horizontalSpan = columnSpan;
203     data.verticalSpan = vertSpan;
204     l.setLayoutData(data);
205   }
206
207   /**
208    * Creates the three widgets that represent the fPassword entry area.
209    * 
210    * @param parent
211    *          the parent of the widgets
212    */
213   protected void createPasswordFields(Composite parent) {
214     new Label(parent, SWT.NONE).setText(Messages.getString("UserValidationDialog.password")); //$NON-NLS-1$
215
216     passwordField = new Text(parent, SWT.BORDER | SWT.PASSWORD);
217     GridData data = new GridData(GridData.FILL_HORIZONTAL);
218     data.horizontalSpan = 2;
219     data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH);
220     passwordField.setLayoutData(data);
221   }
222
223   /**
224    * Creates the three widgets that represent the fUser name entry area.
225    * 
226    * @param parent
227    *          the parent of the widgets
228    */
229   protected void createUsernameFields(Composite parent) {
230     new Label(parent, SWT.NONE).setText(Messages.getString("UserValidationDialog.user")); //$NON-NLS-1$
231
232     usernameField = new Text(parent, SWT.BORDER);
233     GridData data = new GridData(GridData.FILL_HORIZONTAL);
234     data.horizontalSpan = 2;
235     data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH);
236     usernameField.setLayoutData(data);
237   }
238
239   /**
240    * Returns the fPassword entered by the fUser, or null if the fUser canceled.
241    * 
242    * @return the entered fPassword
243    */
244   public String getPassword() {
245     return password;
246   }
247
248   /**
249    * Returns the username entered by the fUser, or null if the fUser canceled.
250    * 
251    * @return the entered username
252    */
253   public String getUsername() {
254     return username;
255   }
256
257   /**
258    * Returns <code>true</code> if the save fPassword checkbox was selected.
259    * 
260    * @return <code>true</code> if the save fPassword checkbox was selected and <code>false</code> otherwise.
261    */
262   public boolean getAllowCaching() {
263     return allowCaching;
264   }
265
266   /**
267    * Notifies that the ok button of this dialog has been pressed.
268    * <p>
269    * The default implementation of this framework method sets this dialog's return code to <code>Window.OK</code> and closes the
270    * dialog. Subclasses may override.
271    * </p>
272    */
273   protected void okPressed() {
274     password = passwordField.getText();
275     username = usernameField.getText();
276
277     super.okPressed();
278   }
279
280   /**
281    * Sets whether or not the username field should be mutable. This method must be called before create(), otherwise it will be
282    * ignored.
283    * 
284    * @param value
285    *          whether the username is mutable
286    */
287   public void setUsernameMutable(boolean value) {
288     isUsernameMutable = value;
289   }
290
291   public void setShowAllowCachingButton(boolean value) {
292     showAllowCachingButton = value;
293   }
294
295   /*
296    * (non-Javadoc)
297    * 
298    * @see org.eclipse.jface.dialogs.Dialog#close()
299    */
300   public boolean close() {
301     if (keyLockImage != null) {
302       keyLockImage.dispose();
303     }
304     return super.close();
305   }
306 }