/* * ----------------------------------------------------------------------------- * *

License and Copyright: The contents of this file are subject * to the Educational Community License (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy * of the License at * http://www.opensource.org/licenses/ecl1.txt.

* *

Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the * License for the specific language governing rights and limitations under * the License.

* *

The entire file consists of original code. Copyright © * 2002-2007 by The Rector and Visitors of the University of Virginia and * Cornell University. All rights reserved.

* * ----------------------------------------------------------------------------- */ /* Based on Fedora's Client LoginDialog.java code */ /* Modified to work with Greenstone: 22 Jan 2008 */ package org.greenstone.gatherer.gui; import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Properties; import javax.swing.AbstractAction; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import java.awt.Rectangle; import java.awt.Toolkit; import org.greenstone.gatherer.Dictionary; import org.greenstone.gatherer.Gatherer; import org.greenstone.gatherer.Configuration; public class FedoraLogin extends JDialog { private static final int MAX_ITEMS = 5; private static final java.awt.Color LIGHT = new java.awt.Color(244, 244, 224); private static final java.awt.Color TRANSPARENT = new java.awt.Color(0,0,0,0); private JPanel m_infoPane; private JComboBox m_serverComboBox; private JComboBox m_protocolComboBox; private JComboBox m_usernameComboBox; private JPasswordField m_passwordField; private String m_lastUsername="fedoraAdmin"; private String m_lastServer="localhost:8080"; private String m_lastProtocol="http"; private String m_lastPassword=""; private boolean login_requested = false; public FedoraLogin(String title, boolean can_cancel) { super(Gatherer.g_man, "Login", true); this.setComponentOrientation(Dictionary.getOrientation()); this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); JLabel serverLabel=new JLabel("Fedora Server"); JLabel protocolLabel=new JLabel("Protocol"); JLabel usernameLabel=new JLabel("Username"); JLabel passwordLabel=new JLabel("Password"); serverLabel.setComponentOrientation(Dictionary.getOrientation()); protocolLabel.setComponentOrientation(Dictionary.getOrientation()); usernameLabel.setComponentOrientation(Dictionary.getOrientation()); passwordLabel.setComponentOrientation(Dictionary.getOrientation()); m_serverComboBox=new JComboBox(new String[]{m_lastServer}); m_serverComboBox.setComponentOrientation(Dictionary.getOrientation()); m_serverComboBox.setEditable(true); m_protocolComboBox=new JComboBox(new String[]{m_lastProtocol, "https"}); // http and https m_protocolComboBox.setComponentOrientation(Dictionary.getOrientation()); m_protocolComboBox.setEditable(true); m_usernameComboBox=new JComboBox(new String[]{m_lastUsername}); m_usernameComboBox.setComponentOrientation(Dictionary.getOrientation()); m_usernameComboBox.setEditable(true); m_passwordField=new JPasswordField(); setComboBoxValues(); LoginAction loginAction=new LoginAction(this); JButton loginButton=new JButton(loginAction); loginAction.setButton(loginButton); loginButton.setEnabled(false); this.getRootPane().setDefaultButton(loginButton); m_passwordField.getDocument().addDocumentListener( new PasswordChangeListener(loginButton, m_passwordField)); m_passwordField.setAction(loginAction); JPanel inputPane=new JPanel(); inputPane.setComponentOrientation(Dictionary.getOrientation()); inputPane.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createCompoundBorder( BorderFactory.createEmptyBorder(6, 6, 6, 6), BorderFactory.createEtchedBorder() ), BorderFactory.createEmptyBorder(6,6,6,6) )); GridBagLayout gridBag=new GridBagLayout(); inputPane.setLayout(gridBag); addLabelValueRows(new JLabel[] {serverLabel, protocolLabel, usernameLabel, passwordLabel}, new JComponent[] {m_serverComboBox, m_protocolComboBox, m_usernameComboBox, m_passwordField}, gridBag, inputPane); // handling Closing and cancelling events this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e){ // this is important, if we want to stop looping on displaying the dialog: login_requested = false; //dispose(); // defaultCloseOperation of this dialog already set to dispose it } }); JButton cancelButton=new JButton(new AbstractAction() { private static final long serialVersionUID = 1L; public void actionPerformed(ActionEvent evt) { // this is important, if we want to stop looping on displaying the dialog: login_requested = false; dispose(); } }); cancelButton.setText("Exit"); // if haven't logged in yet JPanel buttonPane=new JPanel(); buttonPane.setComponentOrientation(Dictionary.getOrientation()); buttonPane.add(loginButton); buttonPane.add(cancelButton); Container contentPane=getContentPane(); contentPane.setComponentOrientation(Dictionary.getOrientation()); contentPane.setLayout(new BorderLayout()); m_infoPane = new JPanel(); m_infoPane.setComponentOrientation(Dictionary.getOrientation()); m_infoPane.setBackground(TRANSPARENT); contentPane.add(m_infoPane, BorderLayout.NORTH); contentPane.add(inputPane, BorderLayout.CENTER); contentPane.add(buttonPane, BorderLayout.SOUTH); addWindowListener(new WindowAdapter() { public void windowOpened(WindowEvent evt) { m_passwordField.requestFocus(); } }); pack(); // Position Dimension size = getSize(); if (Gatherer.g_man != null) { Rectangle frame_bounds = Gatherer.g_man.getBounds(); setLocation(frame_bounds.x + (frame_bounds.width - size.width) / 2, frame_bounds.y + (frame_bounds.height - size.height) / 2); } else { Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize(); setLocation((screen_size.width - size.width) / 2, (screen_size.height - size.height) / 2); } setVisible(true); } public void saveProperties(JComboBox control, String affectedProperty, String editedValue) { //String editedValue = (String)control.getSelectedItem(); // Edited value is the value in the combobox editfield, it has not yet been added to the combobox control.insertItemAt(editedValue, 0); if(editedValue == null) { editedValue = (String)control.getItemAt(0); // else reuse the first default } Configuration.setString(affectedProperty, true, editedValue); // Add the values in the combobox as well. int value_count = 1; for(int i = 0; value_count < MAX_ITEMS && i < control.getItemCount(); i++, value_count++) { String value = (String)control.getItemAt(i); if(value == null || value.equals(editedValue) || value.equals("")) { // skip duplicates of just-edited comboBox value and empty values value_count--; } else { Configuration.setString(affectedProperty+value_count, true, value); } // else retain old value for this affectedProperty (e.g. general.gliserver_url) } } private void setComboBoxValues() { // All comboboxes are of the same size Dimension newSize=new Dimension(m_serverComboBox.getPreferredSize().width+20, m_serverComboBox.getPreferredSize().height); m_passwordField.setPreferredSize(newSize); setComboBoxValues(m_serverComboBox, "fedora.server", newSize); setComboBoxValues(m_protocolComboBox, "fedora.protocol", newSize); setComboBoxValues(m_usernameComboBox, "fedora.username", newSize); newSize = null; } private void setComboBoxValues(JComboBox control, String affectedProperty, Dimension newSize) { // get values from Configuration file, if none, it will go back to using defaultvalues // put them into the combobox String value = Configuration.getString(affectedProperty, true); boolean finished = value.equals(""); if(!finished) { control.removeAllItems(); // remove defaults, since config file now contains init values control.addItem(value); } // else value and combobox already contains the defaults for(int i = 1; !finished; i++) { value = Configuration.getString(affectedProperty+i, true); if(value.equals("")) { finished = true; } else { // value is not empty control.addItem(value); } } // setting the size of the dropdown control control.setPreferredSize(newSize); } public void addLabelValueRows(JLabel[] labels, JComponent[] values, GridBagLayout gridBag, Container container) { GridBagConstraints c=new GridBagConstraints(); c.insets=new Insets(0, 6, 6, 6); for (int i=0; i