/*
 * 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.commands;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.Command;
import org.maskat.framework.screendef.DivSplitDef;
import org.maskat.framework.screendef.IComponentDef;
import org.maskat.framework.screendef.IContainerDef;

public class ChangeConstraintCommand extends Command {
	private IComponentDef nodeElement;

	private Rectangle newConstraint, oldConstraint;

	public ChangeConstraintCommand(IComponentDef node) {
		super(node.getName() + "の位置、サイズ変更");
		nodeElement = node;
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	public void execute() {
		// 制約の変更
		if (nodeElement instanceof DivSplitDef) {
			// ここではnewConstraint.heightあるいはwidthはDivSplitDefのheightとwidthである
			// parentのSplitterDefのheightとwidthではなく
			// だから、nodeElementの相対座標変化を計算して、SplitterDefの座標を計算する

			int deltaX = newConstraint.x - nodeElement.getLeft();
			int deltaY = newConstraint.y - nodeElement.getTop();
			int deltaWidth = newConstraint.width - nodeElement.getWidth();
			int deltaHeight = newConstraint.height - nodeElement.getHeight();

			IContainerDef parent = nodeElement.getParent();
			//
			this.oldConstraint = new Rectangle(parent.getLeft(), parent
					.getTop(), parent.getWidth(), parent.getHeight());
			parent.setConstraint(parent.getLeft() + deltaX, parent.getTop()
					+ deltaY, parent.getWidth() + deltaWidth, parent
					.getHeight()
					+ deltaHeight);
		} else {
			this.oldConstraint = new Rectangle(nodeElement.getLeft(),
					nodeElement.getTop(), nodeElement.getWidth(), nodeElement
							.getHeight());
			nodeElement.setConstraint(newConstraint.x, newConstraint.y,
					newConstraint.width, newConstraint.height);
		}
		// MaskatIDEUtil.dirtyMaskatEditorForLayoutDef(nodeElement.getLayoutDef());
		// MaskatPropertySheet sheet = MaskatIDEUtil.getMaskPropertySheet();
		// if (sheet != null)
		// sheet.refresh();
	}

	public void setConstraint(Rectangle rect) {
		newConstraint = rect;
	}

	public boolean canUndo() {
		return true;
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	public void undo() {
		// 制約を元にもどす

		if (nodeElement instanceof DivSplitDef) {
			nodeElement.getParent().setConstraint(oldConstraint.x,
					oldConstraint.y, oldConstraint.width, oldConstraint.height);
		} else {
			nodeElement.setConstraint(oldConstraint.x, oldConstraint.y,
					oldConstraint.width, oldConstraint.height);
		}
	}
}
