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