//----------------------------BEGIN LICENSE----------------------------
/* 
 * Willow : the Open Source WorkFlow Project
 * Distributable under GNU LGPL license by gun.org
 *
 * Copyright (C) 2004-2010 huihoo.org 
 * Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com> 
 * 
 * ====================================================================
 * Project Homepage : http://www.huihoo.org/god4j
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : god4j@lists.sourceforge.net 
 */
//----------------------------END  LICENSE-----------------------------
package org.huihoo.god4j.jgraph;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import org.jgraph.JGraph;
import org.jgraph.graph.CellView;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.VertexRenderer;
import org.jgraph.graph.VertexView;
public class BeginActivityRenderer extends VertexRenderer
{
	private BasicStroke BORDER_STROKE = new BasicStroke(2);
	private Color FILL_COLOR = new Color(0, 128, 255);
	public BeginActivityRenderer()
	{
		super();
	}
	/**
	 * Configure and return the renderer based on the passed in
	 * components. The value is typically set from messaging the
	 * graph with <code>convertValueToString</code>.
	 * We recommend you check the value's class and throw an
	 * illegal argument exception if it's not correct.
	 *
	 * @param   graph the graph that that defines the rendering context.
	 * @param   value the object that should be rendered.
	 * @param   selected whether the object is selected.
	 * @param   hasFocus whether the object has the focus.
	 * @param   isPreview whether we are drawing a preview.
	 * @return	the component used to render the value.
	 */
	public Component getRendererComponent(JGraph graph, CellView view, boolean sel, boolean focus, boolean preview)
	{
		this.graph = graph;
		isDoubleBuffered = graph.isDoubleBuffered();
		if (view instanceof VertexView)
		{
			this.view = (VertexView) view;
			setComponentOrientation(graph.getComponentOrientation());
			setText("S");
			this.graph = graph;
			this.hasFocus = focus;
			this.childrenSelected = graph.getSelectionModel().isChildrenSelected(view.getCell());
			this.selected = sel;
			this.preview = preview;
			if (this.view.isLeaf() || GraphConstants.isOpaque(view.getAllAttributes()))
			{
				installAttributes(view);
			}
			else
			{
				setText(null);
				setBorder(null);
				setOpaque(false);
				setGradientColor(null);
				setIcon(null);
			}
			return this;
		}
		return null;
	}
	public void paint(Graphics g)
	{
		int b = borderWidth;
		Graphics2D g2 = (Graphics2D) g;
		Dimension d = getSize();
		boolean tmp = selected;
		if (super.isOpaque())
		{
			g.setColor(FILL_COLOR);
			g.fillOval(b - 1, b - 1, d.width - b, d.height - b);
		}
		try
		{
			setBorder(null);
			setOpaque(false);
			selected = false;
			super.paint(g);
		}
		finally
		{
			selected = tmp;
		}
		if (bordercolor != null)
		{
			g.setColor(bordercolor);
			g2.setStroke(BORDER_STROKE);
			g.drawOval(b - 1, b - 1, d.width - b, d.height - b);
		}
		if (selected)
		{
			g2.setStroke(GraphConstants.SELECTION_STROKE);
			g.setColor(graph.getHighlightColor());
			g.drawOval(b - 1, b - 1, d.width - b, d.height - b);
		}
	}
}