package jp.co.cybec.laxide.editParts;

import java.beans.PropertyChangeEvent;
import java.util.List;

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

import org.eclipse.draw2d.BorderLayout;
import org.eclipse.draw2d.CompoundBorder;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.MarginBorder;
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;

/**
 * ラジオボタンを描画するクラス ファイル名：LetsAjaxRadioButtonEditPart.java
 * 
 * @author nakamura 作成時刻：13:45:09
 * 
 */
public class LetsAjaxRadioButtonEditPart extends EditPartWithListener {

	private LetsAjaxDirectEditManager directManager = null;

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

		if (evt.getPropertyName().equals(LetsAjaxRadioButtonModel.P_CONSTRAINT)) {
			LetsAjaxRadioButtonModel model = (LetsAjaxRadioButtonModel) getModel();
			Label label = (Label) getFigure();
			model.setComponentWidth(Integer.toString(label.getSize().width));
			model.setComponentHeight(Integer.toString(label.getSize().height));
			refreshVisuals();
		} else if (evt.getPropertyName().equals(
				LetsAjaxRadioButtonModel.P_VALUE)) {
			Label label = (Label) getFigure();
			List list = label.getChildren();
			Label textLabel = (Label) list.get(0);
			textLabel.setText((String) evt.getNewValue());
		}
	}

	/**
	 * ラジオボタンを生成
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		LetsAjaxRadioButtonModel model = (LetsAjaxRadioButtonModel) getModel();

		Label label = new Label();
		label.setBorder(new CompoundBorder(new LineBorder(),
				new MarginBorder(3)));
		label.setOpaque(true);
		label.setSize(100, 50);
		label.setLayoutManager(new BorderLayout());

		Label textLabel = new Label();
		textLabel.setText(model.getName());
		textLabel.setSize(40, 50);
		textLabel.setOpaque(true);
		label.add(textLabel, BorderLayout.LEFT);

		Label rightCheckBox = new Label();
		rightCheckBox.setBorder(new CompoundBorder(new LineBorder(),
				new MarginBorder(3)));
		rightCheckBox.setOpaque(true);
		rightCheckBox.setSize(40, 50);
		rightCheckBox.setLayoutManager(new BorderLayout());
		label.add(rightCheckBox, BorderLayout.RIGHT);
		return label;
	}

	protected void createEditPolicies() {
		// 
		installEditPolicy(EditPolicy.COMPONENT_ROLE,
				new LetsAjaxComponentEditPolicy());
		installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE,
				new LetsAjaxDirectEditPolicy());
	}

	protected void refreshVisuals() {
		//
		Rectangle constraint = ((LetsAjaxRadioButtonModel) 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();
	}

}
