// PositionedNode.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.*;


/**
 * A tree node that has a scalar position for the purposes of drawing the tree.
 *
 * @author Alexei Drummond
 * @version $Id: PositionedNode.java,v 1.2 2000/11/02 03:16:13 matt Exp $
 */
public class PositionedNode extends SimpleNode {

	public double x; //Please excuse this - it will be returned to its non public state eventually - MG

	public PositionedNode(Node node) {
	
		init(node);
		if (!node.isLeaf()) {
			for (int i = 0; i < node.getChildCount(); i++) {
				addChild(new PositionedNode(node.getChild(i)));
			} 
		}
	}
	
	public void calculatePositions() {

		double[] currentXPos = {0.0};
		calculateXPositions(currentXPos);
	}

	private double calculateXPositions(double[] currentXPos) {

		if (!isLeaf()) {
			// find average x position
			x = ((PositionedNode)getChild(0)).calculateXPositions(currentXPos);
			for (int i = 1; i < getChildCount(); i++) {
				x += ((PositionedNode)getChild(i)).calculateXPositions(currentXPos);
			}
			x /= getChildCount();
		} else {
			x = currentXPos[0];
			currentXPos[0] += 1.0;
		}

		return x;
	}
}
