/**
 * 
 */
package nc.ui.hbbb.hbflowgraph.graph.handler;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

import nc.ui.hbbb.hbflowgraph.graph.HbFlowGraphConsts;

import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.swing.handler.mxVertexHandler;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxRectangle;
import com.mxgraph.view.mxCellState;

/**
 * @author fengzhy
 * @created at 2012-12-5,下午6:08:38
 *
 */
public class HbFlowGraphVertexHandler extends mxVertexHandler{

	/**
	 * @create by fengzhy at 2012-12-5,下午6:09:09
	 *
	 * @param graphComponent
	 * @param state
	 */
	public HbFlowGraphVertexHandler(mxGraphComponent graphComponent,
			mxCellState state) {
		super(graphComponent, state);
	}
	
	@Override
	protected Rectangle[] createHandles()
	{
		Rectangle[] h = null;

		if (graphComponent.getGraph().isCellResizable(getState().getCell()))
		{
			Rectangle bounds = getState().getRectangle();
			
			int width = 10;
			int height = 6;
			
			int topX = bounds.x + bounds.width / 2 - width / 2;
			int topY = bounds.y - height + 1;
			
			int bottomX = topX;
			int bottomY = bounds.y + bounds.height;
			
			int leftX = bounds.x - height + 1;
			int leftY = bounds.y + bounds.height / 2 - width / 2;
			
			int rightX = bounds.x + bounds.width;
			int rightY = leftY;
			
			h = new Rectangle[5];
			h[0] = new Rectangle(topX, topY, width, height);
			h[1] = new Rectangle(bottomX, bottomY, width, height);
			h[2] = new Rectangle(leftX, leftY, height, width);
			h[3] = new Rectangle(rightX, rightY, height, width);
		}
		else
		{
			h = new Rectangle[1];
		}

		int s = mxConstants.LABEL_HANDLE_SIZE;
		mxRectangle bounds = state.getLabelBounds();
		h[h.length - 1] = new Rectangle((int) (bounds.getX()
				+ bounds.getWidth() / 2 - s), (int) (bounds.getY()
				+ bounds.getHeight() / 2 - s), 2 * s, 2 * s);

		return h;
	}
	
	@Override
	protected Color getHandleFillColor(int index)
	{
		if (isLabel(index))
		{
			// ncm  xianymf  NCM_UFOC_2005_修改maven引用jar包引发的合并流程定义问题(适配云管家)  20220120
			return Color.yellow;
		}

		return Color.decode(HbFlowGraphConsts.HANDLE_FILLCOLOR);
	}
	
	@Override
	protected Color getHandleBorderColor(int index)
	{
		return Color.decode(HbFlowGraphConsts.HANDLE_FILLCOLOR);
	}
	
//	/**
//	 * 基类的这个方法是配合选中标志点，变换不同的鼠标形状（原选中标志点是可以拖拽改变图元大小的）
//	 * 这里不再需要拖拽改变大小了
//	 */
//	@Override
//	protected Cursor getCursor(MouseEvent e, int index)
//	{
//		return null;
//	}
//	
//	/**
//	 * 基类此方法用于拖动选中标志点改变图元大小，我们这里不需要拖拽改变大小了
//	 */
//	@Override
//	protected void resizeCell(MouseEvent e) {
//		return;
//	}
	
	/**
	 * 这样设置了就不能拖拽改变图元大小了。
	 */
	@Override
	protected boolean isHandleEnabled(int index)
	{
		return false;
	}
	
	/**
	 * 这个设置成和mxCellHandler相同的，因为mxVertexHandler会去画选中的虚线边框
	 */
	@Override
	public void paint(Graphics g)
	{
		if (handles != null && isHandlesVisible())
		{
			for (int i = 0; i < handles.length; i++)
			{
				if (isHandleVisible(i)
						&& g.hitClip(handles[i].x, handles[i].y,
								handles[i].width, handles[i].height))
				{
					g.setColor(getHandleFillColor(i));
					g.fillRect(handles[i].x, handles[i].y, handles[i].width,
							handles[i].height);

					g.setColor(getHandleBorderColor(i));
					g.drawRect(handles[i].x, handles[i].y,
							handles[i].width - 1, handles[i].height - 1);
				}
			}
		}
	}
	
}
