From 4c400c28ce795546270f4d985e3809693cb26f90 Mon Sep 17 00:00:00 2001 From: "Knoll, Vladislav" Date: Wed, 8 Apr 2026 11:24:26 +0200 Subject: [PATCH] Fixed Topic copy with children When creating a Topic from another ITopic also create copies of the topics children and bind these to the newly created topic instead of moving the children from the original topic to new newly created one. This fixes the issue of not returning the right parent when calling getChildren() for a child topic. Contributes to https://github.com/eclipse-platform/eclipse.platform/issues/525 --- .../src/org/eclipse/help/internal/Topic.java | 14 ++++-- .../org/eclipse/help/internal/UAElement.java | 44 ++++++++++--------- .../help/internal/UAElementFactory.java | 11 +++-- .../ua/tests/help/other/TopicTest.java | 15 +++---- 4 files changed, 46 insertions(+), 38 deletions(-) diff --git a/ua/org.eclipse.help/src/org/eclipse/help/internal/Topic.java b/ua/org.eclipse.help/src/org/eclipse/help/internal/Topic.java index 16d7d2b997a..e7656dbd998 100644 --- a/ua/org.eclipse.help/src/org/eclipse/help/internal/Topic.java +++ b/ua/org.eclipse.help/src/org/eclipse/help/internal/Topic.java @@ -13,9 +13,12 @@ *******************************************************************************/ package org.eclipse.help.internal; +import java.util.Arrays; + import org.eclipse.help.ICriteria; import org.eclipse.help.ITopic; import org.eclipse.help.ITopic2; +import org.eclipse.help.IUAElement; import org.w3c.dom.Element; public class Topic extends UAElement implements ITopic2 { @@ -24,7 +27,7 @@ public class Topic extends UAElement implements ITopic2 { public static final String ATTRIBUTE_HREF = "href"; //$NON-NLS-1$ public static final String ATTRIBUTE_LABEL = "label"; //$NON-NLS-1$ public static final String ATTRIBUTE_ICON = "icon"; //$NON-NLS-1$ - public static final String ATTRIBUTE_SORT= "sort"; //$NON-NLS-1$ + public static final String ATTRIBUTE_SORT = "sort"; //$NON-NLS-1$ public Topic() { super(NAME); @@ -34,16 +37,19 @@ public Topic(ITopic src) { super(NAME, src); setHref(src.getHref()); setLabel(src.getLabel()); - appendChildren(src.getChildren()); + IUAElement[] copiedChildren = Arrays.stream(src.getChildren()).map(UAElementFactory::newElement) + .toArray(IUAElement[]::new); + + appendChildren(copiedChildren); } @Override - public String getIcon(){ + public String getIcon() { return getAttribute(ATTRIBUTE_ICON); } @Override - public boolean isSorted(){ + public boolean isSorted() { return "true".equalsIgnoreCase(getAttribute(ATTRIBUTE_SORT)); //$NON-NLS-1$ } diff --git a/ua/org.eclipse.help/src/org/eclipse/help/internal/UAElement.java b/ua/org.eclipse.help/src/org/eclipse/help/internal/UAElement.java index 7c7c392766c..6af4b7bbf35 100644 --- a/ua/org.eclipse.help/src/org/eclipse/help/internal/UAElement.java +++ b/ua/org.eclipse.help/src/org/eclipse/help/internal/UAElement.java @@ -34,6 +34,7 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; + /* * Base class for UA model elements. */ @@ -59,6 +60,7 @@ public Filter(String name, String value, boolean isNegated) { this.value = value; this.isNegated = isNegated; } + String name; String value; boolean isNegated; @@ -82,7 +84,7 @@ public UAElement(String name, IUAElement src) { } private void copyFilters(IUAElement src) { - UAElement sourceElement = (UAElement)src; + UAElement sourceElement = (UAElement) src; String filter = sourceElement.getAttribute(ATTRIBUTE_FILTER); if (filter != null && filter.length() > 0) { this.setAttribute(ATTRIBUTE_FILTER, filter); @@ -101,15 +103,14 @@ private Filter[] getFilterElements() { if (node.getNodeType() == Node.ELEMENT_NODE) { String elementKind = node.getNodeName(); if (ExpressionTagNames.ENABLEMENT.equals(elementKind)) { - Element enablement = (Element)node; + Element enablement = (Element) node; try { enablementExpression = ExpressionConverter.getDefault().perform(enablement); - } - catch (CoreException e) { + } catch (CoreException e) { } } else if (ELEMENT_FILTER.equals(elementKind)) { - Element filter = (Element)node; + Element filter = (Element) node; String filterName = filter.getAttribute(ATTRIBUTE_NAME); String value = filter.getAttribute(ATTRIBUTE_VALUE); if (filterName.length() > 0 && value.length() > 0) { @@ -146,14 +147,15 @@ public void appendChildren(IUAElement[] children) { if (this.children == null && children.length > 0) { this.children = new ArrayList<>(4); } - for (int i=0;i T[] getChildren(Class clazz) { IUAElement[] children = getChildren(); if (children.length > 0) { List list = new ArrayList<>(); - for (int i=0;i> classByElementName; @@ -119,9 +119,9 @@ public static UAElement newElement(Element element) { } public static UAElement newElement(IUAElement src) { - for (int i=0;i interfaze = interfaceTable[i][0]; - Class clazz = interfaceTable[i][1]; + for (Class[] element : interfaceTable) { + Class interfaze = element[0]; + Class clazz = element[1]; if (interfaze.isAssignableFrom(src.getClass())) { try { Constructor constructor = clazz.getConstructor(interfaze); @@ -132,6 +132,9 @@ public static UAElement newElement(IUAElement src) { ILog.of(UAContentFilter.class).error(msg, e); } } + if (src instanceof UAElement uaElement) { + return new UAElement(uaElement.getElementName(), uaElement); + } } return null; } diff --git a/ua/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/other/TopicTest.java b/ua/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/other/TopicTest.java index ac2a2caf7ee..1f11f4fcbde 100644 --- a/ua/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/other/TopicTest.java +++ b/ua/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/other/TopicTest.java @@ -129,8 +129,7 @@ public void testCopyTopicWithChild() { assertThat(topic2.getSubtopics()).hasSize(1); } - /* - * Disabled, see Bug 210024 [Help] Topic element problems constructing from an ITopic + @Test public void testCopyTopicWithChildRemoveChild() { Topic topic1; topic1 = createTopic(TOPIC_WITH_CHILD); @@ -142,14 +141,12 @@ public void testCopyTopicWithChildRemoveChild() { assertEquals(0, topic1.getSubtopics().length); assertEquals(1, topic2.getSubtopics().length); } - */ /* * Test the assumption that when a topic is created from another topic not only * the topic but all the children are recursively copied */ - /* - * Disabled, see Bug 210024 [Help] Topic element problems constructing from an ITopic + @Test public void testCopyTopicWithChildCheckParents() { Topic topic1; topic1 = createTopic(TOPIC_WITH_CHILD); @@ -158,14 +155,14 @@ public void testCopyTopicWithChildCheckParents() { assertEquals(ECLIPSE_HREF, topic1.getHref()); assertEquals(1, topic1.getSubtopics().length); Topic child1 = (Topic)topic1.getSubtopics()[0]; - assertTrue(child1.getParentElement() == topic1); + assertEquals(child1.getParentElement(), topic1); assertEquals(ECLIPSE, topic2.getLabel()); assertEquals(ECLIPSE_HREF, topic2.getHref()); assertEquals(1, topic2.getSubtopics().length); - Topic child2 = (Topic)topic1.getSubtopics()[0]; - assertTrue(child2.getParentElement() == topic2); + Topic child2 = (Topic) topic2.getSubtopics()[0]; + assertEquals(child2.getParentElement(), topic2); } - */ + @Test public void testEnabledTopic() { Topic topic = createTopic(TOPIC_WITH_ENABLEMENT);