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

Last change on this file since 12115 was 12115, checked in by kjdon, 18 years ago

Changed text handling to use Dictionary.get rather than Dictionary.setText or Dictionary.registerBoth etc

  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 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(Dictionary.get("GAuthenticator.Username"));
86 JTextField username = new JTextField();
87 username.setToolTipText(Dictionary.get("GAuthenticator.Username_Tooltip"));
88 JPanel password_panel = new JPanel();
89 JLabel password_label = new JLabel(Dictionary.get("GAuthenticator.Password"));
90 password = new JPasswordField();
91 password.setEchoChar('*');
92 password.setToolTipText(Dictionary.get("GAuthenticator.Password_Tooltip"));
93 JPanel button_panel = new JPanel();
94 ok_button = new GLIButton(Dictionary.get("General.OK"), Dictionary.get("General.OK_Tooltip"));
95 cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Cancel_Tooltip"));
96
97 // Connection
98 cancel_button.addActionListener(new AuthenticationActionListener(true));
99 ok_button.addActionListener(new AuthenticationActionListener(false));
100 password.addActionListener(new AuthenticationActionListener(false));
101 username.addActionListener(new RequestFocusListener(password));
102
103 // Layout
104 user_panel.setLayout(new GridLayout(1,2));
105 user_panel.add(username_label);
106 user_panel.add(username);
107
108 password_panel.setLayout(new GridLayout(1,2));
109 password_panel.add(password_label);
110 password_panel.add(password);
111
112 button_panel.setLayout(new GridLayout(1,2));
113 button_panel.add(ok_button);
114 button_panel.add(cancel_button);
115
116 content_pane.setLayout(new GridLayout(4,1,0,2));
117 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
118 content_pane.add(title_label);
119 content_pane.add(user_panel);
120 content_pane.add(password_panel);
121 content_pane.add(button_panel);
122
123 // Position the window
124 Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
125 dialog.setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
126 dialog.setVisible(true);
127 if(!authentication_cancelled) {
128 // Store the authentication
129 authentications.put(getRequestingHost() + ":" + getRequestingPort(), username.getText() + "@" + new String(password.getPassword()));
130 return new PasswordAuthentication(username.getText(), password.getPassword());
131 } else {
132 return null;
133 }
134 }
135
136
137 /** This is defined so it can be overridden by subclasses (getRequestingPrompt is final). */
138 protected String getMessageString()
139 {
140 return getRequestingPrompt();
141 }
142
143
144 /** Detects actions upon any control that attempt to submit the current details for authentication. */
145 private class AuthenticationActionListener
146 implements ActionListener {
147 /** <i>true</i> if this authentication action cancels the authentication, <i>false</i> otherwise. */
148 private boolean cancel_action = false;
149 /** Constructor.
150 * @param cancel_action <i>true</i> if this authentication action cancels the authentication, <i>false</i> otherwise.
151 */
152 public AuthenticationActionListener(boolean cancel_action) {
153 this.cancel_action = cancel_action;
154 }
155 /** 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.
156 * @param event An <strong>ActionEvent</strong> with information about the event that fired this method.
157 */
158 public void actionPerformed(ActionEvent event) {
159 authentication_cancelled = cancel_action;
160 dialog.dispose();
161 }
162 }
163
164 /** This listener detects actions on registered controls, and when they occur ensures the focus is moved to some targetted component. */
165 private class RequestFocusListener
166 implements ActionListener {
167 /*The <strong>Component</strong> you wish to gain focus when an action is performed on a registered control. */
168 private Component target = null;
169 /** Constructor.
170 * @param target The <strong>Component</strong> you wish to gain focus when an action is performed on a registered control.
171 */
172 public RequestFocusListener(Component target) {
173 this.target = target;
174 }
175 /** 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.
176 * @param event An <strong>ActionEvent</strong> with information about the event that fired this method.
177 */
178 public void actionPerformed(ActionEvent event) {
179 target.requestFocus();
180 }
181 }
182}
Note: See TracBrowser for help on using the repository browser.