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

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gef.editpolicies.ComponentEditPolicy;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.gef.requests.GroupRequest;
import org.maskat.framework.screendef.DivSplitDef;
import org.maskat.framework.screendef.GridDef;
import org.maskat.framework.screendef.GridHeaderDef;
import org.maskat.framework.screendef.IComponentDef;
import org.maskat.framework.screendef.IContainerDef;
import org.maskat.framework.screendef.LayoutDef;
import org.maskat.ide.gef.commands.DelComponentCommand;
import org.maskat.ide.gef.commands.DelGridHeaderCommand;

public class MaskatComponentEditPolicy extends ComponentEditPolicy {
	protected Command createDeleteCommand(GroupRequest deleteRequest) {
		Object model = getHost().getModel();
		if (model instanceof GridHeaderDef) {
			GridDef grid = ((GridHeaderDef) model).getParent();
			return new DelGridHeaderCommand(grid, (GridHeaderDef) model);
		}
		if (model instanceof IComponentDef) {
			IComponentDef comp = (IComponentDef) model;
			if (comp instanceof LayoutDef) {
				return null;
			}
			if (comp instanceof DivSplitDef) {
				return null;
			}
			return new DelComponentCommand(comp.getParent(), comp);
		}
		return null;
	}

	public void showTargetFeedback(Request request) {
		Object model = getHost().getModel();
		if (model instanceof IContainerDef && !(model instanceof LayoutDef)) {
			// TODO ChangeBoundsRequestの場合、EditPartは親EditPart内移動する場合、
			// ハイライト表示はしないように
			boolean doHighlight = false;
			if (request instanceof CreateRequest) {
				doHighlight = true;
			}
			if (request instanceof ChangeBoundsRequest) {
				doHighlight = true;
			}
			if (doHighlight) {
				// ハイライト表示
				((AbstractGraphicalEditPart) getHost()).getFigure().setBackgroundColor(
						ColorConstants.lightGray);
				((AbstractGraphicalEditPart) getHost()).getFigure().setOpaque(true);
			}
		}
		super.showTargetFeedback(request);
	}

	public void eraseTargetFeedback(Request request) {
		Object model = getHost().getModel();
		if (model instanceof IContainerDef && !(model instanceof LayoutDef)) {
			if (request instanceof CreateRequest
					|| request instanceof ChangeBoundsRequest) {
				((AbstractGraphicalEditPart) getHost()).getFigure().setBackgroundColor(
						ColorConstants.white);
				((AbstractGraphicalEditPart) getHost()).getFigure().setOpaque(false);
			}
		}
		super.eraseTargetFeedback(request);
	}
}
