diff --git a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/utils/KernelModelElementUtils.java b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/utils/KernelModelElementUtils.java index e7334bcce8917404108bcb4ed5c311cf2b91027a..e999598767dd9adef7e9a11cf392c6d35a063c30 100644 --- a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/utils/KernelModelElementUtils.java +++ b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/utils/KernelModelElementUtils.java @@ -32,7 +32,7 @@ import org.fortiss.tooling.kernel.service.IPersistencyService; * @author hoelzl * @author $Author$ * @version $Rev$ - * @ConQAT.Rating YELLOW Hash: 9C6BFB0E29B3623A7A4BE3DC69FA3BEC + * @ConQAT.Rating YELLOW Hash: 38F43F6516A99A19C128B6706D7CBE45 */ public final class KernelModelElementUtils { @@ -55,6 +55,27 @@ public final class KernelModelElementUtils { .getRootModelElement().eContents()); } + /** + * Returns the parent of an element with a certain type. + * + * @param element + * a model element. + * @param targetClass + * the class of the parent that is returned. + * @return instance of class <code>targetClass</code> or <code>null</code> + * if no such parent element was found. + */ + @SuppressWarnings("unchecked") + public static <T extends EObject> T getParentElement(EObject element, + Class<T> targetClass) { + EObject currentParent = element; + while (currentParent != null + && !(targetClass.isAssignableFrom(currentParent.getClass()))) { + currentParent = currentParent.eContainer(); + } + return (T) currentParent; + } + /** * Finds the model element referenced by the given * {@link IIdLabeledReference} element or <code>null</code> if no such