diff --git a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/storage/eclipse/ModelContext.java b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/storage/eclipse/ModelContext.java index 700eb448837a5ba8a6fb13b2d3b1f87fc17b51f4..d530c23330302a712529ba54bbc45d31c0a6a2d9 100644 --- a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/storage/eclipse/ModelContext.java +++ b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/storage/eclipse/ModelContext.java @@ -25,11 +25,9 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.lang.reflect.Method; import java.util.EventObject; -import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; -import java.util.Set; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.CoreException; @@ -47,6 +45,7 @@ import org.fortiss.tooling.kernel.ToolingKernelActivator; import org.fortiss.tooling.kernel.extension.data.ITopLevelElement; import org.fortiss.tooling.kernel.model.IIdLabeled; import org.fortiss.tooling.kernel.service.IPersistencyService; +import org.fortiss.tooling.kernel.utils.UniqueIDUtils; /** * The model context provides additional commands and hooks for a model. @@ -55,7 +54,7 @@ import org.fortiss.tooling.kernel.service.IPersistencyService; * @author hummel * @author $Author$ * @version $Rev$ - * @ConQAT.Rating YELLOW Hash: DC486D7885D3E9603B1672F7B269ABC1 + * @ConQAT.Rating YELLOW Hash: 2A3C09281619DEC4EBEDBA048E8E4046 */ class ModelContext implements ITopLevelElement, CommandStackListener { @@ -139,73 +138,17 @@ class ModelContext implements ITopLevelElement, CommandStackListener { runAsCommand(new Runnable() { @Override public void run() { - generateMissingIDs(); + maxId = UniqueIDUtils.generateMissingIDs( + getRootModelElement(), maxId); } }); } } - /** Generates missing IDs. Must be called within transaction. */ - private void generateMissingIDs() { - for (Iterator<EObject> i = getRootModelElement().eAllContents(); i - .hasNext();) { - EObject eo = i.next(); - if (eo instanceof IIdLabeled) { - if (((IIdLabeled) eo).getId() <= 0) { - ((IIdLabeled) eo).setId((++maxId)); - } - } - } - } - /** {@inheritDoc} */ @Override public void prepareIDs(EObject other) { - if (other == null) { - return; - } - - boolean needSmart = false; - if (other instanceof IIdLabeled) { - if (((IIdLabeled) other).getId() <= 0) { - ((IIdLabeled) other).setId(++maxId); - } else { - needSmart = true; - } - } - - for (Iterator<EObject> i = other.eAllContents(); i.hasNext();) { - EObject eo = i.next(); - if (eo instanceof IIdLabeled) { - if (((IIdLabeled) eo).getId() <= 0) { - ((IIdLabeled) eo).setId(++maxId); - } else { - needSmart = true; - } - } - } - - if (needSmart) { - Set<Integer> usedIDs = new HashSet<Integer>(); - for (Iterator<EObject> i = getRootModelElement().eAllContents(); i - .hasNext();) { - EObject eo = i.next(); - if (eo instanceof IIdLabeled) { - usedIDs.add(((IIdLabeled) eo).getId()); - } - } - - for (Iterator<EObject> i = other.eAllContents(); i.hasNext();) { - EObject eo = i.next(); - if (eo instanceof IIdLabeled) { - if (!usedIDs.add(((IIdLabeled) eo).getId())) { - int newId = ++maxId; - ((IIdLabeled) eo).setId(newId); - usedIDs.add(newId); - } - } - } - } + maxId = UniqueIDUtils.prepareIDs(other, getRootModelElement(), maxId); } /** Destroys this context and closes all dependent editors. */ diff --git a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/utils/UniqueIDUtils.java b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/utils/UniqueIDUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..77ef138fe77f9e0db5540eb226641405703dfc1b --- /dev/null +++ b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/utils/UniqueIDUtils.java @@ -0,0 +1,122 @@ +/*--------------------------------------------------------------------------+ +$Id$ +| | +| Copyright 2012 ForTISS GmbH | +| | +| Licensed under the Apache License, Version 2.0 (the "License"); | +| you may not use this file except in compliance with the License. | +| You may obtain a copy of the License at | +| | +| http://www.apache.org/licenses/LICENSE-2.0 | +| | +| Unless required by applicable law or agreed to in writing, software | +| distributed under the License is distributed on an "AS IS" BASIS, | +| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | +| See the License for the specific language governing permissions and | +| limitations under the License. | ++--------------------------------------------------------------------------*/ +package org.fortiss.tooling.kernel.utils; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import org.eclipse.emf.ecore.EObject; +import org.fortiss.tooling.kernel.model.IIdLabeled; + +/** + * Utility methods for unique IDs of {@link IIdLabeled} model elements. + * IMPORTANT: Use this methods with care! It usually the task of the persistency + * layer to assign consistent IDs. However, test models and programmatic models + * may need to use this capability without the persistency mechanism in place. + * + * @author hoelzl + * @author $Author$ + * @version $Rev$ + * @ConQAT.Rating YELLOW Hash: 8A4299022B07F32CEED5A061DC03FBD2 + */ +public class UniqueIDUtils { + + /** + * Generates missing IDs of the given model starting from the given current + * maximum ID. + * + * @param existingModel + * the model to be updated + * @param currentMaxId + * the current maximum ID + */ + public static int generateMissingIDs(EObject existingModel, int currentMaxId) { + for (Iterator<EObject> i = existingModel.eAllContents(); i.hasNext();) { + EObject eo = i.next(); + if (eo instanceof IIdLabeled) { + if (((IIdLabeled) eo).getId() <= 0) { + ((IIdLabeled) eo).setId((++currentMaxId)); + } + } + } + return currentMaxId; + } + + /** + * Assigns unique IDs to the given object and its content. + * + * @param object + * the model element to be prepared + * @param existingModel + * the existing model to be considered for used IDs if object + * already contains IDs > 0 + * @param currentMaxId + * the current maximum ID to be started + * @return the new current maximum ID + */ + public static int prepareIDs(EObject object, EObject existingModel, + int currentMaxId) { + if (object == null) { + return currentMaxId; + } + + boolean needSmart = false; + if (object instanceof IIdLabeled) { + if (((IIdLabeled) object).getId() <= 0) { + ((IIdLabeled) object).setId(++currentMaxId); + } else { + needSmart = true; + } + } + + for (Iterator<EObject> i = object.eAllContents(); i.hasNext();) { + EObject eo = i.next(); + if (eo instanceof IIdLabeled) { + if (((IIdLabeled) eo).getId() <= 0) { + ((IIdLabeled) eo).setId(++currentMaxId); + } else { + needSmart = true; + } + } + } + + if (needSmart) { + Set<Integer> usedIDs = new HashSet<Integer>(); + for (Iterator<EObject> i = existingModel.eAllContents(); i + .hasNext();) { + EObject eo = i.next(); + if (eo instanceof IIdLabeled) { + usedIDs.add(((IIdLabeled) eo).getId()); + } + } + + for (Iterator<EObject> i = object.eAllContents(); i.hasNext();) { + EObject eo = i.next(); + if (eo instanceof IIdLabeled) { + if (!usedIDs.add(((IIdLabeled) eo).getId())) { + int newId = ++currentMaxId; + ((IIdLabeled) eo).setId(newId); + usedIDs.add(newId); + } + } + } + } + return currentMaxId; + } +}