// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) fieldsfirst 
// Source File Name:   SwingLogTreeModel.java

package lt.monarch.debug;

import java.awt.*;
import java.io.*;
import java.text.DateFormat;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.*;
import javax.swing.tree.*;
import lt.monarch.swing.JTextLabel;
import lt.monarch.swing.tree.StaticTreeNode;
import lt.monarch.swing.tree.StringTreeNode;

class SwingLogTreeModel extends DefaultTreeModel
{
	static class LogNode
		implements TreeNode
	{

		Vector log;

		public void add(ErrorLogEngine.LogPacket logpacket)
		{
			log.addElement(new LogEntryNode(this, logpacket));
		}

		public void clear()
		{
			log.removeAllElements();
		}

		public TreeNode getChildAt(int i)
		{
			return (TreeNode)(LogEntryNode)log.elementAt(i);
		}

		public String toString()
		{
			return "Log";
		}

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

		public TreeNode getParent()
		{
			return null;
		}

		public int getIndex(TreeNode treenode)
		{
			return log.indexOf(treenode);
		}

		public boolean getAllowsChildren()
		{
			return true;
		}

		public boolean isLeaf()
		{
			return false;
		}

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

		LogNode()
		{
			log = new Vector();
		}
	}

	static class LogEntryNode extends StaticTreeNode
	{

		ErrorLogEngine.LogPacket packet;

		public String toString()
		{
			return packet.debugText();
		}

		protected Vector loadSubtree()
		{
			Vector vector = new Vector();
			if(packet.exception() != null)
			{
				try
				{
					CharArrayWriter chararraywriter = new CharArrayWriter();
					packet.exception().printStackTrace(new PrintWriter(chararraywriter));
					chararraywriter.close();
					BufferedReader bufferedreader = new BufferedReader(new CharArrayReader(chararraywriter.toCharArray()));
					for(String s = bufferedreader.readLine(); (s = bufferedreader.readLine()) != null;)
						if(s.length() != 0 && s.charAt(0) == '\t')
						{
							s = s.trim();
							if(s.length() != 0)
							{
								s = s.substring(3);
								vector.addElement(new StackEntryNode(this, s));
							}
						}

				}
				catch(IOException ioexception) { }
			}
			return vector;
		}

		public LogEntryNode(LogNode lognode, ErrorLogEngine.LogPacket logpacket)
		{
			super(lognode);
			packet = logpacket;
		}
	}

	static class StackEntryNode extends StringTreeNode
	{

		public StackEntryNode(LogEntryNode logentrynode, String s)
		{
			super(logentrynode, s);
		}
	}

	public static class CellRenderer extends JPanel
		implements TreeCellRenderer
	{

		ImageIcon logEntryIcon;
		public JLabel icon;
		public JTextLabel text;
		public DefaultTreeCellRenderer defaultRenderer;

		public Component getTreeCellRendererComponent(JTree jtree, Object obj, boolean flag, boolean flag1, boolean flag2, int i, boolean flag3)
		{
			if(obj instanceof StackEntryNode)
			{
				defaultRenderer.getTreeCellRendererComponent(jtree, obj, flag, flag1, flag2, i, false);
				defaultRenderer.setIcon(null);
				return (Component)defaultRenderer;
			}
			if(obj instanceof LogEntryNode)
			{
				if(flag)
				{
					setForeground(defaultRenderer.getTextSelectionColor());
					setBackground(defaultRenderer.getBackgroundSelectionColor());
				} else
				{
					setForeground(defaultRenderer.getTextNonSelectionColor());
					setBackground(defaultRenderer.getBackgroundNonSelectionColor());
				}
				String s;
				for(s = obj.toString().trim(); s.endsWith("\n") || s.endsWith("\r"); s = s.substring(1, s.length() - 1));
				text.setText(s);
				setEnabled(jtree.isEnabled());
				revalidate();
				return (Component)this;
			} else
			{
				return defaultRenderer.getTreeCellRendererComponent(jtree, obj, flag, flag1, flag2, i, false);
			}
		}

		public CellRenderer()
		{
			super(new BorderLayout());
			logEntryIcon = new ImageIcon(getClass().getResource("LogEntry.gif"));
			icon = new JLabel();
			text = new JTextLabel();
			defaultRenderer = new DefaultTreeCellRenderer();
			icon.setIcon(logEntryIcon);
			((Container)this).add("West", icon);
			((Container)this).add("Center", text);
		}
	}


	static DateFormat dateFormat = DateFormat.getDateTimeInstance(3, 2);

	public void clear()
	{
		LogNode lognode = (LogNode)getRoot();
		lognode.clear();
		reload();
	}

	public void add(ErrorLogEngine.LogPacket logpacket)
	{
		LogNode lognode = (LogNode)getRoot();
		lognode.add(logpacket);
		int ai[] = new int[1];
		ai[0] = lognode.getChildCount() - 1;
		nodesWereInserted(lognode, ai);
	}

	SwingLogTreeModel()
	{
		super(new LogNode());
	}

}
