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

class SessionSizeView extends JComponent
{
	SessionSizeView()
	{
		filesNum = 0;
		size = 0;
		setDoubleBuffered(true);
		setBackground(Color.white);
		setFont(new Font("Courier", 0, 12));
		setToolTipText(CDcreator.i18n("TTsessionSize"));
	}
	public void add(File file)
	{
		childsNum++;
		paint(getGraphics());
		Thread counter = new SizeCounter(this, file, 1);
		counter.start();
	}
	synchronized public void childTerminated()
	{
		childsNum--;
		paint(getGraphics());
	}
	synchronized public void inc(long n)
	{
		filesNum++;
		if (n % 1024 == 0)
			n /= 1024;
		else
			n = n / 1024 + 1;
		size += n;
//		paint(getGraphics());
		repaint(new Rectangle(0, 0, getWidth(), getHeight()));
/*		Graphics g = getGraphics();
		g.clearRect(50, 0, 100, 10);
		g.drawString(String.valueOf(filesNum), 50, 10);
		g.drawString(String.valueOf(size), 100, 10);
*/
	}
	synchronized public void dec(long n)
	{
		filesNum--;
		if (n % 1024 == 0)
			n /= 1024;
		else
			n = n / 1024 + 1;
		size -= n;
//		paint(getGraphics());
		repaint(new Rectangle(0, 0, getWidth(), getHeight()));
	}
	public int getFilesNum()
	{
		return filesNum;
	}
	public int getKBytesNum()
	{
		return size;
	}
	public void paint(Graphics g)
	{
		g.clearRect(0, 0, getWidth(), getHeight());
		if (childsNum > 0)
			g.setColor(Color.red);
		else
			g.setColor(Color.gray);
		g.fillArc(1, 1, getHeight()-4, getHeight()-4, 0, 360);
		g.setColor(Color.black);
		g.drawString(
			String.valueOf(size / 1024)
			+ " MByte in "
			+ String.valueOf(filesNum)
			+ " " + CDcreator.i18n("files"),
			getHeight() + 10, 10);
	}
	public void remove(DirTreeNode file)
	{
		childsNum++;
		paint(getGraphics());
		Thread counter = new NodeSizeCounter(this, file, -1);
		counter.start();
	}

	private int filesNum;
	private int size;
	private int childsNum = 0;
}

class SizeCounter extends Thread
{
	SizeCounter(SessionSizeView aView, File aFile, int aFactor)
	{
		view = aView;
		file = aFile;
		factor = aFactor;
	}
	public void countDir(File file)
	{
		String files[] = file.list();
		int filesNum;
		if (files == null)
			filesNum = 0;
		else
			filesNum = files.length;

		for (int i = 0; i < filesNum; i++)
		{
			File file2 = new SecFile(file, files[i]);
			if (file2.isDirectory())
				countDir(file2);
			else
				countFile(file2);
		}
	}
	void countFile(File file)
	{
		if (!file.canRead())
			return;
//		System.out.println(file);
		if (factor > 0)
			view.inc(file.length());
		else
			view.dec(file.length());
	}
	public void run()
	{
		if (file.isDirectory())
			countDir(file);
		else
			countFile(file);
		view.childTerminated();
	}

	private SessionSizeView view;
	private File file;
	private int factor;
}

class NodeSizeCounter extends Thread
{
	NodeSizeCounter(SessionSizeView aView, DirTreeNode aNode,
		int aFactor)
	{
		view = aView;
		node = aNode;
		factor = aFactor;
	}
	void countDir(DirTreeNode node)
	{
		if (!node.isExpanded())
		{
			SizeCounter counter = new SizeCounter(
				view, node.getFile(), factor);
			counter.countDir(node.getFile());
			return;
		}

		for (int i = 0; i < node.getChildCount(); i++)
		{
			DirTreeNode node2
				= (DirTreeNode)node.getChildAt(i);
			if (!node2.isLeaf())
				countDir(node2);
			else
				countFile(node2);
		}
	}
	void countFile(DirTreeNode node)
	{
		if (!node.getFile().canRead())
			return;
//		System.out.println(node.getFile());
		if (factor > 0)
			view.inc(node.getFile().length());
		else
			view.dec(node.getFile().length());
	}
	public void run()
	{
		if (!node.isLeaf())
			countDir(node);
		else
			countFile(node);
		view.childTerminated();
	}

	private SessionSizeView view;
	private DirTreeNode node;
	private int factor;
}

