/
codebase/src/net/sourceforge/pain/admin/console/command/
codebase/src/net/sourceforge/pain/data/role/
codebase/src/net/sourceforge/pain/network/console/telnet/
codebase/src/net/sourceforge/pain/network/guitool/
codebase/src/net/sourceforge/pain/plugin/
codebase/src/net/sourceforge/pain/util/
db/src/net/sourceforge/pain/util/
gui/
gui/lib/
gui/src/net/sourceforge/pain/tools/guitool/dbbrowse/
gui/src/net/sourceforge/pain/tools/guitool/dialog/
gui/src/net/sourceforge/pain/tools/guitool/menu/
gui/src/net/sourceforge/pain/tools/guitool/resources/
gui/src/net/sourceforge/pain/tools/guitool/resources/images/
gui/src/net/sourceforge/pain/tools/guitool/resources/images/explorer/
mudlibs/tinylib/
mudlibs/tinylib/area/
mudlibs/tinylib/etc/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/data/affect/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/data/prototype/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/data/trigger/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/affect/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/deploy/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/guitool/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/guitool/event/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/fn/util/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/trigger/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/trigger/impl/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/command/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/reset/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/shutdown/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/social/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/util/
tests/
tests/src/
tests/src/net/sourceforge/pain/db/data/
package net.sourceforge.pain.tools.guitool;

import net.sourceforge.pain.tools.guitool.action.*;
import net.sourceforge.pain.tools.guitool.action.admin.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.awt.event.*;
import java.util.*;


public class Explorer {
    public final JTree tree;
    public final WorkspacePanel owner;
    public final ExplorerTreeNode root;

    Explorer(WorkspacePanel owner) {
        this.owner = owner;
        root = new ExplorerTreeNode("World", new WorldExplorerAction(), "root", null);
        ExplorerTreeNode server = new ExplorerTreeNode("Administration", new AdminSectionExplorerAction(), "server", root);
        new ExplorerTreeNode("Account Options", new AccountOptionsExplorerAction(), "account", server);
        new ExplorerTreeNode("Server Status", new ServerStatusExplorerAction(), "server", server);
        new ExplorerTreeNode("World Info", new TodoExplorerAction(), "info", server);
        new ExplorerTreeNode("Runtime Errors", new TodoExplorerAction(), "errors", server);
        new ExplorerTreeNode("Raw DB Browser", new RawDBBrowseAction(), "raw_db_browser", server);

        ExplorerTreeNode builder = new ExplorerTreeNode("Builder", new TodoExplorerAction(), "builder", root);
        new ExplorerTreeNode("todo", new TodoExplorerAction(), "todo", builder);

        ExplorerTreeNode code = new ExplorerTreeNode("Code Repository", new TodoExplorerAction(), "code", root);
        new ExplorerTreeNode("todo", new TodoExplorerAction(), "todo", code);

        tree = new JTree(root);

//        DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
//        renderer.setOpenIcon(GuiTool.loadImage("explorer/bluearrowo.gif", ""));
//        renderer.setClosedIcon(GuiTool.loadImage("explorer/bluearrowc.gif", ""));
//        renderer.setLeafIcon(GuiTool.loadImage("explorer/treeCellIcon.gif", ""));
//        renderer.setIcon(GuiTool.loadImage("explorer/treeCellIcon.gif", ""));
//        tree.setCellRenderer(renderer);

//        tree.setRootVisible(false);
        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        tree.addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent e) {
                onNodeSelected(e);
            }
        });

        tree.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                onMouseClickedOnTree(e);
            }
        });
    }

    private void onNodeSelected(TreeSelectionEvent e) {
        System.out.println("Selected:" + e.getPath());
        ExplorerTreeNode obj = getUserObject(e.getPath());
        if (obj != null && obj.action != null) {
            obj.action.executeAction();
        }
    }

    private void onMouseClickedOnTree(MouseEvent e) {
        System.out.println("Mouse clicked on:" + tree.getSelectionPath() + " clicks" + e.getClickCount());
        ExplorerTreeNode node = getUserObject(tree.getSelectionPath());
//        if (node != null && node.action != null) {
//            node.action.clickedInTree(e);
//        }
    }

    private ExplorerTreeNode getUserObject(TreePath p) {
        return p == null ? null : (ExplorerTreeNode) p.getLastPathComponent();
    }

    public void selectPath(String[] path) {
        ExplorerTreeNode objPath[] = new ExplorerTreeNode[path == null ? 1 : path.length + 1];
        objPath[0] = root;
        final int len = objPath.length;
        for (int i = 1; i < len; i++) {
            objPath[i] = objPath[i - 1].getChildByLocalPath(path[i - 1]);
            if (objPath[i] == null) {
                throw new NullPointerException("path not found!:" + path[i]);
            }
        }
        tree.setSelectionPath(new TreePath(objPath));

    }

    public void doDefaultAction(HyperlinkEvent e) {
        final String path = e.getURL().toString().substring("file://".length());
        selectPath(path.split("/"));
    }


    private class ExplorerTreeNode implements TreeNode {
        final String name;
        final GTAction action;
        final String localPath;

        final ExplorerTreeNode parent;
        private HashMap childsMap = new HashMap();
        private Vector childsList = new Vector();
        private TreePath treePath;

        public ExplorerTreeNode(String name, GTAction action, String localPath, ExplorerTreeNode parent) {
            this.name = name;
            this.action = action;
            this.localPath = localPath;
            this.parent = parent;
            if (parent != null) {
                parent.childsMap.put(localPath, this);
                parent.childsList.add(this);
            }
        }

        public String toString() {
            return name;
        }

        public int getChildCount() {
            return childsMap.size();
        }

        public boolean getAllowsChildren() {
            return true;
        }

        public boolean isLeaf() {
            return childsMap.isEmpty();
        }

        public Enumeration children() {
            return childsList.elements();
        }

        public TreeNode getParent() {
            return parent;
        }

        public TreeNode getChildAt(int childIndex) {
            return (TreeNode) childsList.get(childIndex);
        }

        public int getIndex(TreeNode node) {
            return childsList.indexOf(node);
        }

        public ExplorerTreeNode getChildByLocalPath(String path) {
            return (ExplorerTreeNode) childsMap.get(path);
        }

        public boolean hasChildren() {
            return childsList.size() > 0;
        }

        public TreePath getTreePath() {
            if (treePath == null) {
                int i = 0;
                for (ExplorerTreeNode node = this; node != null; node = node.parent) {
                    i++;
                }
                ExplorerTreeNode nodePath[] = new ExplorerTreeNode[i];
                for (ExplorerTreeNode node = this; node != null; node = node.parent) {
                    i--;
                    nodePath[i] = node;
                }
                treePath = new TreePath(nodePath);
            }
            return treePath;
        }
    }
}