/**
 *############################################################################
 * A component of the Greenstone Librarian Interface, part of the Greenstone
 * digital library suite from the New Zealand Digital Library Project at the
 * University of Waikato, New Zealand.
 *
 * Author: Michael Dewsnip, NZDL Project, University of Waikato, NZ
 *
 * Copyright (C) 2004 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.file;

import java.io.*;
import java.util.*;
import javax.swing.tree.TreePath;
import org.greenstone.gatherer.Configuration;
import org.greenstone.gatherer.DebugStream;
import org.greenstone.gatherer.Dictionary;
import org.greenstone.gatherer.Gatherer;
import org.greenstone.gatherer.util.SynchronizedTreeModelTools;


public class WorkspaceTreeModel
    extends FileSystemModel
{
    static private WorkspaceTreeModel workspace_tree_model = null;
    static private WorkspaceTreeNode workspace_tree_root = null;

    static private WorkspaceTreeNode greenstone_collections_node = null;
    static private WorkspaceTreeNode local_filespace_node = null;
    static private WorkspaceTreeNode downloaded_files_node = null;
    static private WorkspaceTreeNode[] folder_shortcuts = null;


    public WorkspaceTreeModel(WorkspaceTreeNode root_node)
    {
	super(root_node);
    }


    static public WorkspaceTreeNode[] getFolderShortcuts()
    {
	// Return any predefined special directories
	HashMap mappings = Configuration.getDirectoryMappings();
	WorkspaceTreeNode[] mapping_nodes = new WorkspaceTreeNode[mappings.size()];
	Iterator mappings_iterator = mappings.keySet().iterator();
	for (int i = 0; mappings_iterator.hasNext(); i++) {
	    String mapping_name = (String) mappings_iterator.next();
	    File mapping_file = (File) mappings.get(mapping_name);
	    mapping_nodes[i] = new WorkspaceTreeNode(mapping_file, mapping_name);
	}
	return mapping_nodes;
    }


    static public WorkspaceTreeModel getWorkspaceTreeModel()
    {
	// Create a root node to contain the various nodes in the workspace tree
 	workspace_tree_root = new WorkspaceTreeNode(null, "ABS_ROOT");
	workspace_tree_model = new WorkspaceTreeModel(workspace_tree_root);

	// Add the "Documents in Greenstone Collections" node
	greenstone_collections_node = new WorkspaceTreeNode(null, Dictionary.get("Tree.World"));
	workspace_tree_root.add(greenstone_collections_node);

	// Add the "Local Filespace" node
	local_filespace_node = FileSystem.getLocalFilespaceNode(workspace_tree_model);
	workspace_tree_root.add(local_filespace_node);

	// Add the "Home Folder" node
	workspace_tree_root.add(FileSystem.getHomeFolderNode());

	// Add the "Downloaded Files" node, if the Download pane is enabled
	if (Gatherer.g_man.download_pane != null) {
	    downloaded_files_node = FileSystem.getDownloadedFilesNode();
	    workspace_tree_root.add(downloaded_files_node);
	}

	// Add any folder shortcuts the user has created
	refreshFolderShortcuts();

 	return workspace_tree_model;
    }


    static public void refreshGreenstoneCollectionsNode()
    {
	greenstone_collections_node.refresh();
    }


    static public void refreshDownloadedFilesNode()
    {
	downloaded_files_node.refresh();
    }


    static public void refreshFolderShortcuts()
    {
	// Check for new/deleted folder shortcuts
	WorkspaceTreeNode[] old_folder_shortcuts = folder_shortcuts;
	folder_shortcuts = getFolderShortcuts();

	// Remove any deleted shortcuts from the tree
	if (old_folder_shortcuts != null) {
	    for (int i = 0; i < old_folder_shortcuts.length; i++) {
		if (!doesArrayContain(folder_shortcuts, old_folder_shortcuts[i])) {
		    DebugStream.println("Deleted shortcut: " + old_folder_shortcuts[i]);
		    SynchronizedTreeModelTools.removeNodeFromParent(workspace_tree_model, old_folder_shortcuts[i]);
		}
	    }
	}

	// Add any new shortcuts to the tree
	if (folder_shortcuts != null) {
	    for (int i = 0; i < folder_shortcuts.length; i++) {
		if (!doesArrayContain(old_folder_shortcuts, folder_shortcuts[i])) {
		    DebugStream.println("Added shortcut: " + folder_shortcuts[i]);
		    SynchronizedTreeModelTools.insertNodeInto(workspace_tree_model, workspace_tree_root, folder_shortcuts[i], false);
		}
	    }
	}
    }


    // -- This code is necessary to support the workspace tree file filter --
    public void refresh(TreePath path)
    {
	// If we're not refreshing the whole tree just refresh a certain path
	if (path != null) {
	    super.refresh(path);
	    return;
	}

	// Refresh each of the nodes in the workspace tree
	for (int i = 0; i < workspace_tree_root.getChildCount(); i++) {
	    WorkspaceTreeNode child_node = (WorkspaceTreeNode) workspace_tree_root.getChildAt(i);
	    super.refresh(new TreePath(child_node.getPath()));
	}
    }


    static private boolean doesArrayContain(Object[] array, Object item)
    {
	if (array == null) {
	    return false;
	}

	for (int i = 0; i < array.length; i++) {
	    if (item.toString().equals(array[i].toString())) {
		return true;
	    }
	}

	return false;
    }
}
