/*
 * Copyright (c) 2006-2008 Maskat Project.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Maskat Project - initial API and implementation
 */
package org.maskat.ui.editors.layout.editparts;

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

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.XYLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.RootEditPart;
import org.eclipse.gef.editpolicies.SnapFeedbackPolicy;
import org.eclipse.swt.graphics.Font;

import org.maskat.core.layout.TreeNode;
import org.maskat.ui.editors.layout.editpolicies.DataBindingEditPolicy;
import org.maskat.ui.editors.layout.editpolicies.LabelDirectEditPolicy;
import org.maskat.ui.editors.layout.editpolicies.TreeNodeComponentEditPolicy;
import org.maskat.ui.editors.layout.editpolicies.TreeNodeXYLayoutEditPolicy;
import org.maskat.ui.editors.layout.figures.TreeViewFigure;

public class TreeNodeEditPart extends ComponentEditPart
	implements LabelDirectEditable {

	public static Font DEFAULT_FONT = new Font(null, "Arial", 8, 0);

	protected IFigure createFigure() {
		TreeNode def = (TreeNode) getModel();

		Label label = new TreeViewFigure(def.getSText());
		label.setBorder(new CompoundBorder(new LineBorder(), new MarginBorder(2)));
		label.setOpaque(false);
		label.setFont(DEFAULT_FONT);

		XYLayout layout = new XYLayout();
		label.setLayoutManager(layout);
		return label;
	}

	protected List getModelChildren() {
		return Collections.EMPTY_LIST;
	}

	protected void createEditPolicies() {
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new TreeNodeXYLayoutEditPolicy());
		installEditPolicy(EditPolicy.COMPONENT_ROLE, new TreeNodeComponentEditPolicy());
		installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new LabelDirectEditPolicy());
		installEditPolicy("Snap Feedback", new SnapFeedbackPolicy());
		installEditPolicy(DataBindingEditPolicy.ROLE, new DataBindingEditPolicy());
	}

	protected void refreshVisuals() {
		Dimension dim = figure.getPreferredSize();
		TreeNode def = (TreeNode) getModel();
		Rectangle constraint = new Rectangle(12 * def.getLayer(), 
				20 * def.getLeftIterationIndex(), dim.width, 20);
		((GraphicalEditPart) getParent()).setLayoutConstraint(this, getFigure(),
				constraint);
	}

	public void propertyChange(PropertyChangeEvent evt) {
		if ("sText".equals(evt.getPropertyName())) {
			((Label) getFigure()).setText((String) evt.getNewValue());
			getFigure().repaint();
//			refreshVisuals();
			super.propertyChange(evt);
		}
		if ("addChild".equals(evt.getPropertyName())
				|| "removeChild".equals(evt.getPropertyName())) {
			EditPart parent = this.getParent();
			while (!(parent instanceof TreeViewEditPart || parent instanceof RootEditPart)) {
				parent = parent.getParent();
			}
			if (parent instanceof TreeViewEditPart) {
				((TreeViewEditPart) parent).forceRefresh();
			}
		}
	}

	public void forceRefresh() {
		refresh();
		// すべての子ノードをリフレッシュ
		for (Iterator it = this.getChildren().iterator(); it.hasNext();) {
			((EditPart) it.next()).refresh();
		}
	}
	
	public String getLabel() {
		return ((TreeNode) getModel()).getSText();
	}

	public void setLabel(String text) {
		((TreeNode) getModel()).setSText(text);
	}
}
