package jp.co.cybec.laxide.editParts;

import java.beans.PropertyChangeEvent;

import jp.co.cybec.laxide.editpolicies.LetsAjaxComponentEditPolicy;
import jp.co.cybec.laxide.editpolicies.LetsAjaxDirectEditPolicy;
import jp.co.cybec.laxide.model.base.LetsAjaxLinkButtonModel;

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

/**
 * リンクを描画するクラス ファイル名：LetsAjaxLinkButtonEditPart.java
 * 
 * @author nakamura 作成時刻：13:42:44
 * 
 */
public class LetsAjaxLinkButtonEditPart extends EditPartWithListener {

	private LetsAjaxDirectEditManager directManager = null;

	// ボタンは枠線を表示しているため、テキスト幅を基準に部品の幅を決定すると
	// 表示文字が途切れてしまう。そのためこの値で調整をおこなう
	private static final int ALIGNMENT = 33;

	/**
	 * 属性が変更された時に呼ばれるメソッド
	 * @see jp.co.cybec.laxide.editParts.EditPartWithListener#propertyChange(java.beans.PropertyChangeEvent)
	 */
	public void propertyChange(PropertyChangeEvent evt) {
		LetsAjaxLinkButtonModel model = (LetsAjaxLinkButtonModel) getModel();
		Label label = (Label) getFigure();

		if (evt.getPropertyName().equals(LetsAjaxLinkButtonModel.P_CONSTRAINT)) {
			int textWidth = label.getTextBounds().width + ALIGNMENT;
			label.getSize().width = textWidth;

			model.getConstraint().width = textWidth;
			model.getConstraint().height = 20;
			model.setComponentWidth(String.valueOf(textWidth));
			model.setComponentHeight("20");

			refreshVisuals();
		} else if (evt.getPropertyName().equals(LetsAjaxLinkButtonModel.P_TEXT)) {
			// モデルの変更をビューに表示する
			String newValue = (String) evt.getNewValue();
			label.setText(newValue);

			int newTextWidth = label.getTextBounds().width + ALIGNMENT;
			label.getSize().width = newTextWidth;

			model.getConstraint().width = newTextWidth;
			model.setComponentWidth(String.valueOf(newTextWidth));
		}
	}

	/**
	 * リンクの生成
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		LetsAjaxLinkButtonModel model = (LetsAjaxLinkButtonModel) getModel();

		Label label = new Label();
		label.setText(model.getText());
		// 背景色を不透明に
		label.setOpaque(false);
		label.setForegroundColor(ColorConstants.blue);
		label.setSize(60, 20);
		label.setBorder(new LineBorder());
		label.setBackgroundColor(ColorConstants.orange);

		return label;
	}

	protected void createEditPolicies() {

		installEditPolicy(EditPolicy.COMPONENT_ROLE,
				new LetsAjaxComponentEditPolicy());
		installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE,
				new LetsAjaxDirectEditPolicy());
	}

	protected void refreshVisuals() {
		Rectangle constraint = ((LetsAjaxLinkButtonModel) getModel())
				.getConstraint();
		((GraphicalEditPart) getParent()).setLayoutConstraint(this,
				getFigure(), constraint);
	}

	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(); // セル・エディタの表示
	}

}
