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

Last change on this file since 10610 was 10581, checked in by mdewsnip, 19 years ago

Moved the call to getRequestingPrompt out to a new function, so it can be overridden.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.5 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.*;
44import org.greenstone.gatherer.gui.GLIButton;
45
46/** Provides a graphic authenticator for network password requests.
47 * @author John Thompson, Greenstone Digital Library, University of Waikato
48 * @version 2.3
49 */
50public class GAuthenticator
51 extends Authenticator
52{
53 static public Hashtable authentications = new Hashtable();
54
55 /** Indicates if this authentication prompt been cancelled, and if so rolls-back authentication. */
56 private boolean authentication_cancelled = false;
57 /** The button used to cancel a prompt. */
58 private JButton cancel_button = null;
59 /** The button used to submit the login/password. */
60 private JButton ok_button = null;
61 /** A reference to the dialog prompt created so inner classes can dispose of it. */
62 private JDialog dialog = null;
63 /** The password is a special starred out password field. */
64 private JPasswordField password = null;
65 /** The default size of this dialog. */
66 static final private Dimension SIZE = new Dimension(410,140);
67
68 /** Constructor. */
69 public GAuthenticator() {
70 }
71
72 /** Prompt the user for authentication using a pretty dialog box.
73 * @return A <strong>PasswordAuthentication</strong> object containing the login and password valuees the user has submitted.
74 * @see org.greenstone.gatherer.GAuthenticator.AuthenticationActionListener
75 * @see org.greenstone.gatherer.GAuthenticator.RequestFocusListener
76 */
77 protected PasswordAuthentication getPasswordAuthentication() {
78 // Component definition.
79 dialog = new JDialog (Gatherer.g_man, Dictionary.get("GAuthenticator.Title"), true);
80 dialog.setModal(true);
81 dialog.setSize(SIZE);
82 JPanel content_pane = (JPanel) dialog.getContentPane();
83 JLabel title_label = new JLabel(getMessageString());
84 JPanel user_panel = new JPanel();
85 JLabel username_label = new JLabel();
86 Dictionary.setText(username_label, "GAuthenticator.Username");
87 JTextField username = new JTextField();
88 Dictionary.setTooltip(username, "GAuthenticator.Username_Tooltip");
89 JPanel password_panel = new JPanel();
90 JLabel password_label = new JLabel();
91 Dictionary.setText(password_label, "GAuthenticator.Password");
92 password = new JPasswordField();
93 password.setEchoChar('*');
94 Dictionary.setTooltip(password, "GAuthenticator.Password_Tooltip");
95 JPanel button_panel = new JPanel();
96 ok_button = new GLIButton();
97 ok_button.setMnemonic(KeyEvent.VK_O);
98 Dictionary.setBoth(ok_button, "General.OK", "General.OK_Tooltip");
99 cancel_button = new GLIButton();
100 cancel_button.setMnemonic(KeyEvent.VK_C);
101 Dictionary.setBoth(cancel_button, "General.Cancel", "General.Cancel_Tooltip");
102
103 // Connection
104 cancel_button.addActionListener(new AuthenticationActionListener(true));
105 ok_button.addActionListener(new AuthenticationActionListener(false));
106 password.addActionListener(new AuthenticationActionListener(false));
107 username.addActionListener(new RequestFocusListener(password));
108
109 // Layout
110 user_panel.setLayout(new GridLayout(1,2));
111 user_panel.add(username_label);
112 user_panel.add(username);
113
114 password_panel.setLayout(new GridLayout(1,2));
115 password_panel.add(password_label);
116 password_panel.add(password);
117
118 button_panel.setLayout(new GridLayout(1,2));
119 button_panel.add(ok_button);
120 button_panel.add(cancel_button);
121
122 content_pane.setLayout(new GridLayout(4,1,0,2));
123 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
124 content_pane.add(title_label);
125 content_pane.add(user_panel);
126 content_pane.add(password_panel);
127 content_pane.add(button_panel);
128
129 // Position the window
130 Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
131 dialog.setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
132 dialog.setVisible(true);
133 if(!authentication_cancelled) {
134 // Store the authentication
135 authentications.put(getRequestingHost() + ":" + getRequestingPort(), username.getText() + "@" + new String(password.getPassword()));
136 return new PasswordAuthentication(username.getText(), password.getPassword());
137 } else {
138 return null;
139 }
140 }
141
142
143 /** This is defined so it can be overridden by subclasses (getRequestingPrompt is final). */
144 protected String getMessageString()
145 {
146 return getRequestingPrompt();
147 }
148
149
150 /** Detects actions upon any control that attempt to submit the current details for authentication. */
151 private class AuthenticationActionListener
152 implements ActionListener {
153 /** <i>true</i> if this authentication action cancels the authentication, <i>false</i> otherwise. */
154 private boolean cancel_action = false;
155 /** Constructor.
156 * @param cancel_action <i>true</i> if this authentication action cancels the authentication, <i>false</i> otherwise.
157 */
158 public AuthenticationActionListener(boolean cancel_action) {
159 this.cancel_action = cancel_action;
160 }
161 /** 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.
162 * @param event An <strong>ActionEvent</strong> with information about the event that fired this method.
163 */
164 public void actionPerformed(ActionEvent event) {
165 authentication_cancelled = cancel_action;
166 dialog.dispose();
167 }
168 }
169
170 /** This listener detects actions on registered controls, and when they occur ensures the focus is moved to some targetted component. */
171 private class RequestFocusListener
172 implements ActionListener {
173 /*The <strong>Component</strong> you wish to gain focus when an action is performed on a registered control. */
174 private Component target = null;
175 /** Constructor.
176 * @param target The <strong>Component</strong> you wish to gain focus when an action is performed on a registered control.
177 */
178 public RequestFocusListener(Component target) {
179 this.target = target;
180 }
181 /** 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.
182 * @param event An <strong>ActionEvent</strong> with information about the event that fired this method.
183 */
184 public void actionPerformed(ActionEvent event) {
185 target.requestFocus();
186 }
187 }
188}
Note: See TracBrowser for help on using the repository browser.