2010-09-19 01:29:21 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
/***************************************************************************
|
|
|
|
|
* (C) Copyright 2003-2010 - Stendhal *
|
|
|
|
|
***************************************************************************
|
|
|
|
|
***************************************************************************
|
|
|
|
|
* *
|
|
|
|
|
* 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. *
|
|
|
|
|
* *
|
|
|
|
|
***************************************************************************/
|
2007-05-08 18:52:04 +00:00
|
|
|
package data.sprites.monsters;
|
2007-08-15 21:16:46 +00:00
|
|
|
|
2007-05-08 10:40:49 +00:00
|
|
|
/**
|
2007-08-15 21:16:46 +00:00
|
|
|
*
|
2007-05-08 10:40:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileNotFoundException;
|
2012-04-29 13:47:46 +00:00
|
|
|
import java.util.Arrays;
|
2007-05-08 10:40:49 +00:00
|
|
|
|
|
|
|
|
import javax.swing.JTree;
|
|
|
|
|
import javax.swing.event.TreeExpansionEvent;
|
|
|
|
|
import javax.swing.event.TreeExpansionListener;
|
|
|
|
|
import javax.swing.tree.DefaultMutableTreeNode;
|
|
|
|
|
import javax.swing.tree.DefaultTreeModel;
|
|
|
|
|
import javax.swing.tree.TreeModel;
|
|
|
|
|
import javax.swing.tree.TreePath;
|
|
|
|
|
|
|
|
|
|
public class FileTree extends JTree {
|
2007-08-15 21:16:46 +00:00
|
|
|
/**
|
2017-05-28 01:03:01 +02:00
|
|
|
*
|
2007-05-11 20:24:57 +00:00
|
|
|
*/
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
2008-07-12 14:44:47 +00:00
|
|
|
public FileTree(final String path) throws FileNotFoundException {
|
2008-01-19 14:43:19 +00:00
|
|
|
// Create the JTree itself
|
|
|
|
|
super((TreeModel) null);
|
2007-08-15 21:16:46 +00:00
|
|
|
|
|
|
|
|
// Use horizontal and vertical lines
|
|
|
|
|
putClientProperty("JTree.lineStyle", "Angled");
|
|
|
|
|
|
|
|
|
|
// Create the first node
|
2008-07-12 14:44:47 +00:00
|
|
|
final FileTreeNode rootNode = new FileTreeNode(null, path);
|
2007-08-15 21:16:46 +00:00
|
|
|
|
|
|
|
|
// Populate the root node with its subdirectories
|
|
|
|
|
rootNode.populateDirectories(true);
|
|
|
|
|
setModel(new DefaultTreeModel(rootNode));
|
|
|
|
|
|
|
|
|
|
// Listen for Tree Selection Events
|
|
|
|
|
addTreeExpansionListener(new TreeExpansionHandler());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns the full pathname for a path, or null
|
|
|
|
|
// if not a known path
|
2008-07-12 14:44:47 +00:00
|
|
|
public String getPathName(final TreePath path) {
|
|
|
|
|
final Object o = path.getLastPathComponent();
|
2007-08-15 21:16:46 +00:00
|
|
|
if (o instanceof FileTreeNode) {
|
|
|
|
|
return ((FileTreeNode) o).file.getAbsolutePath();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns the File for a path, or null if not a known path
|
2008-07-12 14:44:47 +00:00
|
|
|
public File getFile(final TreePath path) {
|
|
|
|
|
final Object o = path.getLastPathComponent();
|
2007-08-15 21:16:46 +00:00
|
|
|
if (o instanceof FileTreeNode) {
|
|
|
|
|
return ((FileTreeNode) o).file;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Inner class that represents a node in this
|
|
|
|
|
// file system tree
|
|
|
|
|
protected class FileTreeNode extends DefaultMutableTreeNode {
|
2017-05-28 01:03:01 +02:00
|
|
|
|
2009-02-26 21:19:36 +00:00
|
|
|
private static final long serialVersionUID = 3223106240309250204L;
|
2017-05-28 01:03:01 +02:00
|
|
|
|
2007-08-15 21:16:46 +00:00
|
|
|
/**
|
2009-02-26 21:19:36 +00:00
|
|
|
* File object for this node.
|
2007-08-15 21:16:46 +00:00
|
|
|
*/
|
2009-02-26 21:19:36 +00:00
|
|
|
protected File file;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Name of this node.
|
|
|
|
|
*/
|
|
|
|
|
protected String name;
|
|
|
|
|
/**
|
|
|
|
|
* true if we have been populated.
|
|
|
|
|
*/
|
|
|
|
|
protected boolean populated;
|
|
|
|
|
/**
|
|
|
|
|
* true if we are in interim state.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
protected boolean interim;
|
|
|
|
|
/**
|
|
|
|
|
* true if this is a directory.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
protected boolean isDir;
|
2017-05-28 01:03:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-08-15 21:16:46 +00:00
|
|
|
|
2008-07-12 14:44:47 +00:00
|
|
|
public FileTreeNode(final File parent, final String name) throws FileNotFoundException {
|
2007-08-15 21:16:46 +00:00
|
|
|
this.name = name;
|
|
|
|
|
|
|
|
|
|
// See if this node exists and whether it
|
|
|
|
|
// is a directory
|
|
|
|
|
file = new File(parent, name);
|
|
|
|
|
if (!file.exists()) {
|
2008-01-19 14:43:19 +00:00
|
|
|
throw new FileNotFoundException("File " + name + " does not exist");
|
2007-08-15 21:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isDir = file.isDirectory();
|
|
|
|
|
|
|
|
|
|
// Hold the File as the user object.
|
|
|
|
|
setUserObject(file);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Override isLeaf to check whether this is a directory
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isLeaf() {
|
|
|
|
|
return !isDir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Override getAllowsChildren to check whether
|
|
|
|
|
// this is a directory
|
|
|
|
|
@Override
|
|
|
|
|
public boolean getAllowsChildren() {
|
|
|
|
|
return isDir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// For display purposes, we return our own name public String toString()
|
|
|
|
|
// { return name; }
|
|
|
|
|
// If we are a directory, scan our contents and populate
|
|
|
|
|
// with children. In addition, populate those children
|
|
|
|
|
// if the "descend" flag is true. We only descend once,
|
|
|
|
|
// to avoid recursing the whole subtree.
|
|
|
|
|
// Returns true if some nodes were added
|
2008-07-12 14:44:47 +00:00
|
|
|
boolean populateDirectories(final boolean descend) {
|
2007-08-15 21:16:46 +00:00
|
|
|
boolean addedNodes = false;
|
|
|
|
|
// Do this only once
|
|
|
|
|
if (!populated) {
|
|
|
|
|
if (interim) {
|
|
|
|
|
// We have had a quick look here before:
|
|
|
|
|
// remove the dummy node that we added last time
|
|
|
|
|
removeAllChildren();
|
|
|
|
|
interim = false;
|
|
|
|
|
}
|
2008-01-19 14:43:19 +00:00
|
|
|
// Get list of contents
|
2008-07-12 14:44:47 +00:00
|
|
|
final String[] names = file.list();
|
2012-04-29 13:47:46 +00:00
|
|
|
Arrays.sort(names);
|
2007-08-15 21:16:46 +00:00
|
|
|
|
|
|
|
|
// Process the directories
|
|
|
|
|
for (int i = 0; i < names.length; i++) {
|
2008-07-12 14:44:47 +00:00
|
|
|
final String nameTemp = names[i];
|
2007-08-15 21:16:46 +00:00
|
|
|
try {
|
|
|
|
|
// if (d.isDirectory()) {
|
2008-07-12 14:44:47 +00:00
|
|
|
final FileTreeNode node = new FileTreeNode(file, nameTemp);
|
2007-08-15 21:16:46 +00:00
|
|
|
this.add(node);
|
|
|
|
|
if (descend) {
|
|
|
|
|
node.populateDirectories(false);
|
|
|
|
|
}
|
|
|
|
|
addedNodes = true;
|
|
|
|
|
if (!descend) {
|
|
|
|
|
// Only add one node if not descending
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
// }
|
|
|
|
|
// else{
|
|
|
|
|
|
|
|
|
|
// }
|
2008-07-12 14:44:47 +00:00
|
|
|
} catch (final Throwable t) {
|
2007-08-15 21:16:46 +00:00
|
|
|
// Ignore phantoms or access problems
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we were scanning to get all subdirectories,
|
|
|
|
|
// or if we found no subdirectories, there is no
|
|
|
|
|
// reason to look at this directory again, so
|
|
|
|
|
// set populated to true. Otherwise, we set interim
|
|
|
|
|
// so that we look again in the future if we need to
|
2008-01-19 14:43:19 +00:00
|
|
|
if (descend || !addedNodes) {
|
2007-08-15 21:16:46 +00:00
|
|
|
populated = true;
|
|
|
|
|
} else {
|
|
|
|
|
// Just set interim state
|
|
|
|
|
interim = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return addedNodes;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-28 01:03:01 +02:00
|
|
|
|
2007-08-15 21:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Inner class that handles Tree Expansion Events
|
2008-01-22 09:48:57 +00:00
|
|
|
protected static class TreeExpansionHandler implements TreeExpansionListener {
|
2013-04-25 21:50:35 +00:00
|
|
|
@Override
|
2008-07-12 14:44:47 +00:00
|
|
|
public void treeExpanded(final TreeExpansionEvent evt) {
|
2008-01-19 14:43:19 +00:00
|
|
|
// The expanded path
|
2008-07-12 14:44:47 +00:00
|
|
|
final TreePath path = evt.getPath();
|
2008-01-22 22:18:07 +00:00
|
|
|
// The tree
|
2008-07-12 14:44:47 +00:00
|
|
|
final JTree tree = (JTree) evt.getSource();
|
2007-08-15 21:16:46 +00:00
|
|
|
|
|
|
|
|
// Get the last component of the path and
|
|
|
|
|
// arrange to have it fully populated.
|
2008-07-12 14:44:47 +00:00
|
|
|
final FileTreeNode node = (FileTreeNode) path.getLastPathComponent();
|
2007-08-15 21:16:46 +00:00
|
|
|
if (node.populateDirectories(true)) {
|
|
|
|
|
((DefaultTreeModel) tree.getModel()).nodeStructureChanged(node);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-25 21:50:35 +00:00
|
|
|
@Override
|
2008-07-12 14:44:47 +00:00
|
|
|
public void treeCollapsed(final TreeExpansionEvent evt) {
|
2007-08-15 21:16:46 +00:00
|
|
|
// Nothing to do
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|