KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > client > gui > swt > SWTAuthenticationDialog


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.agent.client.gui.swt;
21
22 import java.text.MessageFormat JavaDoc;
23
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.PaintEvent;
26 import org.eclipse.swt.events.PaintListener;
27 import org.eclipse.swt.graphics.Image;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Canvas;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.swt.widgets.Text;
34
35 import com.maverick.http.HttpAuthenticator;
36 import com.maverick.http.NTLMAuthentication;
37 import com.maverick.http.PasswordCredentials;
38
39 /**
40  * Dialog that may be used to ask for username, password and optionally domain
41  * (NTML) when authentication is required.
42  *
43  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
44  */

45 public class SWTAuthenticationDialog {
46
47     /**
48      * Prompt for credentials.
49      *
50      * @param authenticator authenticator
51      * @return <code>falsE</code> if cancelled.
52      */

53     public static boolean promptForCredentials(boolean proxy, Shell parent, HttpAuthenticator authenticator, final Image image) {
54         SWTOptionDialog opt = new SWTOptionDialog(parent,
55                         SWT.TITLE | SWT.CLOSE | SWT.BORDER,
56                         Messages.getString("SWTAuthenticationDialog.ok"),
57                         Messages.getString("SWTAuthenticationDialog.cancel"),
58                         Messages.getString("SWTAuthenticationDialog.title"),
59                         null);
60
61         Shell shell = opt.getShell();
62
63         GridLayout gridLayout = new GridLayout(1, false);
64         shell.setLayout(gridLayout);
65
66         // Common About Details Components
67
if (image != null) {
68             Canvas canvas = new Canvas(shell, SWT.NONE);
69             canvas.addPaintListener(new PaintListener() {
70                 public void paintControl(PaintEvent e) {
71                     e.gc.drawImage(image, 0, 0);
72                 }
73             });
74             GridData data = new GridData(SWT.BEGINNING, SWT.BEGINNING, true, true);
75             data.widthHint = image.getBounds().width;
76             data.heightHint = image.getBounds().height;
77             canvas.setLayoutData(data);
78         }
79
80         // Text
81
Label textLabel = new Label(shell, SWT.NONE | SWT.WRAP);
82         String JavaDoc info = authenticator.getInformation();
83         info = info == null ? "Authentication" : info;
84         String JavaDoc host = authenticator.getPort() == 0 ? authenticator.getHost()
85             : (authenticator.getHost() + ":" + authenticator.getPort());
86         textLabel.setText(MessageFormat.format(Messages.getString("SWTAuthenticationDialog.text"), new Object JavaDoc[] { info, host, authenticator.getScheme() }));
87         GridData data = new GridData(SWT.BEGINNING, SWT.BEGINNING, true, true);
88         textLabel.setLayoutData(data);
89
90         // Domain
91
Text domainText = null;
92         if (authenticator instanceof NTLMAuthentication) {
93             Label domainLabel = new Label(shell, SWT.NONE);
94             domainLabel.setText(Messages.getString("SWTAuthenticationDialog.domain"));
95             domainText = new Text(shell, SWT.BORDER);
96             data = new GridData(SWT.BEGINNING, SWT.BEGINNING, true, true);
97             data.widthHint = 300;
98             domainText.setLayoutData(data);
99         }
100
101         // Username
102
Label usernameLabel = new Label(shell, SWT.NONE);
103         usernameLabel.setText(Messages.getString("SWTAuthenticationDialog.username"));
104         Text usernameText = new Text(shell, SWT.BORDER);
105         usernameText.setText("");
106         data = new GridData(SWT.BEGINNING, SWT.BEGINNING, true, true);
107         data.widthHint = 300;
108         usernameText.setLayoutData(data);
109
110         // Password
111
Label passwordLabel = new Label(shell, SWT.NONE);
112         passwordLabel.setText(Messages.getString("SWTAuthenticationDialog.password"));
113
114         Text passwordText = new Text(shell, SWT.BORDER);
115         passwordText.setEchoChar('*');
116         data = new GridData(SWT.BEGINNING, SWT.BEGINNING, true, true);
117         data.widthHint = 300;
118         passwordText.setLayoutData(data);
119
120         // Show dialog
121

122         try {
123             if (opt.open()) {
124                 authenticator.setCredentials(new PasswordCredentials(usernameText.getText().trim(), passwordText.getText()));
125                 if (domainText != null && !domainText.getText().trim().equals("")) { //$NON-NLS-1$
126
((NTLMAuthentication) authenticator).setDomain(domainText.getText().trim());
127                 }
128                 return true;
129             }
130
131             return false;
132         } finally {
133             opt.close();
134         }
135     }
136
137 }
138
Popular Tags