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

Last change on this file since 6540 was 6318, checked in by jmt12, 20 years ago

Changed JButtons for GLIButtons, which know whether they should paint their background depending on what platform they are run on, and finished keyboard shortcuts

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