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

Last change on this file since 5536 was 5536, checked in by mdewsnip, 21 years ago

Many more tooltips and improvements to the Dictionary. Still more to come.

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