Skip to content
Snippets Groups Projects
Commit 3962c337 authored by Sudeep Kanav's avatar Sudeep Kanav
Browse files

distinct names of new component

refs 2572
parent 1a321a72
No related branches found
No related tags found
No related merge requests found
......@@ -20,6 +20,7 @@ package org.fortiss.tooling.base.compose;
import static org.eclipse.emf.ecore.util.EcoreUtil.delete;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import org.eclipse.emf.ecore.EObject;
......@@ -30,6 +31,7 @@ import org.fortiss.tooling.base.model.element.IModelElementReference;
import org.fortiss.tooling.base.model.element.IModelElementSpecification;
import org.fortiss.tooling.kernel.extension.IElementCompositor;
import org.fortiss.tooling.kernel.extension.data.IElementCompositionContext;
import org.fortiss.tooling.kernel.model.INamedElement;
import org.fortiss.tooling.kernel.service.IElementCompositorService;
/**
......@@ -39,7 +41,7 @@ import org.fortiss.tooling.kernel.service.IElementCompositorService;
* @author diewald
* @author $Author$
* @version $Rev$
* @ConQAT.Rating GREEN Hash: 58F65C328C5D37AA24F7593C34F224F1
* @ConQAT.Rating YELLOW Hash: FB3638BD4FAE190CE22B8C7C89F028AB
*/
public abstract class ModelElementCompositorBase<C extends EObject> extends Observable implements
IElementCompositor<C> {
......@@ -57,6 +59,7 @@ public abstract class ModelElementCompositorBase<C extends EObject> extends Obse
if(contained instanceof IModelElement) {
IAnnotationValueService.INSTANCE.instantiateAnnotations((IModelElement)contained);
}
makeNameDistinct(container, contained);
return true;
}
......@@ -147,4 +150,32 @@ public abstract class ModelElementCompositorBase<C extends EObject> extends Obse
delete(element, true);
return true;
}
/**
* This function checks if the name of any element of same type and name as "contained" exists
* in the container, and modifies the name in case if required.
* */
private void makeNameDistinct(EObject container, EObject contained) {
if(!(contained instanceof INamedElement) || ((INamedElement)contained).getName() == null)
return;
INamedElement neNew = (INamedElement)contained;
String newName = ((INamedElement)contained).getName();
List<String> listNames = new ArrayList<String>();
for(EObject o : container.eContents()) {
if(o.getClass().isInstance(contained) && o instanceof INamedElement && o != contained) {
listNames.add(((INamedElement)o).getName());
}
}
if(!listNames.contains(newName)) {
return;
}
int index = 1;
while(listNames.contains(newName + index))
index++;
neNew.setName(neNew.getName() + index);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment