[15505] | 1 | /*
|
---|
| 2 | * -----------------------------------------------------------------------------
|
---|
| 3 | *
|
---|
| 4 | * <p><b>License and Copyright: </b>The contents of this file are subject
|
---|
| 5 | * to the Educational Community License (the "License"); you may not use
|
---|
| 6 | * this file except in compliance with the License. You may obtain a copy
|
---|
| 7 | * of the License at <a href="http://www.opensource.org/licenses/ecl1.txt">
|
---|
| 8 | * http://www.opensource.org/licenses/ecl1.txt.</a></p>
|
---|
| 9 | *
|
---|
| 10 | * <p>Software distributed under the License is distributed on an "AS IS"
|
---|
| 11 | * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
---|
| 12 | * License for the specific language governing rights and limitations under
|
---|
| 13 | * the License.</p>
|
---|
| 14 | *
|
---|
| 15 | * <p>The entire file consists of original code. Copyright ©
|
---|
| 16 | * 2002-2007 by The Rector and Visitors of the University of Virginia and
|
---|
| 17 | * Cornell University. All rights reserved.</p>
|
---|
| 18 | *
|
---|
| 19 | * -----------------------------------------------------------------------------
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 | /* Based on Fedora's Client LoginDialog.java code */
|
---|
| 24 | /* Modified to work with Greenstone: 22 Jan 2008 */
|
---|
| 25 |
|
---|
| 26 | package org.greenstone.gatherer.gui;
|
---|
| 27 |
|
---|
| 28 | import java.awt.BorderLayout;
|
---|
| 29 | import java.awt.Container;
|
---|
| 30 | import java.awt.Dimension;
|
---|
| 31 | import java.awt.GridBagConstraints;
|
---|
| 32 | import java.awt.GridBagLayout;
|
---|
| 33 | import java.awt.Insets;
|
---|
| 34 | import java.awt.event.ActionEvent;
|
---|
| 35 | import java.awt.event.WindowAdapter;
|
---|
| 36 | import java.awt.event.WindowEvent;
|
---|
| 37 |
|
---|
| 38 | import java.io.File;
|
---|
| 39 | import java.io.FileInputStream;
|
---|
| 40 | import java.io.FileOutputStream;
|
---|
| 41 | import java.io.IOException;
|
---|
| 42 |
|
---|
| 43 | import java.util.Enumeration;
|
---|
| 44 | import java.util.HashMap;
|
---|
| 45 | import java.util.Iterator;
|
---|
| 46 | import java.util.List;
|
---|
| 47 | import java.util.Properties;
|
---|
| 48 |
|
---|
| 49 | import javax.swing.AbstractAction;
|
---|
| 50 | import javax.swing.BorderFactory;
|
---|
| 51 | import javax.swing.JButton;
|
---|
| 52 | import javax.swing.JComboBox;
|
---|
| 53 | import javax.swing.JComponent;
|
---|
| 54 | import javax.swing.JDialog;
|
---|
| 55 | import javax.swing.JLabel;
|
---|
| 56 | import javax.swing.JOptionPane;
|
---|
| 57 | import javax.swing.JPanel;
|
---|
| 58 | import javax.swing.JPasswordField;
|
---|
| 59 | import javax.swing.event.DocumentEvent;
|
---|
| 60 | import javax.swing.event.DocumentListener;
|
---|
| 61 |
|
---|
| 62 | import java.awt.Rectangle;
|
---|
| 63 | import java.awt.Toolkit;
|
---|
| 64 |
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 | import org.greenstone.gatherer.Gatherer;
|
---|
[16337] | 68 | import org.greenstone.gatherer.Configuration;
|
---|
[15505] | 69 |
|
---|
| 70 | public class FedoraLogin extends JDialog
|
---|
| 71 | {
|
---|
[16337] | 72 | private static final int MAX_ITEMS = 5;
|
---|
| 73 |
|
---|
[15505] | 74 | private static final java.awt.Color LIGHT = new java.awt.Color(244, 244, 224);
|
---|
| 75 | private static final java.awt.Color TRANSPARENT = new java.awt.Color(0,0,0,0);
|
---|
[16337] | 76 |
|
---|
[15505] | 77 | private JPanel m_infoPane;
|
---|
| 78 | private JComboBox m_serverComboBox;
|
---|
| 79 | private JComboBox m_protocolComboBox;
|
---|
| 80 | private JComboBox m_usernameComboBox;
|
---|
| 81 | private JPasswordField m_passwordField;
|
---|
| 82 |
|
---|
| 83 | private String m_lastUsername="fedoraAdmin";
|
---|
| 84 | private String m_lastServer="localhost:8080";
|
---|
| 85 | private String m_lastProtocol="http";
|
---|
| 86 | private String m_lastPassword="";
|
---|
| 87 |
|
---|
| 88 | private boolean login_requested = false;
|
---|
| 89 |
|
---|
| 90 | public FedoraLogin(String title, boolean can_cancel)
|
---|
| 91 | {
|
---|
| 92 | super(Gatherer.g_man, "Login", true);
|
---|
| 93 | this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
---|
| 94 |
|
---|
[16337] | 95 | JLabel serverLabel=new JLabel("Fedora Server");
|
---|
| 96 | JLabel protocolLabel=new JLabel("Protocol");
|
---|
[15505] | 97 | JLabel usernameLabel=new JLabel("Username");
|
---|
| 98 | JLabel passwordLabel=new JLabel("Password");
|
---|
| 99 |
|
---|
[16337] | 100 | m_serverComboBox=new JComboBox(new String[]{m_lastServer});
|
---|
[15505] | 101 | m_serverComboBox.setEditable(true);
|
---|
[16337] | 102 | m_protocolComboBox=new JComboBox(new String[]{m_lastProtocol, "https"}); // http and https
|
---|
| 103 | m_protocolComboBox.setEditable(true);
|
---|
| 104 | m_usernameComboBox=new JComboBox(new String[]{m_lastUsername});
|
---|
[15505] | 105 | m_usernameComboBox.setEditable(true);
|
---|
| 106 | m_passwordField=new JPasswordField();
|
---|
| 107 |
|
---|
| 108 | setComboBoxValues();
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 | LoginAction loginAction=new LoginAction(this);
|
---|
| 112 | JButton loginButton=new JButton(loginAction);
|
---|
| 113 |
|
---|
| 114 | loginAction.setButton(loginButton);
|
---|
| 115 | loginButton.setEnabled(false);
|
---|
| 116 |
|
---|
| 117 |
|
---|
| 118 | m_passwordField.getDocument().addDocumentListener(
|
---|
| 119 | new PasswordChangeListener(loginButton, m_passwordField));
|
---|
| 120 | m_passwordField.setAction(loginAction);
|
---|
| 121 |
|
---|
| 122 | JPanel inputPane=new JPanel();
|
---|
| 123 | inputPane.setBorder(BorderFactory.createCompoundBorder(
|
---|
| 124 | BorderFactory.createCompoundBorder(
|
---|
| 125 | BorderFactory.createEmptyBorder(6, 6, 6, 6),
|
---|
| 126 | BorderFactory.createEtchedBorder()
|
---|
| 127 | ),
|
---|
| 128 | BorderFactory.createEmptyBorder(6,6,6,6)
|
---|
| 129 | ));
|
---|
| 130 | GridBagLayout gridBag=new GridBagLayout();
|
---|
| 131 | inputPane.setLayout(gridBag);
|
---|
| 132 | addLabelValueRows(new JLabel[] {serverLabel, protocolLabel, usernameLabel, passwordLabel},
|
---|
| 133 | new JComponent[] {m_serverComboBox, m_protocolComboBox, m_usernameComboBox, m_passwordField},
|
---|
| 134 | gridBag, inputPane);
|
---|
| 135 |
|
---|
| 136 | // handling Closing and cancelling events
|
---|
| 137 | this.addWindowListener(new WindowAdapter() {
|
---|
| 138 | public void windowClosing(WindowEvent e){
|
---|
| 139 | // this is important, if we want to stop looping on displaying the dialog:
|
---|
| 140 | login_requested = false;
|
---|
| 141 | //dispose(); // defaultCloseOperation of this dialog already set to dispose it
|
---|
| 142 | }
|
---|
| 143 | });
|
---|
| 144 |
|
---|
| 145 | JButton cancelButton=new JButton(new AbstractAction() {
|
---|
| 146 | private static final long serialVersionUID = 1L;
|
---|
| 147 | public void actionPerformed(ActionEvent evt) {
|
---|
| 148 | // this is important, if we want to stop looping on displaying the dialog:
|
---|
| 149 | login_requested = false;
|
---|
| 150 | dispose();
|
---|
| 151 | }
|
---|
| 152 | });
|
---|
| 153 |
|
---|
| 154 | cancelButton.setText("Exit"); // if haven't logged in yet
|
---|
| 155 |
|
---|
| 156 | JPanel buttonPane=new JPanel();
|
---|
| 157 | buttonPane.add(loginButton);
|
---|
| 158 | buttonPane.add(cancelButton);
|
---|
| 159 | Container contentPane=getContentPane();
|
---|
| 160 |
|
---|
| 161 | contentPane.setLayout(new BorderLayout());
|
---|
| 162 | m_infoPane = new JPanel();
|
---|
| 163 | m_infoPane.setBackground(TRANSPARENT);
|
---|
| 164 | contentPane.add(m_infoPane, BorderLayout.NORTH);
|
---|
| 165 | contentPane.add(inputPane, BorderLayout.CENTER);
|
---|
| 166 | contentPane.add(buttonPane, BorderLayout.SOUTH);
|
---|
| 167 | addWindowListener(new WindowAdapter() {
|
---|
| 168 | public void windowOpened(WindowEvent evt) {
|
---|
| 169 | m_passwordField.requestFocus();
|
---|
| 170 | }
|
---|
| 171 | });
|
---|
| 172 | pack();
|
---|
| 173 |
|
---|
| 174 | // Position
|
---|
| 175 | Dimension size = getSize();
|
---|
| 176 | if (Gatherer.g_man != null) {
|
---|
| 177 | Rectangle frame_bounds = Gatherer.g_man.getBounds();
|
---|
| 178 | setLocation(frame_bounds.x + (frame_bounds.width - size.width) / 2, frame_bounds.y + (frame_bounds.height - size.height) / 2);
|
---|
| 179 | }
|
---|
| 180 | else {
|
---|
| 181 | Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
| 182 | setLocation((screen_size.width - size.width) / 2, (screen_size.height - size.height) / 2);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 |
|
---|
| 186 | setVisible(true);
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 |
|
---|
[16337] | 190 | public void saveProperties(JComboBox control, String affectedProperty, String editedValue) {
|
---|
| 191 | //String editedValue = (String)control.getSelectedItem();
|
---|
| 192 | // Edited value is the value in the combobox editfield, it has not yet been added to the combobox
|
---|
| 193 | control.insertItemAt(editedValue, 0);
|
---|
| 194 |
|
---|
| 195 | if(editedValue == null) {
|
---|
| 196 | editedValue = (String)control.getItemAt(0); // else reuse the first default
|
---|
| 197 | }
|
---|
| 198 | Configuration.setString(affectedProperty, true, editedValue);
|
---|
| 199 |
|
---|
| 200 | // Add the values in the combobox as well.
|
---|
| 201 | int value_count = 1;
|
---|
| 202 | for(int i = 0; value_count < MAX_ITEMS && i < control.getItemCount(); i++, value_count++) {
|
---|
| 203 | String value = (String)control.getItemAt(i);
|
---|
| 204 | if(value == null || value.equals(editedValue) || value.equals("")) {
|
---|
| 205 | // skip duplicates of just-edited comboBox value and empty values
|
---|
| 206 | value_count--;
|
---|
| 207 | } else {
|
---|
| 208 | Configuration.setString(affectedProperty+value_count, true, value);
|
---|
| 209 | } // else retain old value for this affectedProperty (e.g. general.gliserver_url)
|
---|
| 210 | }
|
---|
[15505] | 211 | }
|
---|
| 212 |
|
---|
| 213 | private void setComboBoxValues() {
|
---|
[16337] | 214 | // All comboboxes are of the same size
|
---|
| 215 | Dimension newSize=new Dimension(m_serverComboBox.getPreferredSize().width+20, m_serverComboBox.getPreferredSize().height);
|
---|
| 216 | m_passwordField.setPreferredSize(newSize);
|
---|
| 217 |
|
---|
| 218 | setComboBoxValues(m_serverComboBox, "fedora.server", newSize);
|
---|
| 219 | setComboBoxValues(m_protocolComboBox, "fedora.protocol", newSize);
|
---|
| 220 | setComboBoxValues(m_usernameComboBox, "fedora.username", newSize);
|
---|
| 221 | newSize = null;
|
---|
[15505] | 222 | }
|
---|
[16337] | 223 |
|
---|
| 224 | private void setComboBoxValues(JComboBox control, String affectedProperty, Dimension newSize) {
|
---|
| 225 | // get values from Configuration file, if none, it will go back to using defaultvalues
|
---|
| 226 | // put them into the combobox
|
---|
| 227 |
|
---|
| 228 | String value = Configuration.getString(affectedProperty, true);
|
---|
| 229 | boolean finished = value.equals("");
|
---|
| 230 | if(!finished) {
|
---|
| 231 | control.removeAllItems(); // remove defaults, since config file now contains init values
|
---|
| 232 | control.addItem(value);
|
---|
| 233 | } // else value and combobox already contains the defaults
|
---|
| 234 |
|
---|
| 235 |
|
---|
| 236 | for(int i = 1; !finished; i++) {
|
---|
| 237 | value = Configuration.getString(affectedProperty+i, true);
|
---|
| 238 | if(value.equals("")) {
|
---|
| 239 | finished = true;
|
---|
| 240 | }
|
---|
| 241 | else { // value is not empty
|
---|
| 242 | control.addItem(value);
|
---|
| 243 | }
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | // setting the size of the dropdown control
|
---|
| 247 | control.setPreferredSize(newSize);
|
---|
| 248 | }
|
---|
[15505] | 249 |
|
---|
| 250 | public void addLabelValueRows(JLabel[] labels, JComponent[] values,
|
---|
[16337] | 251 | GridBagLayout gridBag, Container container) {
|
---|
[15505] | 252 | GridBagConstraints c=new GridBagConstraints();
|
---|
| 253 | c.insets=new Insets(0, 6, 6, 6);
|
---|
| 254 | for (int i=0; i<labels.length; i++) {
|
---|
| 255 | c.anchor=GridBagConstraints.EAST;
|
---|
| 256 | c.gridwidth=GridBagConstraints.RELATIVE; //next-to-last
|
---|
| 257 | c.fill=GridBagConstraints.NONE; //reset to default
|
---|
| 258 | c.weightx=0.0; //reset to default
|
---|
| 259 | gridBag.setConstraints(labels[i], c);
|
---|
| 260 | container.add(labels[i]);
|
---|
[16337] | 261 |
|
---|
[15505] | 262 | c.gridwidth=GridBagConstraints.REMAINDER; //end row
|
---|
| 263 | if (!(values[i] instanceof JComboBox)) {
|
---|
| 264 | c.fill=GridBagConstraints.HORIZONTAL;
|
---|
| 265 | } else {
|
---|
| 266 | c.anchor=GridBagConstraints.WEST;
|
---|
| 267 | }
|
---|
| 268 | c.weightx=1.0;
|
---|
| 269 | gridBag.setConstraints(values[i], c);
|
---|
| 270 | container.add(values[i]);
|
---|
| 271 | }
|
---|
[16337] | 272 |
|
---|
[15505] | 273 | }
|
---|
| 274 |
|
---|
| 275 | /** Displays the given errormessage (which is split over several lines)
|
---|
| 276 | * in the FedoraLogin itself
|
---|
| 277 | * @param errorLines: the lines of the error message, where each line
|
---|
| 278 | * will be presented in its own JLabel in the infoPane.
|
---|
| 279 | */
|
---|
| 280 | public void setErrorMessage(String[] errorLines) {
|
---|
| 281 | m_infoPane.removeAll();
|
---|
| 282 | m_infoPane.setBackground(LIGHT);
|
---|
| 283 | // n rows and 1 column
|
---|
| 284 | m_infoPane.setLayout(new java.awt.GridLayout(errorLines.length, 1));
|
---|
| 285 | for(int i = 0; i < errorLines.length; i++) {
|
---|
| 286 | JLabel line = new JLabel(" " + errorLines[i] + " ");
|
---|
| 287 | m_infoPane.add(line);
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | // Adjust and resize this dialog to take into account the
|
---|
| 291 | // recently added components (labels)
|
---|
| 292 | m_infoPane.validate();
|
---|
| 293 | this.pack();
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | public class PasswordChangeListener
|
---|
| 297 | implements DocumentListener {
|
---|
| 298 |
|
---|
| 299 | private JButton m_loginButton;
|
---|
| 300 | private JPasswordField m_passField;
|
---|
| 301 |
|
---|
| 302 | public PasswordChangeListener(JButton loginButton, JPasswordField pf) {
|
---|
| 303 | m_loginButton=loginButton;
|
---|
| 304 | m_passField=pf;
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | public void changedUpdate(DocumentEvent e) {
|
---|
| 308 | dataChanged();
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | public void insertUpdate(DocumentEvent e) {
|
---|
| 312 | dataChanged();
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | public void removeUpdate(DocumentEvent e) {
|
---|
| 316 | dataChanged();
|
---|
| 317 | }
|
---|
[16337] | 318 |
|
---|
[15505] | 319 | public void dataChanged() {
|
---|
[16337] | 320 | if (m_passField.getPassword().length == 0) {
|
---|
[15505] | 321 | m_loginButton.setEnabled(false);
|
---|
| 322 | } else {
|
---|
| 323 | m_loginButton.setEnabled(true);
|
---|
| 324 | }
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 |
|
---|
| 330 | public class LoginAction
|
---|
| 331 | extends AbstractAction {
|
---|
| 332 |
|
---|
| 333 | FedoraLogin m_loginDialog;
|
---|
| 334 | JButton m_button;
|
---|
| 335 |
|
---|
| 336 | public LoginAction(FedoraLogin loginDialog) {
|
---|
| 337 | super("Login");
|
---|
| 338 | m_loginDialog=loginDialog;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | public void setButton(JButton button) {
|
---|
| 342 | m_button=button;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | public void actionPerformed(ActionEvent evt) {
|
---|
| 346 |
|
---|
| 347 |
|
---|
| 348 | if (m_button.isEnabled()) {
|
---|
| 349 |
|
---|
| 350 | try {
|
---|
| 351 | // pull out values and do a quick syntax check
|
---|
| 352 | String hostPort=(String) m_serverComboBox.getSelectedItem();
|
---|
| 353 | int colonPos=hostPort.indexOf(":");
|
---|
| 354 | if (colonPos==-1) {
|
---|
| 355 | throw new IOException("Server must be specified as host:port");
|
---|
| 356 | }
|
---|
| 357 | String[] s=hostPort.split(":");
|
---|
| 358 | String host=s[0];
|
---|
| 359 | if (host.length()==0) {
|
---|
| 360 | throw new IOException("No server name provided.");
|
---|
| 361 | }
|
---|
| 362 | int port=0;
|
---|
| 363 | try {
|
---|
| 364 | port=Integer.parseInt(s[1]);
|
---|
| 365 | } catch (NumberFormatException nfe) {
|
---|
| 366 | throw new IOException("Server port must be an integer.");
|
---|
| 367 | }
|
---|
| 368 | String protocol=(String) m_protocolComboBox.getSelectedItem();
|
---|
| 369 | if (protocol.equals("")) {
|
---|
| 370 | throw new IOException("No protocol provided.");
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | String username=(String) m_usernameComboBox.getSelectedItem();
|
---|
| 374 | if (username.equals("")) {
|
---|
| 375 | throw new IOException("No username provided.");
|
---|
| 376 | }
|
---|
| 377 | String pass = new String(m_passwordField.getPassword());
|
---|
| 378 |
|
---|
| 379 |
|
---|
| 380 | // all looks ok...just save stuff and exit now
|
---|
[16337] | 381 | m_lastServer=host + ":" + port;
|
---|
[15505] | 382 | m_lastProtocol=protocol;
|
---|
| 383 | m_lastUsername=username;
|
---|
| 384 | m_lastPassword=pass;
|
---|
| 385 |
|
---|
[16337] | 386 | m_loginDialog.saveProperties(m_serverComboBox, "fedora.server", m_lastServer);
|
---|
| 387 | m_loginDialog.saveProperties(m_protocolComboBox, "fedora.protocol", m_lastProtocol);
|
---|
| 388 | m_loginDialog.saveProperties(m_usernameComboBox, "fedora.username", m_lastUsername);
|
---|
| 389 |
|
---|
| 390 | // now save these values to the Configuration file
|
---|
| 391 | Configuration.save();
|
---|
[15505] | 392 |
|
---|
| 393 | login_requested = true;
|
---|
| 394 |
|
---|
| 395 | // hiding it instead of disposing it on OK-press allows us to loop
|
---|
| 396 | // on displaying the dialog in case there's more checking to do
|
---|
| 397 | m_loginDialog.setVisible(false);
|
---|
| 398 |
|
---|
| 399 | } catch (Exception e) {
|
---|
| 400 | //String msg = e.getMessage();
|
---|
| 401 | //System.err.println(msg);
|
---|
| 402 |
|
---|
| 403 | e.printStackTrace();
|
---|
| 404 |
|
---|
| 405 | }
|
---|
| 406 | }
|
---|
| 407 | }
|
---|
| 408 | }
|
---|
| 409 |
|
---|
| 410 | public boolean loginRequested()
|
---|
| 411 | {
|
---|
| 412 | return login_requested;
|
---|
| 413 | }
|
---|
| 414 |
|
---|
| 415 | public String getLibraryURL()
|
---|
| 416 | {
|
---|
| 417 | String libraryUrl = m_lastProtocol + "://" + m_lastServer + "/fedora/search";
|
---|
| 418 |
|
---|
| 419 | return libraryUrl;
|
---|
| 420 | }
|
---|
| 421 |
|
---|
| 422 | public String getHostname()
|
---|
| 423 | {
|
---|
| 424 | String[] s=m_lastServer.split(":");
|
---|
| 425 | String host=s[0];
|
---|
| 426 | return host;
|
---|
| 427 | }
|
---|
| 428 | public String getPort()
|
---|
| 429 | {
|
---|
| 430 | String[] s=m_lastServer.split(":");
|
---|
| 431 |
|
---|
| 432 | String port = s[1];
|
---|
| 433 | return port;
|
---|
| 434 | }
|
---|
| 435 |
|
---|
| 436 | public String getUsername()
|
---|
| 437 | {
|
---|
| 438 | return m_lastUsername;
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 | public String getPassword()
|
---|
| 442 | {
|
---|
| 443 | return m_lastPassword;
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 | public String getProtocol()
|
---|
| 447 | {
|
---|
| 448 | return m_lastProtocol;
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 |
|
---|
| 452 | }
|
---|