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

Last change on this file since 4436 was 4363, checked in by kjdon, 21 years ago

re-tabbed the code for java

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