package jp.co.cybec.laxide.editParts;

import java.beans.PropertyChangeEvent;

import jp.co.cybec.laxide.layout.AbstractLayoutPoints;
import jp.co.cybec.laxide.layout.LetsAjax2ColumnLayoutPoints;
import jp.co.cybec.laxide.layout.LetsAjaxLayoutPointFactory;
import jp.co.cybec.laxide.model.base.LetsAjax2ColumnLayoutModel;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.jface.viewers.TextCellEditor;

/**
 * ２段組レイアウトのエディットパートクラス このクラスは２段組のレイアウトを描画します。 描画のみを行うため、描画後は移動や大きさの変更は行わないため、
 * プロパティの変更、エディットポリシーのインストール、リスナへの登録は行いません。
 * 
 * @author tokumaru
 */
public class LetsAjax2ColumnLayoutEditPart extends EditPartWithListener {

	private LetsAjaxDirectEditManager directManager = null;

	/*
	 * (non-Javadoc)
	 * 
	 * @see jp.co.cybec.laxide.editParts.EditPartWithListener#propertyChange(java.beans.PropertyChangeEvent)
	 */
	public void propertyChange(PropertyChangeEvent evt) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		LetsAjaxLayoutPointFactory factory = LetsAjaxLayoutPointFactory
				.getInstance();
		AbstractLayoutPoints points = factory
				.getLayoutPoints(LetsAjax2ColumnLayoutPoints.class);
		

		return points.getLayoutPolyLine();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractEditPart#refreshVisuals()
	 */
	protected void refreshVisuals() {
		Rectangle constraint = ((LetsAjax2ColumnLayoutModel) getModel())
				.getConstraint();

		((GraphicalEditPart) getParent()).setLayoutConstraint(this,
				getFigure(), constraint);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractEditPart#performRequest(org.eclipse.gef.Request)
	 */
	public void performRequest(Request req) {
		if (req.getType().equals(RequestConstants.REQ_DIRECT_EDIT)) {
			performDirectEdit();
			return;
		}
		super.performRequest(req);
	}

	/**
	 * 
	 */
	private void performDirectEdit() {
		if (directManager == null) {
			directManager = new LetsAjaxDirectEditManager(this,
					TextCellEditor.class, new LetsAjaxCellEditorLocator(
							getFigure()));
		}
		directManager.show();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jp.co.cybec.laxide.editParts.EditPartWithListener#activate()
	 */
	public void activate() {
		// イベントを受け付けないようにリスナへの登録は行わない
	}
}
