diff options
author | bblajer | 2007-02-23 16:19:37 +0000 |
---|---|---|
committer | bblajer | 2007-02-23 16:19:37 +0000 |
commit | 7f7691249db82f2c6b195d93a3042d935e47bf72 (patch) | |
tree | c8654742c4404415562b7bb6dc29f30ed0e26017 /plugins/org.eclipse.gmf.runtime.lite/src/org | |
parent | 32068774f466b6961fac64ee897bd4d690fe53cd (diff) | |
download | org.eclipse.gmf-tooling-7f7691249db82f2c6b195d93a3042d935e47bf72.tar.gz org.eclipse.gmf-tooling-7f7691249db82f2c6b195d93a3042d935e47bf72.tar.xz org.eclipse.gmf-tooling-7f7691249db82f2c6b195d93a3042d935e47bf72.zip |
ComponentEditPolicy separated into a top-level class;
CreateRequest now knows about the MODEL_ID;
All "Command" class references fully-qualified in templates to prevent compilation problems;
[170078]: Remove link with class from the domain model on deleting link's destination
Diffstat (limited to 'plugins/org.eclipse.gmf.runtime.lite/src/org')
3 files changed, 167 insertions, 0 deletions
diff --git a/plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/commands/DestroyElementCommand.java b/plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/commands/DestroyElementCommand.java new file mode 100644 index 000000000..5f2b9c589 --- /dev/null +++ b/plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/commands/DestroyElementCommand.java @@ -0,0 +1,147 @@ +/** + * Copyright (c) 2007 Borland Software Corporation + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * bblajer - initial API and implementation + */ +package org.eclipse.gmf.runtime.lite.commands; + +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.Map; + +import org.eclipse.emf.common.command.Command; +import org.eclipse.emf.common.command.CompoundCommand; +import org.eclipse.emf.common.util.UniqueEList; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.emf.edit.command.CommandParameter; +import org.eclipse.emf.edit.command.RemoveCommand; +import org.eclipse.emf.edit.command.SetCommand; +import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain; +import org.eclipse.emf.edit.domain.EditingDomain; +import org.eclipse.gmf.runtime.notation.NotationPackage; + +/** + * Command that should be used to destroy an underlying model element for which + * a notational element may be registered. It behaves effectively the same way + * as the {@link DeleteCommand}, but allows filtering of some references, so that they are not removed. + */ +public class DestroyElementCommand extends CompoundCommand { + /** + * This is the editing doman in which this command operates. + */ + protected EditingDomain domain; + + /** + * This is the collection of objects to be deleted. + */ + protected Collection<?> collection; + + /** + * This creates a command that deletes the given object. + */ + public static Command create(EditingDomain domain, Object object) { + return create(domain, Collections.singleton(object)); + } + + /** + * This creates a command that deletes the objects in the given collection. + */ + public static Command create(EditingDomain domain, Collection<?> collection) { + return domain.createCommand(DestroyElementCommand.class, new CommandParameter(null, null, collection)); + } + + protected static final String LABEL = "Delete"; + + protected static final String DESCRIPTION = "Remove the objects and clean up references to them from within the editing domain"; + + /** + * This constructs a command that deletes the objects in the given + * collection. + */ + public DestroyElementCommand(EditingDomain domain, Collection<?> collection) { + super(0, LABEL, DESCRIPTION); + this.domain = domain; + this.collection = collection; + } + + /** + * This constructs a command that deletes the objects in the collection + * specified by the given command parameter. This constructor is called by + * default implementations of editing domains. + */ + public DestroyElementCommand(EditingDomain domain, CommandParameter commandParameter) { + this(domain, commandParameter.getCollection()); + } + + /** + * Returns whether value should be removed from the reference identified by + * the given setting. + */ + protected boolean shouldRemoveReference(EStructuralFeature.Setting setting) { + return NotationPackage.eINSTANCE.getView_Element() != setting.getEStructuralFeature(); + } + + protected boolean prepare() { + prepareCommand(); + return super.prepare(); + } + + protected void prepareCommand() { + append(RemoveCommand.create(domain, collection)); + } + + @Override + public void execute() { + Collection<Object> eObjects = new UniqueEList<Object>(); + for (Iterator<?> i = collection.iterator(); i.hasNext(); ) { + Object object = AdapterFactoryEditingDomain.unwrap(i.next()); + if (object instanceof EObject) { + eObjects.add(object); + for (Iterator<EObject> j = ((EObject) object).eAllContents(); j.hasNext(); ) { + eObjects.add(j.next()); + } + } else if (object instanceof Resource) { + for (Iterator<EObject> j = ((Resource) object).getAllContents(); j.hasNext(); ) { + eObjects.add(j.next()); + } + } + } + + Map<EObject, Collection<EStructuralFeature.Setting>> usages = EcoreUtil.UsageCrossReferencer.findAll(eObjects, domain.getResourceSet()); + + super.execute(); + + for (Iterator<Map.Entry<EObject, Collection<EStructuralFeature.Setting>>> i = usages.entrySet().iterator(); i.hasNext(); ) { + Map.Entry<EObject, Collection<EStructuralFeature.Setting>> entry = i.next(); + EObject eObject = entry.getKey(); + Collection<EStructuralFeature.Setting> settings = entry.getValue(); + for (Iterator<EStructuralFeature.Setting> j = settings.iterator(); j.hasNext(); ) { + EStructuralFeature.Setting setting = j.next(); + if (!shouldRemoveReference(setting)) { + continue; + } + EObject referencingEObject = setting.getEObject(); + if (!eObjects.contains(referencingEObject)) { + EStructuralFeature eStructuralFeature = setting.getEStructuralFeature(); + if (eStructuralFeature.isChangeable()) { + if (eStructuralFeature.isMany()) { + appendAndExecute(RemoveCommand.create(domain, referencingEObject, eStructuralFeature, eObject)); + } else { + appendAndExecute(SetCommand.create(domain, referencingEObject, eStructuralFeature, null)); + } + } + } + } + } + } +} diff --git a/plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/requests/CreateConnectionRequestEx.java b/plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/requests/CreateConnectionRequestEx.java index 571013d56..65cb331b8 100644 --- a/plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/requests/CreateConnectionRequestEx.java +++ b/plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/requests/CreateConnectionRequestEx.java @@ -19,12 +19,18 @@ import org.eclipse.gef.requests.CreateConnectionRequest; */ public class CreateConnectionRequestEx extends CreateConnectionRequest { private final int[] visualIds; + private final String modelID; /** * Creates a new request to create a connection of the given object types. */ public CreateConnectionRequestEx(int[] visualIds) { + this(null, visualIds); + } + + public CreateConnectionRequestEx(String modelID, int[] visualIds) { this.visualIds = visualIds; + this.modelID = modelID; } /** @@ -37,4 +43,8 @@ public class CreateConnectionRequestEx extends CreateConnectionRequest { public void setCreatedObject(Object createdObject) { ((ModelCreationFactory)getFactory()).setCreatedObject(createdObject); } + + public String getModelID() { + return modelID; + } } diff --git a/plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/requests/CreateRequestEx.java b/plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/requests/CreateRequestEx.java index ef233d8b6..42b1db2f6 100644 --- a/plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/requests/CreateRequestEx.java +++ b/plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/requests/CreateRequestEx.java @@ -18,12 +18,18 @@ import org.eclipse.gef.requests.CreateRequest; */ public class CreateRequestEx extends CreateRequest { private final int[] visualIds; + private final String modelID; /** * Creates a new request to create an object of the given object types. */ public CreateRequestEx(int[] visualIds) { + this(null, visualIds); + } + + public CreateRequestEx(String modelID, int[] visualIds) { this.visualIds = visualIds; + this.modelID = modelID; } /** @@ -36,4 +42,8 @@ public class CreateRequestEx extends CreateRequest { public void setCreatedObject(Object createdObject) { ((ModelCreationFactory)getFactory()).setCreatedObject(createdObject); } + + public String getModelID() { + return modelID; + } } |