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;
}
}
}