KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > security > UserValidationDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.update.internal.ui.security;
12
13 import org.eclipse.jface.dialogs.*;
14 import org.eclipse.jface.dialogs.Dialog;
15 import org.eclipse.swt.*;
16 import org.eclipse.swt.layout.*;
17 import org.eclipse.swt.widgets.*;
18 import org.eclipse.ui.*;
19 import org.eclipse.update.internal.ui.*;
20
21 /**
22  * User authentication dialog
23  */

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

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

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

73     protected UserValidationDialog(Shell parentShell, String JavaDoc host,
74             String JavaDoc message) {
75         super(parentShell);
76         this.host = host;
77         this.message = message;
78         setBlockOnOpen(true);
79     }
80     /**
81      */

82     protected void configureShell(Shell newShell) {
83         super.configureShell(newShell);
84         newShell.setText(UpdateUIMessages.UserVerificationDialog_PasswordRequired);
85     }
86     /**
87      */

88     public void create() {
89         super.create();
90         //give focus to username field
91
usernameField.selectAll();
92         usernameField.setFocus();
93     }
94     /**
95      */

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

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

133     protected void createUsernameFields(Composite parent) {
134         new Label(parent, SWT.NONE).setText(UpdateUIMessages.UserVerificationDialog_UserName);
135
136         usernameField = new Text(parent, SWT.BORDER);
137         GridData data = new GridData(GridData.FILL_HORIZONTAL);
138         data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH);
139         usernameField.setLayoutData(data);
140
141         new Label(parent, SWT.NONE); //spacer
142
}
143     /**
144      * Returns the UserAuthentication entered by the user, or null if the user
145      * canceled.
146      */

147     public Authentication getAuthentication() {
148         return userAuthentication;
149     }
150     /**
151      * Notifies that the ok button of this dialog has been pressed.
152      */

153     protected void okPressed() {
154         userAuthentication = new Authentication(usernameField.getText(),
155                 passwordField.getText());
156         super.okPressed();
157     }
158
159 }
160
Popular Tags