org.eclipse.emf.edit.command.InitializeCopyCommand Maven / Gradle / Ivy
/**
* Copyright (c) 2002-2010 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* IBM - Initial API and implementation
*/
package org.eclipse.emf.edit.command;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.FeatureMap;
import org.eclipse.emf.ecore.util.FeatureMapUtil;
import org.eclipse.emf.edit.EMFEditPlugin;
import org.eclipse.emf.edit.domain.EditingDomain;
/**
* The initialize copy command is implemented to set the values of an object copy based on those
* of the original (copied) object. It is a helper command used by the CopyCommand.
*
*
* An initialize copy command is an {@link OverrideableCommand}.
*/
public class InitializeCopyCommand extends AbstractOverrideableCommand
{
public static Command create(EditingDomain domain, Object owner, CopyCommand.Helper copyHelper)
{
return domain.createCommand(InitializeCopyCommand.class, new CommandParameter(owner, null, copyHelper));
}
/**
* This caches the label.
*/
protected static final String LABEL = EMFEditPlugin.INSTANCE.getString("_UI_InitializeCopyCommand_label");
/**
* This caches the description.
*/
protected static final String DESCRIPTION = EMFEditPlugin.INSTANCE.getString("_UI_InitializeCopyCommand_description");
/**
* This is the object being copied.
*/
protected EObject owner;
/**
* This is the object (copy) being initialized.
*/
protected EObject copy;
/**
* This is a map of objects to their copies
*/
protected CopyCommand.Helper copyHelper;
/**
* This constructs an instance that will copy the attribute values of value to those of owner.
*/
public InitializeCopyCommand(EditingDomain domain, EObject owner, CopyCommand.Helper copyHelper)
{
super(domain, LABEL, DESCRIPTION);
this.owner = owner;
this.copy = copyHelper.getCopy(owner);
this.copyHelper = copyHelper;
}
/**
* This is the object being copied.
*/
public EObject getOwner()
{
return owner;
}
/**
* This is the object (copy) being initialized.
*/
public EObject getCopy()
{
return copy;
}
/**
* This is the map of objects to their copies.
*/
public CopyCommand.Helper getCopyHelper()
{
return copyHelper;
}
@Override
protected boolean prepare()
{
return owner.eClass().isInstance(copy);
}
@Override
public void doExecute()
{
copyAttributes();
copyReferences();
}
protected Collection extends EAttribute> getAttributesToCopy()
{
return owner.eClass().getEAllAttributes();
}
/**
* This method will iterate over the attributes of the owner object and set them
* accordingly in the copy.
*/
protected void copyAttributes()
{
for (EAttribute attribute : getAttributesToCopy())
{
if (attribute.isChangeable() && !attribute.isDerived() && (attribute.isMany() || owner.eIsSet(attribute)))
{
Object value = owner.eGet(attribute);
if (!attribute.isMany())
{
copy.eSet(attribute, value);
}
else
{
@SuppressWarnings("unchecked")
List