blob: a498d31ccaf0b28dd887da1011a64f1a7c501f8b [file] [log] [blame]
package org.eclipse.epf.library.edit.command;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import org.eclipse.emf.common.command.AbstractCommand;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.epf.library.edit.ui.UserInteractionHelper;
import org.eclipse.epf.library.edit.util.MethodElementPropertyHelper;
import org.eclipse.epf.uma.MethodElement;
import org.eclipse.epf.uma.MethodElementProperty;
import org.eclipse.epf.uma.UmaPackage;
public class MethodElementSetPropertyCommand extends AbstractCommand implements
IResourceAwareCommand {
protected MethodElement element;
protected String key;
protected String value;
protected String oldValue;
protected Collection<Resource> modifiedResources;
public MethodElementSetPropertyCommand(MethodElement element, String key, String value) {
this.element = element;
this.key = key;
this.value = value;
}
protected boolean prepare() {
// check if this operation will modify the element in opposite feature
// value
//
return UserInteractionHelper.checkModifyOpposite(element, UmaPackage.Literals.METHOD_ELEMENT__METHOD_ELEMENT_PROPERTY,
element);
}
public Collection<Resource> getModifiedResources() {
if (element != null && element.eResource() != null
&& modifiedResources == null) {
modifiedResources = new HashSet<Resource>();
modifiedResources.add(element.eResource());
}
if (modifiedResources == null) {
return Collections.emptyList();
}
return modifiedResources;
}
public void execute() {
redo();
}
public void redo() {
MethodElementProperty oldProperty = MethodElementPropertyHelper.getProperty(element, key);
if (oldProperty != null) {
oldValue = oldProperty.getValue();
} else {
oldValue = null;
}
MethodElementPropertyHelper.setProperty(element, key, value);
}
@Override
public void undo() {
if (oldValue != null) {
MethodElementPropertyHelper.setProperty(element, key, oldValue);
} else {
MethodElementPropertyHelper.removeProperty(element, key);
}
}
}