- Timestamp:
- 2003-05-27T15:34:50+12:00 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/GAuthenticator.java
r4293 r4363 55 55 import javax.swing.JPasswordField; 56 56 /** Provides a graphic authenticator for network password requests. 57 * @author John Thompson, Greenstone Digital Library, University of Waikato58 * @version 2.359 */57 * @author John Thompson, Greenstone Digital Library, University of Waikato 58 * @version 2.3 59 */ 60 60 public class GAuthenticator 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 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 78 * @return A <strong>PasswordAuthentication</strong> object containing the login and password valuees the user has submitted. 79 79 * @see org.greenstone.gatherer.GAuthenticator.AuthenticationActionListener 80 80 * @see org.greenstone.gatherer.GAuthenticator.RequestFocusListener 81 81 */ 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 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 108 109 110 111 109 password_panel.setLayout(new GridLayout(1,2)); 110 password_panel.add(password_label); 111 password_panel.add(password); 112 112 113 114 115 113 button_panel.setLayout(new GridLayout(1,2)); 114 button_panel.add(ok_button); 115 button_panel.add(cancel_button); 116 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 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 133 134 134 /** Retrieve a phrase from the dictionary. 135 135 * @param key A <strong>String</strong> used to determine what phrase to retrieve. 136 136 * @return The required phrase as a <strong>String</strong>, or at least some meaningful error message. … … 138 138 * @see org.greenstone.gatherer.Gatherer 139 139 */ 140 141 142 143 144 145 146 147 148 149 150 151 152 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 153 */ 154 155 156 157 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 158 * @param event An <strong>ActionEvent</strong> with information about the event that fired this method. 159 159 */ 160 161 162 163 164 165 166 167 168 169 170 171 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 172 */ 173 174 175 176 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 177 * @param event An <strong>ActionEvent</strong> with information about the event that fired this method. 178 178 */ 179 180 181 182 179 public void actionPerformed(ActionEvent event) { 180 target.requestFocus(); 181 } 182 } 183 183 } 184 184
Note:
See TracChangeset
for help on using the changeset viewer.