From eca9c4d60af4177b4619de0a9cef43bd99c7b695 Mon Sep 17 00:00:00 2001 From: Patrick Ziegler Date: Sat, 6 Jan 2024 11:10:42 +0100 Subject: [PATCH] [Draw2D] Remove data field in Figure with GEF VisualPartMap We use the data field to keep track of the EditPart corresponding to a given Figure. The same functionality is provided by the VisualPartMap of the EditPartViewer interface. --- .../policy/TabOrderContainerEditPolicy.java | 4 +-- .../org/eclipse/wb/draw2d/Figure.java | 16 +--------- .../wb/gef/graphical/GraphicalEditPart.java | 11 +++++-- .../gef/core/TargetEditPartFindVisitor.java | 13 +++++--- .../gef/graphical/GraphicalViewer.java | 4 +-- .../wb/core/gef/figure/TextFeedback.java | 9 ++---- .../gef/part/menu/SubmenuAwareEditPart.java | 12 ++++--- .../eclipse/wb/tests/draw2d/FigureTest.java | 27 +--------------- .../eclipse/wb/tests/gef/EditPartTest.java | 32 +++++++++++++------ 9 files changed, 57 insertions(+), 71 deletions(-) diff --git a/org.eclipse.wb.core.java/src/org/eclipse/wb/core/gef/policy/TabOrderContainerEditPolicy.java b/org.eclipse.wb.core.java/src/org/eclipse/wb/core/gef/policy/TabOrderContainerEditPolicy.java index 285da0882..36bc32e15 100644 --- a/org.eclipse.wb.core.java/src/org/eclipse/wb/core/gef/policy/TabOrderContainerEditPolicy.java +++ b/org.eclipse.wb.core.java/src/org/eclipse/wb/core/gef/policy/TabOrderContainerEditPolicy.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011, 2023 Google, Inc. + * Copyright (c) 2011, 2024 Google, Inc. * 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 @@ -99,7 +99,7 @@ private void showChildrenIndexes(List children, Object selectedChild) { m_indexFeedbacks.add(feedback); // register feedback with edit part, so we can click on number feedback as // well as on EditPart's main figure - feedback.setData(part); + viewer.getVisualPartMap().put(feedback.getLabel(), part); // set background feedback.setBackground(ColorConstants.yellow); if (child == selectedChild) { diff --git a/org.eclipse.wb.core/src-draw2d/org/eclipse/wb/draw2d/Figure.java b/org.eclipse.wb.core/src-draw2d/org/eclipse/wb/draw2d/Figure.java index ebf5057e8..b57a4d88c 100644 --- a/org.eclipse.wb.core/src-draw2d/org/eclipse/wb/draw2d/Figure.java +++ b/org.eclipse.wb.core/src-draw2d/org/eclipse/wb/draw2d/Figure.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011, 2023 Google, Inc. + * Copyright (c) 2011, 2024 Google, Inc. * 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 @@ -33,7 +33,6 @@ public class Figure extends org.eclipse.draw2d.Figure { private Figure m_parent; private List
m_children; - private Object m_data; private String m_toolTipText; private ICustomTooltipProvider m_customTooltipProvider; @@ -344,19 +343,6 @@ protected void paintClientArea(Graphics graphics) { // Properties // //////////////////////////////////////////////////////////////////////////// - /** - * Get user define data. - */ - public Object getData() { - return m_data; - } - - /** - * Set user define data. - */ - public void setData(Object data) { - m_data = data; - } /** * Returns the receiver's tool tip text, or null if it has not been set. diff --git a/org.eclipse.wb.core/src-gef/org/eclipse/wb/gef/graphical/GraphicalEditPart.java b/org.eclipse.wb.core/src-gef/org/eclipse/wb/gef/graphical/GraphicalEditPart.java index 32376a64d..1502dad3b 100644 --- a/org.eclipse.wb.core/src-gef/org/eclipse/wb/gef/graphical/GraphicalEditPart.java +++ b/org.eclipse.wb.core/src-gef/org/eclipse/wb/gef/graphical/GraphicalEditPart.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011, 2023 Google, Inc. + * Copyright (c) 2011, 2024 Google, Inc. * 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 @@ -15,6 +15,7 @@ import org.eclipse.wb.gef.core.tools.DragEditPartTracker; import org.eclipse.wb.gef.core.tools.Tool; +import org.eclipse.gef.EditPartViewer; import org.eclipse.gef.Request; /** @@ -67,7 +68,9 @@ protected void addChildVisual(org.eclipse.gef.EditPart childPart, int index) { if (!graphicalChildPart.addSelfVisual(index)) { getContentPane().add(graphicalChildPart.getFigure(), index); } - graphicalChildPart.getFigure().setData(childPart); + EditPartViewer graphicalChildViewer = graphicalChildPart.getViewer(); + Figure graphicalChildFigure = graphicalChildPart.getFigure(); + graphicalChildViewer.getVisualPartMap().put(graphicalChildFigure, childPart); } /** @@ -88,7 +91,9 @@ protected void removeChildVisual(org.eclipse.gef.EditPart childPart) { if (!graphicalChildPart.removeSelfVisual()) { getContentPane().remove(graphicalChildPart.getFigure()); } - graphicalChildPart.getFigure().setData(null); + EditPartViewer graphicalChildViewer = graphicalChildPart.getViewer(); + Figure graphicalChildFigure = graphicalChildPart.getFigure(); + graphicalChildViewer.getVisualPartMap().remove(graphicalChildFigure); } /** diff --git a/org.eclipse.wb.core/src-gef/org/eclipse/wb/internal/gef/core/TargetEditPartFindVisitor.java b/org.eclipse.wb.core/src-gef/org/eclipse/wb/internal/gef/core/TargetEditPartFindVisitor.java index 9ab1919b2..baf1a2acb 100644 --- a/org.eclipse.wb.core/src-gef/org/eclipse/wb/internal/gef/core/TargetEditPartFindVisitor.java +++ b/org.eclipse.wb.core/src-gef/org/eclipse/wb/internal/gef/core/TargetEditPartFindVisitor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011 Google, Inc. + * Copyright (c) 2011, 2024 Google, Inc. * 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 @@ -15,6 +15,8 @@ import org.eclipse.wb.internal.draw2d.FigureCanvas; import org.eclipse.wb.internal.draw2d.TargetFigureFindVisitor; +import org.eclipse.gef.EditPartViewer; + /** * This class use to find {@link EditPart} under mouse. * @@ -22,13 +24,16 @@ * @coverage gef.core */ public class TargetEditPartFindVisitor extends TargetFigureFindVisitor { + private final EditPartViewer m_viewer; + //////////////////////////////////////////////////////////////////////////// // // Constructor // //////////////////////////////////////////////////////////////////////////// - public TargetEditPartFindVisitor(FigureCanvas canvas, int x, int y) { + public TargetEditPartFindVisitor(FigureCanvas canvas, int x, int y, EditPartViewer viewer) { super(canvas, x, y); + m_viewer = viewer; } //////////////////////////////////////////////////////////////////////////// @@ -51,10 +56,10 @@ public EditPart getTargetEditPart() { /** * Extract {@link EditPart} from given {@link Figure}. */ - protected static EditPart extractEditPart(Figure figure) { + protected EditPart extractEditPart(Figure figure) { EditPart editPart = null; while (editPart == null && figure != null) { - editPart = (EditPart) figure.getData(); + editPart = (EditPart) m_viewer.getVisualPartMap().get(figure); figure = figure.getParent(); } return editPart; diff --git a/org.eclipse.wb.core/src-gef/org/eclipse/wb/internal/gef/graphical/GraphicalViewer.java b/org.eclipse.wb.core/src-gef/org/eclipse/wb/internal/gef/graphical/GraphicalViewer.java index 0475591f5..f0dad12b7 100644 --- a/org.eclipse.wb.core/src-gef/org/eclipse/wb/internal/gef/graphical/GraphicalViewer.java +++ b/org.eclipse.wb.core/src-gef/org/eclipse/wb/internal/gef/graphical/GraphicalViewer.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011, 2023 Google, Inc. + * Copyright (c) 2011, 2024 Google, Inc. * 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 @@ -179,7 +179,7 @@ public EditPart findTargetEditPart(int x, final Collection exclude, final IConditional conditional, String layer) { - TargetEditPartFindVisitor visitor = new TargetEditPartFindVisitor(m_canvas, x, y) { + TargetEditPartFindVisitor visitor = new TargetEditPartFindVisitor(m_canvas, x, y, this) { @Override protected boolean acceptVisit(Figure figure) { for (EditPart editPart : exclude) { diff --git a/org.eclipse.wb.core/src/org/eclipse/wb/core/gef/figure/TextFeedback.java b/org.eclipse.wb.core/src/org/eclipse/wb/core/gef/figure/TextFeedback.java index 66372c7d7..fc37adcc3 100644 --- a/org.eclipse.wb.core/src/org/eclipse/wb/core/gef/figure/TextFeedback.java +++ b/org.eclipse.wb.core/src/org/eclipse/wb/core/gef/figure/TextFeedback.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011, 2023 Google, Inc. + * Copyright (c) 2011, 2024 Google, Inc. * 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 @@ -112,11 +112,8 @@ public void setLocation(Point location) { m_label.setLocation(location); } - /** - * Set user defined data. - */ - public void setData(Object data) { - m_label.setData(data); + public Label getLabel() { + return m_label; } //////////////////////////////////////////////////////////////////////////// diff --git a/org.eclipse.wb.core/src/org/eclipse/wb/internal/core/gef/part/menu/SubmenuAwareEditPart.java b/org.eclipse.wb.core/src/org/eclipse/wb/internal/core/gef/part/menu/SubmenuAwareEditPart.java index c136181f6..2bd053dd7 100644 --- a/org.eclipse.wb.core/src/org/eclipse/wb/internal/core/gef/part/menu/SubmenuAwareEditPart.java +++ b/org.eclipse.wb.core/src/org/eclipse/wb/internal/core/gef/part/menu/SubmenuAwareEditPart.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011, 2023 Google, Inc. + * Copyright (c) 2011, 2024 Google, Inc. * 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 @@ -13,7 +13,6 @@ import org.eclipse.wb.draw2d.Figure; import org.eclipse.wb.gef.core.EditPart; import org.eclipse.wb.gef.core.IEditPartViewer; -import org.eclipse.wb.gef.core.policies.EditPolicy; import org.eclipse.wb.gef.graphical.GraphicalEditPart; import org.eclipse.wb.internal.core.gef.policy.menu.MenuSelectionEditPolicy; import org.eclipse.wb.internal.core.gef.policy.menu.SubmenuAwareLayoutEditPolicy; @@ -21,6 +20,9 @@ import org.eclipse.wb.internal.core.model.menu.IMenuObjectInfo; import org.eclipse.wb.internal.core.model.menu.MenuObjectInfoUtils; +import org.eclipse.gef.EditPartViewer; +import org.eclipse.gef.EditPolicy; + import java.util.Collections; import java.util.List; @@ -61,8 +63,10 @@ protected void addChildVisual(org.eclipse.gef.EditPart childPart, int index) { // The workaround is to override this method and forcibly // add figure with default index. GraphicalEditPart graphicalPart = (GraphicalEditPart) childPart; - getContentPane().add(graphicalPart.getFigure()); - graphicalPart.getFigure().setData(childPart); + EditPartViewer graphicalViewer = graphicalPart.getViewer(); + Figure graphicalFigure = graphicalPart.getFigure(); + getContentPane().add(graphicalFigure); + graphicalViewer.getVisualPartMap().put(graphicalFigure, childPart); } ///////////////////////////////////////////////////////////////////// diff --git a/org.eclipse.wb.tests/src/org/eclipse/wb/tests/draw2d/FigureTest.java b/org.eclipse.wb.tests/src/org/eclipse/wb/tests/draw2d/FigureTest.java index 3c7a9dd46..942efbff0 100644 --- a/org.eclipse.wb.tests/src/org/eclipse/wb/tests/draw2d/FigureTest.java +++ b/org.eclipse.wb.tests/src/org/eclipse/wb/tests/draw2d/FigureTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011, 2023 Google, Inc. + * Copyright (c) 2011, 2024 Google, Inc. * 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 @@ -615,7 +615,6 @@ public void test_init_properties() throws Exception { assertFalse(testFigure.isOpaque()); assertTrue(testFigure.isVisible()); assertNull(testFigure.getToolTipText()); - assertNull(testFigure.getData()); } @Test @@ -768,30 +767,6 @@ public void test_tooltip() throws Exception { assertNull(testFigure.getToolTipText()); } - @Test - public void test_data() throws Exception { - Figure testFigure = new Figure(); - // - // check user data for new Figure - assertNull(testFigure.getData()); - // - // check set user data - testFigure.setData("zzz"); - assertEquals("zzz", testFigure.getData()); - // - // check set other user data - testFigure.setData(3); - assertEquals(3, testFigure.getData()); - // - // check set user data itself - testFigure.setData(testFigure); - assertSame(testFigure, testFigure.getData()); - // - // check set 'null' user data - testFigure.setData(null); - assertNull(testFigure.getData()); - } - //////////////////////////////////////////////////////////////////////////// // // Visiting test diff --git a/org.eclipse.wb.tests/src/org/eclipse/wb/tests/gef/EditPartTest.java b/org.eclipse.wb.tests/src/org/eclipse/wb/tests/gef/EditPartTest.java index c7e6821ab..4904341cb 100644 --- a/org.eclipse.wb.tests/src/org/eclipse/wb/tests/gef/EditPartTest.java +++ b/org.eclipse.wb.tests/src/org/eclipse/wb/tests/gef/EditPartTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011, 2023 Google, Inc. + * Copyright (c) 2011, 2024 Google, Inc. * 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 @@ -18,7 +18,9 @@ import org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils; import org.eclipse.draw2d.EventListenerList; +import org.eclipse.draw2d.IFigure; import org.eclipse.gef.EditPartListener; +import org.eclipse.gef.EditPartViewer; import org.eclipse.gef.Request; import org.eclipse.gef.RequestConstants; import org.eclipse.gef.commands.Command; @@ -43,12 +45,14 @@ public class EditPartTest extends GefTestCase { @Test public void test_init() throws Exception { TestEditPart templatePart = new TestEditPart(); + IFigure templateFigure = templatePart.getFigure(); + EditPartViewer templateViewer = templatePart.getViewer(); // // check new EditPart assertNull(templatePart.getParent()); assertTrue(templatePart.getChildren().isEmpty()); assertNotNull(templatePart.getFigure()); - assertNull(templatePart.getFigure().getData()); + assertNull(templateViewer.getVisualPartMap().get(templateFigure)); assertNull(templatePart.getFigure().getParent()); assertEquals(templatePart.getFigure(), templatePart.getContentPane()); assertEquals(org.eclipse.gef.EditPart.SELECTED_NONE, templatePart.getSelected()); @@ -99,6 +103,7 @@ protected Figure createFigure() { @Test public void test_addChild() throws Exception { TestEditPart templatePart = new TestEditPart(); + IEditPartViewer templateViewer = templatePart.getViewer(); assertTrue(templatePart.getChildren().isEmpty()); /* * check add 'null' child EditPart @@ -112,7 +117,7 @@ public void test_addChild() throws Exception { /* * check 'null' parent on new EditPart */ - TestEditPart testPart1 = new TestEditPart(); + TestEditPart testPart1 = new TestEditPart(templateViewer); assertNull(testPart1.getParent()); /* * check add EditPart from wrong index (positive) @@ -146,21 +151,21 @@ public void test_addChild() throws Exception { assertEquals(1, templatePart.getChildren().size()); assertEquals(templatePart.getChildren().get(0), testPart1); assertEquals(templatePart.getFigure(), testPart1.getFigure().getParent()); - assertEquals(testPart1, testPart1.getFigure().getData()); + assertEquals(testPart1, templateViewer.getVisualPartMap().get(testPart1.getFigure())); assertFalse(templatePart.isActive()); assertFalse(testPart1.isActive()); /* * check add EditPart after activate() */ templatePart.activate(); - TestEditPart testPart2 = new TestEditPart(); + TestEditPart testPart2 = new TestEditPart(templateViewer); assertNull(testPart2.getParent()); templatePart.test_access_addChild(testPart2, -1); assertSame(templatePart, testPart2.getParent()); assertEquals(2, templatePart.getChildren().size()); assertEquals(templatePart.getChildren().get(1), testPart2); assertEquals(templatePart.getFigure(), testPart2.getFigure().getParent()); - assertEquals(testPart2, testPart2.getFigure().getData()); + assertEquals(testPart2, templateViewer.getVisualPartMap().get(testPart2.getFigure())); assertTrue(templatePart.isActive()); assertTrue(testPart1.isActive()); } @@ -168,6 +173,7 @@ public void test_addChild() throws Exception { @Test public void test_removeChild() throws Exception { TestEditPart templatePart = new TestEditPart(); + EditPartViewer templateViewer = templatePart.getViewer(); TestEditPart testPart1 = new TestEditPart(); // // check remove EditPart before activate() @@ -178,7 +184,7 @@ public void test_removeChild() throws Exception { assertTrue(testPart1.isActive()); assertNull(testPart1.getParent()); assertNull(testPart1.getFigure().getParent()); - assertNull(testPart1.getFigure().getData()); + assertNull(templateViewer.getVisualPartMap().get(testPart1.getFigure())); // // check remove EditPart after activate() templatePart.activate(); @@ -188,7 +194,7 @@ public void test_removeChild() throws Exception { assertFalse(testPart1.isActive()); assertNull(testPart1.getParent()); assertNull(testPart1.getFigure().getParent()); - assertNull(testPart1.getFigure().getData()); + assertNull(templateViewer.getVisualPartMap().get(testPart1.getFigure())); } //////////////////////////////////////////////////////////////////////////// @@ -831,7 +837,15 @@ public void removingChild(org.eclipse.gef.EditPart child, int index) { // //////////////////////////////////////////////////////////////////////////// private static class TestEditPart extends GraphicalEditPart { - private final IEditPartViewer m_viewer = new EmptyEditPartViewer(); + private final IEditPartViewer m_viewer; + + private TestEditPart() { + this(new EmptyEditPartViewer()); + } + + private TestEditPart(IEditPartViewer viewer) { + m_viewer = viewer; + } //////////////////////////////////////////////////////////////////////////// //