KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > net > auth > 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.ui.internal.net.auth;
12
13 import org.eclipse.jface.dialogs.Dialog;
14 import org.eclipse.jface.dialogs.IDialogConstants;
15 import org.eclipse.osgi.util.NLS;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.*;
20 import org.eclipse.ui.PlatformUI;
21 import org.eclipse.ui.internal.net.NetUIMessages;
22
23 /**
24  * User authentication dialog
25  */

26 public class UserValidationDialog extends Dialog {
27     protected Text usernameField;
28     protected Text passwordField;
29
30     protected String JavaDoc host;
31     protected String JavaDoc message;
32     protected Authentication userAuthentication = null;
33     /**
34      * Gets user and password from a user. May be called from any thread
35      * @param host the host name
36      * @param message the message to be displayed when prompting
37      *
38      * @return UserAuthentication that contains the userid and the password or
39      * <code>null</code> if the dialog has been cancelled
40      */

41     public static Authentication getAuthentication(final String JavaDoc host,
42             final String JavaDoc message) {
43         class UIOperation implements Runnable JavaDoc {
44             public Authentication authentication;
45             public void run() {
46                 authentication = UserValidationDialog.askForAuthentication(
47                         host, message);
48             }
49         }
50
51         UIOperation uio = new UIOperation();
52         if (Display.getCurrent() != null) {
53             uio.run();
54         } else {
55             Display.getDefault().syncExec(uio);
56         }
57         return uio.authentication;
58     }
59     /**
60      * Gets user and password from a user Must be called from UI thread
61      *
62      * @return UserAuthentication that contains the userid and the password or
63      * <code>null</code> if the dialog has been cancelled
64      */

65     protected static Authentication askForAuthentication(String JavaDoc host,
66             String JavaDoc message) {
67         UserValidationDialog ui = new UserValidationDialog(null, host, message);
68         ui.open();
69         return ui.getAuthentication();
70     }
71     /**
72      * Creates a new UserValidationDialog.
73      *
74      * @param parentShell
75      * parent Shell or null
76      */

77     protected UserValidationDialog(Shell parentShell, String JavaDoc host,
78             String JavaDoc message) {
79         super(parentShell);
80         this.host = host;
81         this.message = message;
82         setBlockOnOpen(true);
83     }
84     /**
85      */

86     protected void configureShell(Shell newShell) {
87         super.configureShell(newShell);
88         newShell.setText(NetUIMessages.UserValidationDialog_0);
89     }
90     /**
91      */

92     public void create() {
93         super.create();
94         //give focus to username field
95
usernameField.selectAll();
96         usernameField.setFocus();
97     }
98     /**
99      */

100     protected Control createDialogArea(Composite parent) {
101         Composite main = new Composite(parent, SWT.NONE);
102         GridLayout layout = new GridLayout();
103         layout.numColumns = 3;
104         main.setLayout(layout);
105         main.setLayoutData(new GridData(GridData.FILL_BOTH));
106
107         Label label = new Label(main, SWT.WRAP);
108         String JavaDoc text = NLS.bind(NetUIMessages.UserValidationDialog_1, host);
109         text += "\n\n" + message; //$NON-NLS-1$
110
label.setText(text);
111         GridData data = new GridData(GridData.FILL_HORIZONTAL);
112         data.horizontalSpan = 3;
113         label.setLayoutData(data);
114
115         createUsernameFields(main);
116         createPasswordFields(main);
117         PlatformUI.getWorkbench().getHelpSystem().setHelp(main,
118                 "org.eclipse.update.ui.UserValidationDialog"); //$NON-NLS-1$
119
return main;
120     }
121     /**
122      * Creates the three widgets that represent the user name entry area.
123      */

124     protected void createPasswordFields(Composite parent) {
125         new Label(parent, SWT.NONE).setText(NetUIMessages.UserValidationDialog_2);
126
127         passwordField = new Text(parent, SWT.BORDER | SWT.PASSWORD);
128         GridData data = new GridData(GridData.FILL_HORIZONTAL);
129         data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH);
130         passwordField.setLayoutData(data);
131
132         new Label(parent, SWT.NONE); //spacer
133
}
134     /**
135      * Creates the three widgets that represent the user name entry area.
136      */

137     protected void createUsernameFields(Composite parent) {
138         new Label(parent, SWT.NONE).setText(NetUIMessages.UserValidationDialog_3);
139
140         usernameField = new Text(parent, SWT.BORDER);
141         GridData data = new GridData(GridData.FILL_HORIZONTAL);
142         data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH);
143         usernameField.setLayoutData(data);
144
145         new Label(parent, SWT.NONE); //spacer
146
}
147     /**
148      * Returns the UserAuthentication entered by the user, or null if the user
149      * canceled.
150      * @return the authentication information
151      */

152     public Authentication getAuthentication() {
153         return userAuthentication;
154     }
155     /**
156      * Notifies that the ok button of this dialog has been pressed.
157      */

158     protected void okPressed() {
159         userAuthentication = new Authentication(usernameField.getText(),
160                 passwordField.getText());
161         super.okPressed();
162     }
163
164 }
165
Popular Tags