// TreeComponent.java
//
// (c) 1999-2000 PAL Development Core Team
//
// This package may be distributed under the
// terms of the GNU General Public License (GPL)

package pal.gui;

import pal.tree.*;
import pal.misc.*;

import java.awt.*;


/**
 * An AWT Component for displaying a tree.  
 *
 * @author Alexei Drummond
 * @version $Id: TreeComponent.java,v 1.9 2001/01/09 07:22:16 alexi Exp $
 */
public class TreeComponent extends Component {

	public final static int NORMAL = 0;
	public final static int CIRCULAR = 1;

	public final static String[] MODE_NAMES = new String[2];

	// unfortunate but necessary to avoid Java 1.1 language features
	private void initModeNames()
	{
		MODE_NAMES[0] = "Normal";
		MODE_NAMES[1] = "Circular";
	}

	TreePainterCircular circlePainter_;
	TreePainter painter_;

	int mode_;
	boolean invertCiruclar_;

	// constructors

	public TreeComponent(Tree tree, boolean usingSymbols) {
		this(tree, (TimeOrderCharacterData)null, usingSymbols);
	}

	public TreeComponent(Tree tree, TimeOrderCharacterData tocd, boolean usingSymbols) {
		this(tree);
		
		if (tocd != null) {
			painter_.setTimeOrderCharacterData(tocd);
		}
		painter_.setUsingSymbols(usingSymbols);
	}

	public TreeComponent(Tree tree) {
		this(tree, "", false);
	}

	public TreeComponent(Tree tree, String title) {
		this(tree, title, true);
	}

	private TreeComponent(Tree tree, String title, boolean showTitle) {
	
		initModeNames();
	
		painter_ = new TreePainterNormal(tree,title,showTitle);
		circlePainter_ = new TreePainterCircular(tree,title,showTitle);
		this.mode_ = NORMAL;
		setSize(getPreferredSize());
	}

	public void setTree(Tree tree) {
		painter_.setTree(tree);
		circlePainter_.setTree(tree);
	}

	public void setTitle(String title) {
		painter_.setTitle(title);
		circlePainter_.setTitle(title);
	}

	public Dimension getPreferredSize() {
		return painter_.getPreferredSize();
	}
	public void setInvertCircular(boolean invert) {
		this.invertCiruclar_ = invert;
	}


	public Dimension getMinimumSize() {
		return getPreferredSize();
	}

	public String getTitle() {
			return painter_.getTitle();
	}

	/** 
	 * Sets the mode of display for this component.
	 * @param the mode to switch to. Valid arguments are NORMAL (for normal tree
	 * display), and CIRCULAR (for a circular view of the trees)
	 */
	public void setMode(int mode) {
		this.mode_ = mode;
	}

	public void paint(Graphics g) {
		switch (mode_) {
			case NORMAL : {
				painter_.paint(g,getSize().width,getSize().height);
				break;
			}
			case CIRCULAR : {
				circlePainter_.paint(g,getSize().width,getSize().height,invertCiruclar_);
				break;
			}
		}
	}
}
