/**
 *#########################################################################
 *
 * 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, 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.gui;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import org.greenstone.gatherer.Configuration;
import org.greenstone.gatherer.Dictionary;
import org.greenstone.gatherer.Gatherer;
import org.greenstone.gatherer.collection.BasicCollectionConfiguration;
import org.greenstone.gatherer.util.StaticStrings;
import org.greenstone.gatherer.util.Utility;

/** A dialog which provides a straight-forward access to the currently installed collections. It also will contain the ability to continue through to the original OpenCollectionDialog if your source collection is located somewhere other than the gsdl collect folder. */
public class OpenCollectionDialog
    extends ModalDialog {

    static final public int OK_OPTION = 0;
    static final public int CANCEL_OPTION = 1;

    static final private Dimension SIZE = new Dimension(640,480);
    static final private String BLANK = "b";
    static final private String DESCRIPTION = "d";

    private CardLayout card_layout;
    private int result;
    private JButton cancel_button;
    private JButton open_button;
    private JList collection_list;
    private JTextArea description_textarea;
    private JPanel description_pane;
    private String filename;

    public OpenCollectionDialog() {
	super(Gatherer.g_man, "", true);
	setJMenuBar(new SimpleMenuBar("openingacollection"));
	setSize(SIZE);
	setTitle(Dictionary.get("OpenCollectionDialog.Title"));
	
	// Creation
	JPanel content_pane = (JPanel) getContentPane();

	JPanel center_pane = new JPanel();

	JPanel collection_list_pane = new JPanel();
	JLabel collection_list_label = new JLabel(Dictionary.get("OpenCollectionDialog.Available_Collections"));
	collection_list = new JList(new CollectionListModel());
	description_pane = new JPanel();
	card_layout = new CardLayout();

	JPanel blank_pane = new JPanel();

	JPanel description_textarea_pane = new JPanel();
	JLabel description_textarea_label = new JLabel(Dictionary.get("OpenCollectionDialog.Description"));
	description_textarea = new JTextArea();

	JPanel button_pane = new JPanel();
	open_button = new GLIButton(Dictionary.get("OpenCollectionDialog.Open"), Dictionary.get("OpenCollectionDialog.Open_Tooltip"));
	open_button.setEnabled(false);
	
	cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Pure_Cancel_Tooltip"));
	
	// Connection
	cancel_button.addActionListener(new CancelListener());
	open_button.addActionListener(new OpenListener());
	CollectionListSelectionListener clsl = new CollectionListSelectionListener();
	collection_list.addListSelectionListener(clsl);
	collection_list.addMouseListener(clsl);
	clsl = null;

	// Layout
	collection_list_pane.setLayout(new BorderLayout());
	collection_list_pane.add(collection_list_label, BorderLayout.NORTH);
	collection_list_pane.add(new JScrollPane(collection_list), BorderLayout.CENTER);

	description_textarea_pane.setLayout(new BorderLayout());
	description_textarea_pane.add(description_textarea_label, BorderLayout.NORTH);
	description_textarea_pane.add(new JScrollPane(description_textarea), BorderLayout.CENTER);

	description_pane.setLayout(card_layout);
	description_pane.add(description_textarea_pane, DESCRIPTION);
	description_pane.add(blank_pane, BLANK);

	center_pane.setLayout(new GridLayout(2,1,0,5));
	center_pane.add(collection_list_pane);
	center_pane.add(description_pane);

	button_pane.setLayout(new GridLayout(1,4));
	button_pane.add(new JPanel());
	button_pane.add(open_button);
	button_pane.add(cancel_button);

	content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
	content_pane.setLayout(new BorderLayout());
	content_pane.add(center_pane, BorderLayout.CENTER);
	content_pane.add(button_pane, BorderLayout.SOUTH);

	Dimension screen_size = Configuration.screen_size;
	setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
	screen_size = null;
    }

    public void destroy() {
    }

    public int display() {
	setVisible(true);
	return result;
    }

    public String getFileName() {
	return this.filename;
    }

    private class CancelListener
	implements ActionListener {

	public void actionPerformed(ActionEvent event) {
	    result = CANCEL_OPTION;
	    OpenCollectionDialog.this.dispose();
	}
    }

    private class CollectionListSelectionListener
	extends MouseAdapter
	implements ListSelectionListener {

	public void mouseClicked(MouseEvent event) {
	    ///ystem.err.println("Mouse clicked");
	    if(event.getClickCount() >= 2) {
		Point location = event.getPoint();
		int index = collection_list.locationToIndex(location);
		collection_list.setSelectedIndex(index);
		location = null;
		open_button.doClick();
	    }
	}

	public void valueChanged(ListSelectionEvent event) {
	    if(collection_list.isSelectionEmpty()) {
		card_layout.show(description_pane, BLANK);
		open_button.setEnabled(false);
	    }
	    else {
		BasicCollectionConfiguration collection_configuration = (BasicCollectionConfiguration) collection_list.getSelectedValue();
		description_textarea.setText(collection_configuration.getDescription());
		description_textarea.setCaretPosition(0);
		card_layout.show(description_pane, DESCRIPTION);
		open_button.setEnabled(true);
	    }
	}
    }

    // use this if we ever go back to viewing all colls at once
    /*    private class GS3CollectionListModel
	extends AbstractListModel {

	private TreeSet data;

	public GS3CollectionListModel() {
	    data = new TreeSet();
	    File sites_folder = new File(Utility.getSitesDir(Configuration.gsdl3_path));
	    File sites[] = sites_folder.listFiles();
	    for (int s=0; s<sites.length; s++) {
		String site_name = sites[s].getName();
		System.err.println("found site "+site_name);
		File collect_folder = new File(Utility.getCollectDir(Configuration.gsdl3_path, site_name) );
		if (!collect_folder.exists()) {
		    continue;
		}
		File[] collection_folders = collect_folder.listFiles();
		for(int i = 0; i < collection_folders.length; i++) {
		    File collection_folder = collection_folders[i];
		    String collection_foldername = collection_folder.getName();
		    if(!collection_folder.isFile() && !collection_foldername.equals(StaticStrings.MODEL_COLLECTION_NAME)) {
			BasicCollectionConfiguration collection_configuration = new BasicCollectionConfiguration(new File(collection_folder, Utility.CONFIG_FILE), site_name);
			if(!collection_configuration.getName().equals(StaticStrings.ERROR_STR)) {
			    data.add(collection_configuration);
			}
		    }
		    collection_foldername = null;
		    collection_folder = null;
		} // for each collection
		collection_folders = null;
		collect_folder = null;
	    } // for each site
	    sites_folder = null;
	    sites = null;
	}

	public Object getElementAt(int index) {
	    Iterator iterator = data.iterator();
	    for(int i = 0; iterator.hasNext(); i++) {
		Object object = iterator.next();
		if(i == index) {
		    iterator = null;
		    return object;
		}
		object = null;
	    }
	    iterator = null;
	    return null;
	}

	public int getSize() {
	    return data.size();
	}
    }
    */	
    private class CollectionListModel
	extends AbstractListModel {

	private TreeSet data;

	public CollectionListModel() {
	    data = new TreeSet();
	    File collect_directory = new File(Gatherer.getCollectDirectoryPath());
	    if (!collect_directory.exists()) {
		collect_directory = null;
		return;
	    }
	    File[] collection_folders = collect_directory.listFiles();
	    for(int i = 0; i < collection_folders.length; i++) {
		File collection_folder = collection_folders[i];
		String collection_foldername = collection_folder.getName();
		if(collection_folder.isDirectory() && !collection_foldername.equals(StaticStrings.MODEL_COLLECTION_NAME)) {
		    File config_file = new File(collection_folder, Utility.CONFIG_FILE);
		    if (config_file.exists()) {
			BasicCollectionConfiguration collection_configuration = new BasicCollectionConfiguration(config_file);
			data.add(collection_configuration);
		    }
		    config_file = null;
		}
		collection_foldername = null;
		collection_folder = null;
	    }
	    collection_folders = null;
	    collect_directory = null;
	}

	public Object getElementAt(int index) {
	    Iterator iterator = data.iterator();
	    for(int i = 0; iterator.hasNext(); i++) {
		Object object = iterator.next();
		if(i == index) {
		    iterator = null;
		    return object;
		}
		object = null;
	    }
	    iterator = null;
	    return null;
	}

	public int getSize() {
	    return data.size();
	}
    }

    private class OpenListener
	implements ActionListener {

	public void actionPerformed(ActionEvent event) {
	    result = OK_OPTION;
	    Object selected_object = collection_list.getSelectedValue();
	    if (selected_object instanceof BasicCollectionConfiguration) {
		BasicCollectionConfiguration collection_configuration = (BasicCollectionConfiguration)selected_object; //(BasicCollectionConfiguration) collection_list.getSelectedValue();
		//*************888
		File collect_cfg_file = collection_configuration.getFile(); // returns the collect.cfg file
		File etc_folder = collect_cfg_file.getParentFile();
		File collection_folder = etc_folder.getParentFile();
		filename = collection_folder.getAbsolutePath() + File.separator + collection_folder.getName() + ".col";
		collection_folder = null;
		etc_folder = null;
		collect_cfg_file = null;
		collection_configuration = null;
		OpenCollectionDialog.this.dispose();
	    }
	}
    }
}
