/*
 * Created on 30-Sep-2005
 */
package fmp.provider.command;

import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.edit.command.SetCommand;
import org.eclipse.emf.edit.domain.EditingDomain;

import fmp.Clonable;
import fmp.FmpPackage;
import fmp.system.ModelManipulation;
import fmp.system.NodeIdDictionary;
import fmp.system.RoleQuery;

/**
 * @author Michal
 */
public class CloneCommand extends CompoundCommand {
	AdapterFactory adapterFactory;
	Clonable owner;
	EditingDomain domain;
	
	public CloneCommand(EditingDomain domain, AdapterFactory adapterFactory, Clonable owner) {
		this.owner = owner;
		this.adapterFactory = adapterFactory;
		this.domain = domain;
	}
	public void execute() {
		Clonable clone = (Clonable) ModelManipulation.INSTANCE.copy(owner, domain, adapterFactory, this);
		clone.setMin(1);
		clone.setMax(1);
		// set the prototype of a clone
		appendAndExecute(new SetCommand(domain, clone, FmpPackage.eINSTANCE.getClonable_Prototype(), owner));
		NodeIdDictionary.INSTANCE.visit(clone);
	}
	/**
	 * you can clone only if:
	 * - you are in the configuration that is: CONFIGURATION, MODEL_PROPERTIES, METAMODEL_PROPERTIES
	 */
	public boolean canExecute() {
		int locationType = RoleQuery.INSTANCE.getLocationType(owner);
		int[] cardinality = owner.getCheckboxViewCardinality();
		
		return (cardinality[1] > 1 || cardinality[1] == -1) && 
			   (locationType == RoleQuery.MODEL_PROPERTIES ||
			   locationType == RoleQuery.METAMODEL_PROPERTIES ||
		       locationType == RoleQuery.CONFIGURATION || 
			   locationType == RoleQuery.CONFIGURATION_PROPERTIES);
	}
}
