/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/


import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.Shape;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;

/**
 * A triangular graphical figure.
 */


public class Diamond
	extends Shape
	implements INode
{

	int x_pos, y_pos, height, width;
	
	// Marker for traversal, starts false
	private boolean traversed = false;
	
	// questionmark label that sits at center
	final Label questionMark = new Label("?");
	
	/** The points of the diamond. */
	protected PointList diamond = new PointList(4);
	
	// Background color after node is traversed
	private Color traversedColor = new Color(null,50,255,50);

	public Diamond(){
		addQuestion();
		// This is specific to the "?" decision figure.  This class shiould be generalized and the DecisionPoint class
		// should subclass it.
		setSize(40,40);
	
		// Let it be Yellow
		setBackgroundColor(new Color(null,255,255,225));
	}
	
	//	 Return traversal status
	public boolean isTraversed(){
		return traversed;
	}

	//	 Set the node's traversal status (and thus color)
	public void setTraversed(boolean stat){
		setBackgroundColor(traversedColor);
		traversed = stat;
	}
	
	public void setSize(int x, int y){
		// Get the current bounds, so we know our x and y positions
		Rectangle r = getBounds();
		// Replace the width and height with our arguements
		r.height = y;
		r.width = x;
		setBounds(r);
	}
	
	/**
	 * @see Shape#fillShape(Graphics)
	 */
	protected void fillShape(Graphics g) {
		g.fillPolygon(diamond);
	}

	/**
	 * @see Shape#outlineShape(Graphics)
	 */
	protected void outlineShape(Graphics g) {
		g.drawPolygon(diamond);
	}

	/**
	 * @see Figure#primTranslate(int, int)
	 */
	public void primTranslate(int dx, int dy) {
		super.primTranslate(dx, dy);
		diamond.translate(dx, dy);
	}

	/**
	 * @see IFigure#validate()
	 */
	public void validate() {
		super.validate();
		Rectangle r = new Rectangle();
		r.setBounds(getBounds());
		r.crop(getInsets());


		int pixelWidth = 2;
		
		// 4 points of the diamond.
		Point top, right, bottom, left;
		top = new Point(r.x + r.width / 2, r.y);
		right = new Point((r.x + r.width - pixelWidth), r.y + r.height/2);
		bottom = new Point((r.x + r.width/2 - pixelWidth), r.y + r.height -1); // The -1 fixes the look
		left = new Point(r.x, r.y + r.height/2);
				
		diamond.removeAllPoints();
		diamond.addPoint(top);
		diamond.addPoint(right);
		diamond.addPoint(bottom);
		diamond.addPoint(left);
	}

	public void addQuestion(){
		Font titleFont = new Font(null, "Arial", 24, SWT.NORMAL);
		questionMark.setFont(titleFont);
		
		XYLayout layout = new XYLayout();
	    setLayoutManager(layout);
	    
	    // This is a hack to center the "?" Label in the center of the diamond
	    layout.setConstraint(questionMark, new Rectangle(10,11,20,20));
		this.add(questionMark);		
		getLayoutManager().layout(questionMark);
	}
}
