source: gli/trunk/src/org/greenstone/gatherer/GAuthenticator.java@ 16884

Last change on this file since 16884 was 14974, checked in by davidb, 16 years ago

Changes to GLI to support export into Fedora. New utility called flisvn diff gems/MetadataSetManager.java

  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer;
38
39import java.awt.*;
40import java.awt.event.*;
41import java.net.*;
42import java.util.*;
43import javax.swing.*;
44
45
46import org.greenstone.gatherer.gui.GLIButton;
47
48
49/** Provides a graphic authenticator for network password requests.
50 * @author John Thompson, Greenstone Digital Library, University of Waikato
51 * @version 2.3
52 */
53public class GAuthenticator
54 extends Authenticator
55{
56 static public Hashtable authentications = new Hashtable();
57
58 /** Indicates if this authentication prompt been cancelled, and if so rolls-back authentication. */
59 private boolean authentication_cancelled = false;
60 /** The button used to cancel a prompt. */
61 private JButton cancel_button = null;
62 /** The button used to submit the login/password. */
63 private JButton ok_button = null;
64 /** A reference to the dialog prompt created so inner classes can dispose of it. */
65 private JDialog dialog = null;
66 /** The password is a special starred out password field. */
67 private JPasswordField password = null;
68 /** The default size of this dialog. */
69 static final private Dimension SIZE = new Dimension(470,160);
70
71 /** Constructor. */
72 public GAuthenticator() {
73 }
74
75 /** Prompt the user for authentication using a pretty dialog box.
76 * @return A <strong>PasswordAuthentication</strong> object containing the login and password valuees the user has submitted.
77 * @see org.greenstone.gatherer.GAuthenticator.AuthenticationActionListener
78 * @see org.greenstone.gatherer.GAuthenticator.RequestFocusListener
79 */
80 protected PasswordAuthentication getPasswordAuthentication(String username_str, String password_str) {
81 // Component definition.
82 dialog = new JDialog (Gatherer.g_man, Dictionary.get("GAuthenticator.Title"), true);
83 dialog.setModal(true);
84 dialog.setSize(SIZE);
85 JPanel content_pane = (JPanel) dialog.getContentPane();
86 JLabel title_label = new JLabel(getMessageString());
87
88 JPanel user_panel = new JPanel();
89 JLabel username_label = new JLabel(Dictionary.get("GAuthenticator.Username"));
90 JTextField username = new JTextField();
91 username.setToolTipText(Dictionary.get("GAuthenticator.Username_Tooltip"));
92 if (username_str != null) {
93 username.setText(username_str);
94 }
95
96 JPanel password_panel = new JPanel();
97 JLabel password_label = new JLabel(Dictionary.get("GAuthenticator.Password"));
98 password = new JPasswordField();
99 password.setEchoChar('*');
100 password.setToolTipText(Dictionary.get("GAuthenticator.Password_Tooltip"));
101 if (password_str != null) {
102 password.setText(password_str);
103 }
104
105 JPanel button_panel = new JPanel();
106 ok_button = new GLIButton(Dictionary.get("General.OK"), Dictionary.get("General.OK_Tooltip"));
107 cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Cancel_Tooltip"));
108
109 // Connection
110 cancel_button.addActionListener(new AuthenticationActionListener(true));
111 ok_button.addActionListener(new AuthenticationActionListener(false));
112 password.addActionListener(new AuthenticationActionListener(false));
113 username.addActionListener(new RequestFocusListener(password));
114
115 // Layout
116 user_panel.setLayout(new GridLayout(1,2));
117 user_panel.add(username_label);
118 user_panel.add(username);
119
120 password_panel.setLayout(new GridLayout(1,2));
121 password_panel.add(password_label);
122 password_panel.add(password);
123
124 button_panel.setLayout(new GridLayout(1,2));
125 button_panel.add(ok_button);
126 button_panel.add(cancel_button);
127
128 content_pane.setLayout(new GridLayout(4,1,0,2));
129 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
130 content_pane.add(title_label);
131 content_pane.add(user_panel);
132 content_pane.add(password_panel);
133 content_pane.add(button_panel);
134
135 // Position the window
136 Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
137 dialog.setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
138 dialog.setVisible(true);
139 if(!authentication_cancelled) {
140 // Store the authentication
141 authentications.put(getRequestingHost() + ":" + getRequestingPort(), username.getText() + "@" + new String(password.getPassword()));
142 return new PasswordAuthentication(username.getText(), password.getPassword());
143 } else {
144 return null;
145 }
146 }
147
148 protected PasswordAuthentication getPasswordAuthentication() {
149 return getPasswordAuthentication(null,null);
150 }
151
152 /** This is defined so it can be overridden by subclasses (getRequestingPrompt is final). */
153 protected String getMessageString()
154 {
155 return getRequestingPrompt();
156 }
157
158
159 /** Detects actions upon any control that attempt to submit the current details for authentication. */
160 private class AuthenticationActionListener
161 implements ActionListener {
162 /** <i>true</i> if this authentication action cancels the authentication, <i>false</i> otherwise. */
163 private boolean cancel_action = false;
164 /** Constructor.
165 * @param cancel_action <i>true</i> if this authentication action cancels the authentication, <i>false</i> otherwise.
166 */
167 public AuthenticationActionListener(boolean cancel_action) {
168 this.cancel_action = cancel_action;
169 }
170 /** Any implementation of an ActionListener must include this method so that we can be informed when an action has been performed on our registered controls, allowing us to dispose of the authentication dialog after determining if this is a submit action or a cancel one.
171 * @param event An <strong>ActionEvent</strong> with information about the event that fired this method.
172 */
173 public void actionPerformed(ActionEvent event) {
174 authentication_cancelled = cancel_action;
175 dialog.dispose();
176 }
177 }
178
179 /** This listener detects actions on registered controls, and when they occur ensures the focus is moved to some targetted component. */
180 private class RequestFocusListener
181 implements ActionListener {
182 /*The <strong>Component</strong> you wish to gain focus when an action is performed on a registered control. */
183 private Component target = null;
184 /** Constructor.
185 * @param target The <strong>Component</strong> you wish to gain focus when an action is performed on a registered control.
186 */
187 public RequestFocusListener(Component target) {
188 this.target = target;
189 }
190 /** Any implementation of an ActionListener must include this method so that we can be informed when an action has been performed on our registered controls, allowing us to request focus in the target control.
191 * @param event An <strong>ActionEvent</strong> with information about the event that fired this method.
192 */
193 public void actionPerformed(ActionEvent event) {
194 target.requestFocus();
195 }
196 }
197}
Note: See TracBrowser for help on using the repository browser.