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;
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;
30 * A dialog for prompting for a category and fPassword
32 public class UserValidationDialog extends Dialog {
34 protected Text usernameField;
36 protected Text passwordField;
38 protected Button allowCachingButton;
40 protected String domain;
42 protected String defaultUsername;
44 protected String password = null;
46 protected boolean allowCaching = false;
48 protected Image keyLockImage;
50 // whether or not the category can be changed
51 protected boolean isUsernameMutable = true;
53 protected boolean showAllowCachingButton = true;
55 protected String username = null;
57 protected String message = null;
60 * Creates a new CategoryDialog.
67 * the default fUser name
69 * a mesage to display to the fUser
71 public UserValidationDialog(Shell parentShell, String location, String defaultName, String message) {
73 this.defaultUsername = defaultName;
74 this.domain = location;
75 this.message = message;
79 * @see Window#configureShell
81 protected void configureShell(Shell newShell) {
82 super.configureShell(newShell);
83 newShell.setText(Messages.getString("CategoryDialog.required")); //$NON-NLS-1$
85 // WorkbenchHelp.setHelp(newShell, IHelpContextIds.USER_VALIDATION_DIALOG);
91 public void create() {
93 // add some default values
94 usernameField.setText(defaultUsername);
96 if (isUsernameMutable) {
97 // give focus to category field
98 usernameField.selectAll();
99 usernameField.setFocus();
101 usernameField.setEditable(false);
102 passwordField.setFocus();
107 * @see Dialog#createDialogArea
109 protected Control createDialogArea(Composite parent) {
110 Composite top = new Composite(parent, SWT.NONE);
111 GridLayout layout = new GridLayout();
112 layout.numColumns = 2;
114 top.setLayout(layout);
115 top.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
117 Composite imageComposite = new Composite(top, SWT.NONE);
118 layout = new GridLayout();
119 imageComposite.setLayout(layout);
120 imageComposite.setLayoutData(new GridData(GridData.FILL_VERTICAL));
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));
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);
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);
142 if (domain != null) {
143 Label d = new Label(main, SWT.WRAP);
144 d.setText(Messages.getString("CategoryDialog.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("CategoryDialog.labelUser", domain)); //$NON-NLS-1$
151 label.setText(Messages.bind("CategoryDialog.labelPassword", new Object[] { defaultUsername, domain })); //$NON-NLS-1$
153 data = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
154 data.horizontalSpan = 2;
155 data.widthHint = 300;
156 label.setLayoutData(data);
158 createUsernameFields(main);
159 createPasswordFields(main);
161 if (domain != null && showAllowCachingButton) {
162 allowCachingButton = new Button(main, SWT.CHECK);
163 allowCachingButton.setText(Messages.getString("CategoryDialog.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();
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("CategoryDialog.7")); //$NON-NLS-1$
186 data = new GridData(GridData.FILL_HORIZONTAL);
187 data.widthHint = 300;
188 warningText.setLayoutData(data);
191 Dialog.applyDialogFont(parent);
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);
208 * Creates the three widgets that represent the fPassword entry area.
211 * the parent of the widgets
213 protected void createPasswordFields(Composite parent) {
214 new Label(parent, SWT.NONE).setText(Messages.getString("CategoryDialog.password")); //$NON-NLS-1$
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);
224 * Creates the three widgets that represent the fUser name entry area.
227 * the parent of the widgets
229 protected void createUsernameFields(Composite parent) {
230 new Label(parent, SWT.NONE).setText(Messages.getString("CategoryDialog.user")); //$NON-NLS-1$
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);
240 * Returns the fPassword entered by the fUser, or null if the fUser canceled.
242 * @return the entered fPassword
244 public String getPassword() {
249 * Returns the category entered by the fUser, or null if the fUser canceled.
251 * @return the entered category
253 public String getUsername() {
258 * Returns <code>true</code> if the save fPassword checkbox was selected.
260 * @return <code>true</code> if the save fPassword checkbox was selected and <code>false</code> otherwise.
262 public boolean getAllowCaching() {
267 * Notifies that the ok button of this dialog has been pressed.
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.
273 protected void okPressed() {
274 password = passwordField.getText();
275 username = usernameField.getText();
281 * Sets whether or not the category field should be mutable. This method must be called before create(), otherwise it will be
285 * whether the category is mutable
287 public void setUsernameMutable(boolean value) {
288 isUsernameMutable = value;
291 public void setShowAllowCachingButton(boolean value) {
292 showAllowCachingButton = value;
298 * @see org.eclipse.jface.dialogs.Dialog#close()
300 public boolean close() {
301 if (keyLockImage != null) {
302 keyLockImage.dispose();
304 return super.close();