Skip to content

Commit

Permalink
[Draw2D] Remove data field in Figure with GEF VisualPartMap
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ptziegler committed Jan 6, 2024
1 parent 8b07098 commit eca9c4d
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 1 addition & 15 deletions org.eclipse.wb.core/src-draw2d/org/eclipse/wb/draw2d/Figure.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -33,7 +33,6 @@
public class Figure extends org.eclipse.draw2d.Figure {
private Figure m_parent;
private List<Figure> m_children;
private Object m_data;
private String m_toolTipText;
private ICustomTooltipProvider m_customTooltipProvider;

Expand Down Expand Up @@ -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 <code>null</code> if it has not been set.
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,20 +15,25 @@
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.
*
* @author lobas_av
* @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;
}

////////////////////////////////////////////////////////////////////////////
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -179,7 +179,7 @@ public EditPart findTargetEditPart(int x,
final Collection<EditPart> 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
}

////////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,14 +13,16 @@
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;
import org.eclipse.wb.internal.core.model.menu.IMenuInfo;
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;

Expand Down Expand Up @@ -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);
}

/////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -615,7 +615,6 @@ public void test_init_properties() throws Exception {
assertFalse(testFigure.isOpaque());
assertTrue(testFigure.isVisible());
assertNull(testFigure.getToolTipText());
assertNull(testFigure.getData());
}

@Test
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand All @@ -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());
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -146,28 +151,29 @@ 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());
}

@Test
public void test_removeChild() throws Exception {
TestEditPart templatePart = new TestEditPart();
EditPartViewer templateViewer = templatePart.getViewer();
TestEditPart testPart1 = new TestEditPart();
//
// check remove EditPart before activate()
Expand All @@ -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();
Expand All @@ -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()));
}

////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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;
}

////////////////////////////////////////////////////////////////////////////
//
Expand Down

0 comments on commit eca9c4d

Please sign in to comment.