diff options
| author | Laurent Fasani | 2018-09-28 09:00:52 +0000 |
|---|---|---|
| committer | Maxime Porhel | 2018-09-28 12:43:07 +0000 |
| commit | 828c9cb50d2669c928f20b941507698dde9bc0b6 (patch) | |
| tree | 342511b4e307403e2ec033b2e6d7009b42e139b3 | |
| parent | 83baac34daff867dc1f2942b6732a3f46043aee8 (diff) | |
| download | org.eclipse.sirius-828c9cb50d2669c928f20b941507698dde9bc0b6.tar.gz org.eclipse.sirius-828c9cb50d2669c928f20b941507698dde9bc0b6.tar.xz org.eclipse.sirius-828c9cb50d2669c928f20b941507698dde9bc0b6.zip | |
[525261] Add a SiriusCopier to handle id attribute
* This EMF copier ensures that an EAttribute which is id attribute will
have a different value on the copied object than on the original object.
Bug 525261
Change-Id: Idefee2cb67aec4f48342368169dd8cffba8647e3
Signed-off-by: Laurent Fasani <laurent.fasani@obeo.fr>
3 files changed, 110 insertions, 0 deletions
diff --git a/plugins/org.eclipse.sirius.common/src/org/eclipse/sirius/common/tools/api/util/SiriusCopier.java b/plugins/org.eclipse.sirius.common/src/org/eclipse/sirius/common/tools/api/util/SiriusCopier.java new file mode 100644 index 0000000000..0cdf129301 --- /dev/null +++ b/plugins/org.eclipse.sirius.common/src/org/eclipse/sirius/common/tools/api/util/SiriusCopier.java @@ -0,0 +1,104 @@ +/******************************************************************************* + * Copyright (c) 2018 THALES GLOBAL SERVICES. + * 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: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.common.tools.api.util; + +import java.util.Collection; + +import org.eclipse.emf.ecore.EAttribute; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EStructuralFeature.Setting; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.emf.ecore.util.EcoreUtil.Copier; + +/** + * Utility class used to copy object with special management for {@link EAttribute} that are id attribute.</br> + * This copier ensure that id attribute of copied object will have a different value from the original Object. + * + * @see Copier + * + * @author <a href="mailto:laurent.fasani@obeo.fr">Laurent Fasani</a> + */ +public class SiriusCopier extends Copier { + + private static final long serialVersionUID = -5736164116383051335L; + + /** + * This method does not copy {@link EAttribute} that is an id (see {@link EAttribute#isID()}).<br/> + * If the id has not been set by the factory and if the attribute type is String, sets it using the + * EcoreUtil.generateUUID(). + * + * @see org.eclipse.emf.ecore.util.EcoreUtil.Copier#copyAttributeValue(EAttribute, EObject, Object, Setting) + */ + @Override + protected void copyAttributeValue(EAttribute eAttribute, EObject eObject, Object value, Setting setting) { + Object valueToCopy = value; + if (eAttribute.isID()) { + if (!setting.isSet() && String.class.equals(eAttribute.getEAttributeType().getInstanceClass())) { + // See org.eclipse.emf.ecore.util.EcoreUtil.setID(EObject, String) + valueToCopy = EcoreUtil.generateUUID(); + } + } + super.copyAttributeValue(eAttribute, eObject, valueToCopy, setting); + } + + /** + * Utility to ease the use of the {@link SiriusCopier}. <br/> + * The behavior of the utility methods comes from EcoreUtil, the only change it the use of a {@link SiriusCopier} + * instead of a {@link EcoreUtil.Copier}. + */ + public static class Helper { + + /** + * Returns a self-contained copy of the eObject. + * + * The copy will have an id different from the copied EObject, see {@link SiriusCopier}. + * + * @param eObject + * the object to copy. + * @param <T> + * a subclass of {@link EObject} + * @return the copy + * @see SiriusCopier + * @see EcoreUtil#copy(EObject) + */ + public static <T extends EObject> T copy(T eObject) { + + Copier copier = new SiriusCopier(); + EObject result = copier.copy(eObject); + copier.copyReferences(); + + @SuppressWarnings("unchecked") + T t = (T) result; + return t; + } + + /** + * Returns a collection of the self-contained copies of each {@link EObject} in eObjects. + * + * The copies will have an id different from the copied EObjects, see {@link SiriusCopier}. + * + * @param eObjects + * the collection of objects to copy. + * @param <T> + * a subclass of {@link EObject} + * @return the collection of copies. + * @see SiriusCopier + * @see EcoreUtil#copyAll(Collection) + */ + public static <T> Collection<T> copyAll(Collection<? extends T> eObjects) { + Copier copier = new SiriusCopier(); + Collection<T> result = copier.copyAll(eObjects); + copier.copyReferences(); + return result; + } + } + +} diff --git a/plugins/org.eclipse.sirius.doc/doc/Release_Notes.html b/plugins/org.eclipse.sirius.doc/doc/Release_Notes.html index e0ab93a5ad..c6b1965a95 100644 --- a/plugins/org.eclipse.sirius.doc/doc/Release_Notes.html +++ b/plugins/org.eclipse.sirius.doc/doc/Release_Notes.html @@ -321,6 +321,11 @@ <code>PREF_ASK_TO_SAVE_RESOURCE_AFTER_MIGRATION</code> has been added in <code>CommonPreferencesConstants</code>. If true users will be asked to save the VSM or aird if it has been automatically migrated after a user action. </li> + <li><span class="label label-success">Added</span> The new + <code>org.eclipse.sirius.common.tools.api.util.SiriusCopier</code> has been added to provide the ability to copy an object without copying the attribute seen as id by EMF. If this id is not set by the factory or during object creation and if its concrete expected type is + <code>String</code>, it is set during the copy using + <code>org.eclipse.emf.ecore.util.EcoreUtil.generateUUID()</code>. + </li> </ul> <h4 id="Changesinorg.eclipse.sirius.tests.swtbot.support">Changes in <code>org.eclipse.sirius.tests.swtbot.support</code> diff --git a/plugins/org.eclipse.sirius.doc/doc/Release_Notes.textile b/plugins/org.eclipse.sirius.doc/doc/Release_Notes.textile index 9009e2ffcd..4ad52d4336 100644 --- a/plugins/org.eclipse.sirius.doc/doc/Release_Notes.textile +++ b/plugins/org.eclipse.sirius.doc/doc/Release_Notes.textile @@ -91,6 +91,7 @@ h4. Changes in @org.eclipse.sirius.diagram.sequence.ui@ h4. Changes in @org.eclipse.sirius.common@ * <span class="label label-success">Added</span> The new preference @PREF_ASK_TO_SAVE_RESOURCE_AFTER_MIGRATION@ has been added in @CommonPreferencesConstants@. If true users will be asked to save the VSM or aird if it has been automatically migrated after a user action. +* <span class="label label-success">Added</span> The new @org.eclipse.sirius.common.tools.api.util.SiriusCopier@ has been added to provide the ability to copy an object without copying the attribute seen as id by EMF. If this id is not set by the factory or during object creation and if its concrete expected type is @String@, it is set during the copy using @org.eclipse.emf.ecore.util.EcoreUtil.generateUUID()@. h4. Changes in @org.eclipse.sirius.tests.swtbot.support@ |
