
import java.io.*;
import javax.swing.tree.*;

class DirTreeNode implements MutableTreeNode
{
	DirTreeNode(String aName)
	{
		parent = null;
		name = aName;
		file = null;
		childs = new TreeNode[10];
		childsNum = 0;
		expanded = true;
		isDir = true;
	}

	DirTreeNode(File aFile)
	{
		parent = null;
		name = aFile.getName();
		file = aFile;
		expanded = false;
		isDir = file.isDirectory();

//		System.out.println("new DirTreeNode(" + aFile + ")");
	}

	public java.util.Enumeration children()
	{
		return null;
	}

	public boolean getAllowsChildren()
	{
		return !isLeaf();
	}

	public TreeNode getChildAt(int childIndex)
	{
		expand();
		return childs[childIndex];
	}

	public int getChildCount()
	{
		expand();
		return childsNum;
	}

	public File getFile()
	{
		return file;
	}

	public int getIndex(TreeNode node)
	{
		expand();
		int i = 0;
		while (i < childsNum && childs[i] != node)
			i++;
		if (i < childsNum)
			return i;
		else
			return -1;
	}

	public TreeNode getParent()
	{
		return parent;
	}

	private void expand()
	{
		if (expanded)
			return;

//		System.out.println("expand(): " + name);
		String files[] = file.list();
		int filesNum;
		if (files == null)
			filesNum = 0;
		else
			filesNum = files.length;

		childs = new TreeNode[filesNum + 100];
		childsNum = 0;
		expanded = true;

		for (int i = 0; i < filesNum; i++)
		{
			SecFile f = new SecFile(file, files[i]);
			if (CDcreator.sessionType != CDcreator.audioSession
				|| f.isDirectory()
				|| f.getName().toLowerCase()
					.endsWith(".wav")
				|| (CDcreator.mp3Support
					&&  f.getName().toLowerCase()
						.endsWith(".mp3")))
				insert(new DirTreeNode(f));
		}
	}

	public void insert(MutableTreeNode child)
	{
		expand();
		int i;
		for (i = 0; i < childsNum
			&& childs[i].toString().compareTo(
				child.toString()) < 0; i++);
		insert(child, i);
	}

	public void insert(MutableTreeNode child, int index)
	{
		expand();
		if (childsNum == childs.length)
		{
			TreeNode[] old = childs;
			childs = new TreeNode[childsNum + 100];
			for (int i = 0; i < childsNum; i++)
				childs[i] = old[i];
		}

		for (int i = childsNum; i > index; i--)
			childs[i] = childs[i-1];
		childs[index] = child;
		childsNum++;
		child.setParent(this);
	}

	public boolean isExpanded()
	{
		return expanded;
	}

	public boolean isLeaf()
	{
//		return file != null && !file.isDirectory();
		return !isDir;
	}

	public void remove(int index)
	{
		((MutableTreeNode)childs[index]).setParent(null);
		for (int i = index; i < childsNum; i++)
			childs[i] = childs[i+1];
		childsNum--;
	}

	public void remove(MutableTreeNode node)
	{
		remove(getIndex(node));
	}

	public void removeFromParent()
	{
		((MutableTreeNode)getParent()).remove(this);
	}

	public void resetExpanded()
	{
		expanded = false;
	}

	public void setParent(MutableTreeNode newParent)
	{
		parent = newParent;
	}

	public void setUserObject(Object object)
	{
//		System.out.println(
//			"DirTreeNode::setUserObject: " + object);
		name = (String)object;
	}

	public String toString()
	{
		return name;
	}

	private TreeNode parent;
	private String name;
	private File file;
	private boolean expanded, isDir;
	private TreeNode childs[];
	private int childsNum;
}
