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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpeclipse.wiki.preferences;
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;
33 * A dialog for prompting for a username and fPassword
35 public class UserValidationDialog extends Dialog {
37 protected Text usernameField;
38 protected Text passwordField;
39 protected Button allowCachingButton;
41 protected String domain;
42 protected String defaultUsername;
43 protected String password = null;
44 protected boolean allowCaching = false;
45 protected Image keyLockImage;
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;
54 * Creates a new UserValidationDialog.
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
61 public UserValidationDialog(Shell parentShell, String location, String defaultName, String message) {
63 this.defaultUsername = defaultName;
64 this.domain = location;
65 this.message = message;
68 * @see Window#configureShell
70 protected void configureShell(Shell newShell) {
71 super.configureShell(newShell);
72 newShell.setText(Messages.getString("UserValidationDialog.required")); //$NON-NLS-1$
74 // WorkbenchHelp.setHelp(newShell, IHelpContextIds.USER_VALIDATION_DIALOG);
79 public void create() {
81 // add some default values
82 usernameField.setText(defaultUsername);
84 if (isUsernameMutable) {
85 // give focus to username field
86 usernameField.selectAll();
87 usernameField.setFocus();
89 usernameField.setEditable(false);
90 passwordField.setFocus();
95 * @see Dialog#createDialogArea
97 protected Control createDialogArea(Composite parent) {
98 Composite top = new Composite(parent, SWT.NONE);
99 GridLayout layout = new GridLayout();
100 layout.numColumns = 2;
102 top.setLayout(layout);
103 top.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
105 Composite imageComposite = new Composite(top, SWT.NONE);
106 layout = new GridLayout();
107 imageComposite.setLayout(layout);
108 imageComposite.setLayoutData(new GridData(GridData.FILL_VERTICAL));
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));
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);
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);
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$
139 label.setText(Messages.bind("UserValidationDialog.labelPassword", new Object[]{defaultUsername, domain})); //$NON-NLS-1$
141 data = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
142 data.horizontalSpan = 2;
143 data.widthHint = 300;
144 label.setLayoutData(data);
146 createUsernameFields(main);
147 createPasswordFields(main);
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();
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);
179 Dialog.applyDialogFont(parent);
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);
195 * Creates the three widgets that represent the fPassword entry area.
197 * @param parent the parent of the widgets
199 protected void createPasswordFields(Composite parent) {
200 new Label(parent, SWT.NONE).setText(Messages.getString("UserValidationDialog.fPassword")); //$NON-NLS-1$
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);
209 * Creates the three widgets that represent the fUser name entry area.
211 * @param parent the parent of the widgets
213 protected void createUsernameFields(Composite parent) {
214 new Label(parent, SWT.NONE).setText(Messages.getString("UserValidationDialog.fUser")); //$NON-NLS-1$
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);
224 * Returns the fPassword entered by the fUser, or null
225 * if the fUser canceled.
227 * @return the entered fPassword
229 public String getPassword() {
234 * Returns the username entered by the fUser, or null
235 * if the fUser canceled.
237 * @return the entered username
239 public String getUsername() {
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>
248 public boolean getAllowCaching() {
253 * Notifies that the ok button of this dialog has been pressed.
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.
260 protected void okPressed() {
261 password = passwordField.getText();
262 username = usernameField.getText();
267 * Sets whether or not the username field should be mutable.
268 * This method must be called before create(), otherwise it
271 * @param value whether the username is mutable
273 public void setUsernameMutable(boolean value) {
274 isUsernameMutable = value;
277 public void setShowAllowCachingButton(boolean value) {
278 showAllowCachingButton = value;
282 * @see org.eclipse.jface.dialogs.Dialog#close()
284 public boolean close() {
285 if(keyLockImage != null) {
286 keyLockImage.dispose();
288 return super.close();