/*
 * Copyright 2006 Mask@ Project.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.maskat.ide.gef.editparts;

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

import org.eclipse.draw2d.CompoundBorder;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.maskat.framework.screendef.DivSplitDef;
import org.maskat.framework.screendef.IComponentDef;
import org.maskat.framework.screendef.IContainerDef;
import org.maskat.ide.gef.editpolicies.MaskatComponentEditPolicy;
import org.maskat.ide.gef.editpolicies.MyXYLayoutEditPolicy;
import org.maskat.ide.gef.figure.SplitPageFigure;

public class SplitPageEditPart extends AbstractGraphicalEditPart implements
		PropertyChangeListener {

	public void activate() {
		super.activate();
		IComponentDef model = (IComponentDef) getModel();
		model.addPropertyChangeListener(this);
	}

	public void deactivate() {
		IComponentDef model = (IComponentDef) getModel();
		model.removePropertyChangeListener(this);
		super.deactivate();
	}

	protected IFigure createFigure() {
		DivSplitDef divSplit = (DivSplitDef) getModel();
		// divSplitのインデックスを取得
		int idx = divSplit.getParent().getChildIdx(divSplit);

		SplitPageFigure splitPageFig = new SplitPageFigure(idx);
		XYLayout layout = new XYLayout();
		splitPageFig.setLayoutManager(layout);
		splitPageFig.setBorder(new CompoundBorder(new LineBorder(), new MarginBorder(0)));

		SplitterEditPart splitterEditPart = (SplitterEditPart) this.getParent();
		// 該当選択されたdivSplitはvisible、ではなければinvisible
		if (divSplit.getIdx() == splitterEditPart.getCurrentSelectedSplitIdx()) {
			splitPageFig.setVisible(true);
		} else {
			splitPageFig.setVisible(false);
		}

		return splitPageFig;
	}

	protected void createEditPolicies() {
		installEditPolicy(EditPolicy.COMPONENT_ROLE, new MaskatComponentEditPolicy());
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new MyXYLayoutEditPolicy());
	}

	protected List getModelChildren() {
		return ((IContainerDef) getModel()).getComponentChildren();
	}

	protected void refreshVisuals() {
		// 制約の取得
		IComponentDef comp = (IComponentDef) getModel();
		Rectangle constraint = new Rectangle(comp.getLeft(), comp.getTop(), comp
				.getWidth(), comp.getHeight());

		// Rectangleオブジェクトを制約としてビューに設定する
		// setLayoutConstraintメソッドは親EditPartから呼び出す
		((GraphicalEditPart) getParent()).setLayoutConstraint(this, getFigure(),
				constraint);
	}

	public void propertyChange(PropertyChangeEvent evt) {
		if ("addChild".equals(evt.getPropertyName())
				|| "removeChild".equals(evt.getPropertyName())) {
			refreshChildren();
		}
		refreshVisuals();
	}
}
