Index: /gli/trunk/src/org/greenstone/gatherer/FedoraInfo.java
===================================================================
--- /gli/trunk/src/org/greenstone/gatherer/FedoraInfo.java	(revision 14975)
+++ /gli/trunk/src/org/greenstone/gatherer/FedoraInfo.java	(revision 14975)
@@ -0,0 +1,135 @@
+/**
+ *#########################################################################
+ *
+ * A component of the Gatherer application, part of the Greenstone digital
+ * library suite from the New Zealand Digital Library Project at the
+ * University of Waikato, New Zealand.
+ *
+ * Author: John Thompson and David Bainbridge, 
+ *         Greenstone Digital Library, University of Waikato
+ *
+ * Copyright (C) 1999 New Zealand Digital Library Project
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *########################################################################
+ */
+
+package org.greenstone.gatherer;
+
+import java.io.*;
+import org.greenstone.gatherer.util.StaticStrings; 
+import org.greenstone.gatherer.util.Utility;
+
+public class FedoraInfo 
+{
+    public String home = null;
+
+    public String hostname = null;
+    public String port     = null;
+    public String username = null;
+    public String password = null;
+    public String protocol = null;
+
+    public FedoraInfo() 
+    {	
+	this.hostname ="localhost";
+	this.port     = "8080";
+	this.username = "fedoraAdmin";
+	this.password = "fedoraAdmin";
+	this.protocol = "http";
+    }
+
+    public FedoraInfo(String hostname, String port, String username, String password, String protocol) 
+    {
+	this.hostname = hostname;
+	this.port     = port;
+	this.username = username;
+	this.password = password;
+	this.protocol = protocol;
+    }
+
+
+    public boolean isActive()
+    {
+	return home != null;
+    }
+
+    public void setHome(String home)
+    {
+	this.home = home;
+    }
+
+
+    public String getHome()
+    {
+	return home;
+    }
+
+
+    public void setHostname(String hostname)
+    {
+	this.hostname = hostname;
+    }
+
+    public String getHostname()
+    {
+	return hostname;
+    }
+
+
+    public void setPort(String port)
+    {
+	this.port = port;
+    }
+
+    public String getPort()
+    {
+	return port;
+    }
+
+
+    public void setUsername(String username)
+    {
+	this.username = username;
+    }
+
+    public String getUsername()
+    {
+	return username;
+    }
+
+
+    public void setPassword(String password)
+    {
+	this.password = password;
+    }
+
+    public String getPassword()
+    {
+	return password;
+    }
+
+
+    public void setProtocol(String protocol)
+    {
+	this.protocol = protocol;
+    }
+
+    public String getProtocol()
+    {
+	return protocol;
+    }
+
+}
Index: /gli/trunk/src/org/greenstone/gatherer/gui/FedoraLogin.java
===================================================================
--- /gli/trunk/src/org/greenstone/gatherer/gui/FedoraLogin.java	(revision 14975)
+++ /gli/trunk/src/org/greenstone/gatherer/gui/FedoraLogin.java	(revision 14975)
@@ -0,0 +1,462 @@
+/*
+ * -----------------------------------------------------------------------------
+ *
+ * <p><b>License and Copyright: </b>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 <a href="http://www.opensource.org/licenses/ecl1.txt">
+ * http://www.opensource.org/licenses/ecl1.txt.</a></p>
+ *
+ * <p>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.</p>
+ *
+ * <p>The entire file consists of original code.  Copyright &copy;
+ * 2002-2007 by The Rector and Visitors of the University of Virginia and
+ * Cornell University.  All rights reserved.</p>
+ *
+ * -----------------------------------------------------------------------------
+ */
+
+
+/* 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.Gatherer;
+
+public class FedoraLogin extends JDialog 
+{
+
+    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 HashMap m_usernames;
+    private HashMap m_servers;
+    private HashMap m_protocols;
+
+    private boolean login_requested = false;
+
+    public FedoraLogin(String title, boolean can_cancel) 
+    {
+	super(Gatherer.g_man, "Login", true);
+        
+        m_servers=new HashMap();
+        m_protocols=new HashMap();
+        m_protocols.put("http", "");
+        m_protocols.put("https", "");
+        m_usernames=new HashMap();
+
+        JLabel serverLabel=new JLabel("Fedora Server");
+		JLabel protocolLabel=new JLabel("Protocol");
+        JLabel usernameLabel=new JLabel("Username");
+        JLabel passwordLabel=new JLabel("Password");
+
+        m_serverComboBox=new JComboBox();
+        m_serverComboBox.setEditable(true);
+		m_protocolComboBox=new JComboBox();
+		m_protocolComboBox.setEditable(true);
+        m_usernameComboBox=new JComboBox();
+        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);
+	
+
+        m_passwordField.getDocument().addDocumentListener(
+                new PasswordChangeListener(loginButton, m_passwordField));
+        m_passwordField.setAction(loginAction);
+
+        JPanel inputPane=new JPanel();
+        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);
+
+        JButton cancelButton=new JButton(new AbstractAction() {
+        	private static final long serialVersionUID = 1L;
+            public void actionPerformed(ActionEvent evt) {
+                dispose();
+            }
+        });
+
+	cancelButton.setText("Exit"); // if haven't logged in yet
+
+        JPanel buttonPane=new JPanel();
+        buttonPane.add(loginButton);
+        buttonPane.add(cancelButton);
+        Container contentPane=getContentPane();
+        contentPane.setLayout(new BorderLayout());
+        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);
+    }
+
+    // re-writes fedora-admin.properties with latest values for servers 
+    // and usernames
+
+    
+    public void saveProperties() {
+        try {
+            Properties props=new Properties();
+            props.setProperty("lastServer", m_lastServer);
+			props.setProperty("lastProtocol", m_lastProtocol);
+            props.setProperty("lastUsername", m_lastUsername);
+            Iterator iter;
+            int i;
+            iter=m_servers.keySet().iterator();
+            i=0;
+            while (iter.hasNext()) {
+                String name=(String) iter.next();
+                props.setProperty("server" + i, name);
+                i++;
+            }
+			iter=m_protocols.keySet().iterator();
+			i=0;
+			while (iter.hasNext()) {
+				String name=(String) iter.next();
+				props.setProperty("protocol" + i, name);
+				i++;
+			}
+            iter=m_usernames.keySet().iterator();
+            i=0;
+            while (iter.hasNext()) {
+                String name=(String) iter.next();
+                props.setProperty("username" + i, name);
+                i++;
+            }
+            props.store(new FileOutputStream(new File(Gatherer.getGLIUserDirectoryPath(), "fedora-admin.properties")), "Fedora Administrator saved settings");
+        } catch (Exception e) {
+            System.err.println("Warning: Error writing properties: " 
+                    + e.getClass().getName() + ": " + e.getMessage());
+        }
+    }
+    
+
+    private void setComboBoxValues() {
+        // get values from prop file, or use localhost:8080/fedoraAdmin if none
+        try {
+            Properties props=new Properties();
+            props.load(new FileInputStream(new File(Gatherer.getGLIUserDirectoryPath(), "fedora-admin.properties")));
+            Enumeration names=props.propertyNames();
+            while (names.hasMoreElements()) {
+                String prop=(String) names.nextElement();
+                if (prop.equals("lastServer")) {
+                    m_lastServer=props.getProperty(prop);
+				} else if (prop.equals("lastProtocol")) {
+					m_lastProtocol=props.getProperty(prop);
+                } else if (prop.equals("lastUsername")) {
+                    m_lastUsername=props.getProperty(prop);
+                } else if (prop.startsWith("server")) {
+                    m_servers.put(props.getProperty(prop), "");
+				} else if (prop.startsWith("protocol")) {
+					m_protocols.put(props.getProperty(prop), "");
+                } else if (prop.startsWith("username")) {
+                    m_usernames.put(props.getProperty(prop), "");
+                }
+            }
+        } catch (Exception e) {
+            // no problem if props file doesn't exist...we have defaults
+        }
+        // finally, populate them
+        m_serverComboBox.addItem(m_lastServer);
+        Iterator sIter=m_servers.keySet().iterator();
+        while (sIter.hasNext()) {
+            String a=(String) sIter.next();
+            if (!a.equals(m_lastServer)) {
+                m_serverComboBox.addItem(a);
+            }
+        }
+        m_servers.put(m_lastServer, "");
+        
+		m_protocolComboBox.addItem(m_lastProtocol);
+		Iterator protocolIter=m_protocols.keySet().iterator();
+		while (protocolIter.hasNext()) {
+			String a=(String) protocolIter.next();
+			if (!a.equals(m_lastProtocol)) {
+				m_protocolComboBox.addItem(a);
+			}
+		}
+		m_protocols.put(m_lastProtocol, "");
+
+        m_usernameComboBox.addItem(m_lastUsername);
+        Iterator uIter=m_usernames.keySet().iterator();
+        while (uIter.hasNext()) {
+            String a=(String) uIter.next();
+            if (!a.equals(m_lastUsername)) {
+                m_usernameComboBox.addItem(a);
+            }
+        }
+        m_usernames.put(m_lastUsername, "");
+
+        // make all entry widgets same size
+        Dimension newSize=new Dimension(
+                m_serverComboBox.getPreferredSize().width+20,
+                m_serverComboBox.getPreferredSize().height);
+        m_serverComboBox.setPreferredSize(newSize);
+		m_protocolComboBox.setPreferredSize(newSize);
+        m_usernameComboBox.setPreferredSize(newSize);
+        m_passwordField.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<labels.length; i++) {
+            c.anchor=GridBagConstraints.EAST;
+            c.gridwidth=GridBagConstraints.RELATIVE; //next-to-last
+            c.fill=GridBagConstraints.NONE;      //reset to default
+            c.weightx=0.0;                       //reset to default
+            gridBag.setConstraints(labels[i], c);
+            container.add(labels[i]);
+
+            c.gridwidth=GridBagConstraints.REMAINDER;     //end row
+            if (!(values[i] instanceof JComboBox)) {
+                c.fill=GridBagConstraints.HORIZONTAL;
+            } else {
+                c.anchor=GridBagConstraints.WEST;
+            }
+            c.weightx=1.0;
+            gridBag.setConstraints(values[i], c);
+            container.add(values[i]);
+        }
+
+    }
+
+    public class PasswordChangeListener
+            implements DocumentListener {
+
+        private JButton m_loginButton;
+        private JPasswordField m_passField;
+
+        public PasswordChangeListener(JButton loginButton, JPasswordField pf) {
+            m_loginButton=loginButton;
+            m_passField=pf;
+        }
+
+        public void changedUpdate(DocumentEvent e) {
+            dataChanged();
+        }
+
+        public void insertUpdate(DocumentEvent e) {
+            dataChanged();
+        }
+
+        public void removeUpdate(DocumentEvent e) {
+            dataChanged();
+        }
+
+        public void dataChanged() {
+        	if (m_passField.getPassword().length == 0) { 
+                m_loginButton.setEnabled(false);
+            } else {
+                m_loginButton.setEnabled(true);
+            }
+        }
+
+    }
+
+    
+    
+      public class LoginAction
+	  extends AbstractAction {
+
+	  FedoraLogin m_loginDialog;
+	  JButton m_button;
+	  
+	  public LoginAction(FedoraLogin loginDialog) {
+	      super("Login");
+	      m_loginDialog=loginDialog;
+	  }
+	  
+	  public void setButton(JButton button) {
+            m_button=button;
+	  }
+	  
+	  public void actionPerformed(ActionEvent evt) {
+	     
+	    
+            if (m_button.isEnabled()) {
+
+                try {
+                    // pull out values and do a quick syntax check
+                    String hostPort=(String) m_serverComboBox.getSelectedItem();
+                    int colonPos=hostPort.indexOf(":");
+                    if (colonPos==-1) {
+                        throw new IOException("Server must be specified as host:port");
+                    }
+                    String[] s=hostPort.split(":");
+                    String host=s[0];
+                    if (host.length()==0) {
+                        throw new IOException("No server name provided.");
+                    }
+                    int port=0;
+                    try {
+                        port=Integer.parseInt(s[1]);
+                    } catch (NumberFormatException nfe) {
+                        throw new IOException("Server port must be an integer.");
+                    }
+		    String protocol=(String) m_protocolComboBox.getSelectedItem();
+		    if (protocol.equals("")) {
+			throw new IOException("No protocol provided.");
+		    }
+
+                    String username=(String) m_usernameComboBox.getSelectedItem();
+                    if (username.equals("")) {
+                        throw new IOException("No username provided.");
+                    }
+                    String pass = new String(m_passwordField.getPassword());
+
+
+                    // all looks ok...just save stuff and exit now
+                    m_lastServer=host + ":" + port;
+                    m_lastProtocol=protocol;
+                    m_lastUsername=username;
+		    m_lastPassword=pass;
+
+                    m_loginDialog.saveProperties();
+
+
+		    login_requested = true;
+
+                    m_loginDialog.dispose();
+                } catch (Exception e) {
+                    //String msg = e.getMessage();
+		    //System.err.println(msg);
+
+		    e.printStackTrace();
+		    
+
+                }
+            }
+	  }
+    
+
+      }
+
+    public boolean loginRequested()
+    {
+	return login_requested;
+    }
+
+    public String getLibraryURL()
+    {
+	String libraryUrl = m_lastProtocol + "://" + m_lastServer + "/fedora/search";
+
+	return libraryUrl;
+    }
+
+    public String getHostname()
+    {
+	String[] s=m_lastServer.split(":");
+	String host=s[0];
+	return host;
+    }
+    public String getPort()
+    {
+	String[] s=m_lastServer.split(":");
+
+	String port = s[1];
+	return port;
+    }
+
+    public String getUsername()
+    {
+	return m_lastUsername;
+    }
+
+    public String getPassword()
+    {
+	return m_lastPassword;
+    }
+
+    public String getProtocol()
+    {
+	return m_lastProtocol;
+    }
+
+
+}
