From b67a7b3affe8993bddfb6e9b960e37f9d5e303cc Mon Sep 17 00:00:00 2001 From: Alois Zoitl Date: Mon, 27 Mar 2023 00:18:53 +0200 Subject: [PATCH] Typefied exclusion search With this cleanup the figure and edipart search was typified. As part of this work it was identified that the TargetingTool documentation was wrong. While there it was written that getExclusion set provides editparts all its children provided a list of IFigures. Also the search expected IFigures. Furthermore AbstractTransferDropTargetListener delivered EditParts but the search required IFigures. The code was fixed here was well and by typifying the Collections it is now clearer. --- .../org/eclipse/draw2d/ExclusionSearch.java | 4 ++-- .../src/org/eclipse/draw2d/Figure.java | 4 ++-- .../src/org/eclipse/draw2d/IFigure.java | 2 +- .../gef/examples/shapes/ShapesEditor.java | 1 + .../src/org/eclipse/gef/EditPartViewer.java | 9 ++++---- .../AbstractTransferDropTargetListener.java | 21 +++++++++++-------- .../gef/tools/ConnectionEndpointTracker.java | 7 ++++--- .../gef/tools/DragEditPartsTracker.java | 6 +++--- .../org/eclipse/gef/tools/TargetingTool.java | 12 ++++++----- .../gef/ui/parts/AbstractEditPartViewer.java | 5 +++-- .../gef/ui/parts/GraphicalViewerImpl.java | 8 ++++--- .../parts/TreeViewerTransferDropListener.java | 6 ++++-- .../eclipse/gef/ui/rulers/RulerComposite.java | 6 ++++-- 13 files changed, 53 insertions(+), 38 deletions(-) diff --git a/org.eclipse.draw2d/src/org/eclipse/draw2d/ExclusionSearch.java b/org.eclipse.draw2d/src/org/eclipse/draw2d/ExclusionSearch.java index 46460d79d..d740a2e06 100644 --- a/org.eclipse.draw2d/src/org/eclipse/draw2d/ExclusionSearch.java +++ b/org.eclipse.draw2d/src/org/eclipse/draw2d/ExclusionSearch.java @@ -21,14 +21,14 @@ */ public class ExclusionSearch implements TreeSearch { - private final Collection c; + private final Collection c; /** * Constructs an Exclusion search using the given collection. * * @param collection the exclusion set */ - public ExclusionSearch(Collection collection) { + public ExclusionSearch(Collection collection) { this.c = collection; } diff --git a/org.eclipse.draw2d/src/org/eclipse/draw2d/Figure.java b/org.eclipse.draw2d/src/org/eclipse/draw2d/Figure.java index a10c953fb..88fd6f6df 100644 --- a/org.eclipse.draw2d/src/org/eclipse/draw2d/Figure.java +++ b/org.eclipse.draw2d/src/org/eclipse/draw2d/Figure.java @@ -374,7 +374,7 @@ protected IFigure findDescendantAtExcluding(int x, int y, TreeSearch search) { */ @Override public final IFigure findFigureAt(Point pt) { - return findFigureAtExcluding(pt.x, pt.y, Collections.EMPTY_LIST); + return findFigureAtExcluding(pt.x, pt.y, Collections.emptyList()); } /** @@ -406,7 +406,7 @@ public IFigure findFigureAt(int x, int y, TreeSearch search) { * @see IFigure#findFigureAtExcluding(int, int, Collection) */ @Override - public final IFigure findFigureAtExcluding(int x, int y, Collection c) { + public final IFigure findFigureAtExcluding(int x, int y, Collection c) { return findFigureAt(x, y, new ExclusionSearch(c)); } diff --git a/org.eclipse.draw2d/src/org/eclipse/draw2d/IFigure.java b/org.eclipse.draw2d/src/org/eclipse/draw2d/IFigure.java index d2efd9164..71a66b2ff 100644 --- a/org.eclipse.draw2d/src/org/eclipse/draw2d/IFigure.java +++ b/org.eclipse.draw2d/src/org/eclipse/draw2d/IFigure.java @@ -249,7 +249,7 @@ public boolean isEmpty() { * @return The IFigure at the specified location, excluding any IFigures in * collection */ - IFigure findFigureAtExcluding(int x, int y, Collection collection); + IFigure findFigureAtExcluding(int x, int y, Collection collection); /** * Returns the IFigure located at the given location which will accept mouse diff --git a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditor.java b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditor.java index 4ebe7afde..d89e8e3cc 100644 --- a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditor.java +++ b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditor.java @@ -155,6 +155,7 @@ protected void configurePaletteViewer(PaletteViewer viewer) { */ private TransferDropTargetListener createTransferDropTargetListener() { return new TemplateTransferDropTargetListener(getGraphicalViewer()) { + @Override protected CreationFactory getFactory(Object template) { return new SimpleFactory((Class) template); } diff --git a/org.eclipse.gef/src/org/eclipse/gef/EditPartViewer.java b/org.eclipse.gef/src/org/eclipse/gef/EditPartViewer.java index d6abc1d8a..4e848b804 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/EditPartViewer.java +++ b/org.eclipse.gef/src/org/eclipse/gef/EditPartViewer.java @@ -26,6 +26,7 @@ import org.eclipse.jface.util.TransferDropTargetListener; import org.eclipse.jface.viewers.ISelection; +import org.eclipse.draw2d.IFigure; import org.eclipse.draw2d.geometry.Point; /** @@ -194,10 +195,10 @@ interface Conditional { * {@link #findObjectAt(Point)}. * * @param location The mouse location - * @param exclusionSet The set of EditParts to be excluded + * @param exclusionSet The set of IFigures to be excluded * @return null or an EditPart */ - EditPart findObjectAtExcluding(Point location, Collection exclusionSet); + EditPart findObjectAtExcluding(Point location, Collection exclusionSet); /** * Returns null or the EditPart at the specified @@ -205,11 +206,11 @@ interface Conditional { * similarly to {@link #findObjectAt(Point)}. * * @param location The mouse location - * @param exclusionSet The set of EditParts to be excluded + * @param exclusionSet The set of IFigures to be excluded * @param conditional the Conditional used to evaluate a potential hit * @return null or an EditPart */ - EditPart findObjectAtExcluding(Point location, Collection exclusionSet, Conditional conditional); + EditPart findObjectAtExcluding(Point location, Collection exclusionSet, Conditional conditional); /** * Flushes all pending updates to the Viewer. diff --git a/org.eclipse.gef/src/org/eclipse/gef/dnd/AbstractTransferDropTargetListener.java b/org.eclipse.gef/src/org/eclipse/gef/dnd/AbstractTransferDropTargetListener.java index f1381a919..dc46c248a 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/dnd/AbstractTransferDropTargetListener.java +++ b/org.eclipse.gef/src/org/eclipse/gef/dnd/AbstractTransferDropTargetListener.java @@ -12,6 +12,8 @@ import java.util.Collection; import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; import org.eclipse.swt.dnd.DND; import org.eclipse.swt.dnd.DropTarget; @@ -20,11 +22,13 @@ import org.eclipse.swt.dnd.Transfer; import org.eclipse.swt.dnd.TransferData; +import org.eclipse.draw2d.IFigure; import org.eclipse.draw2d.geometry.Point; import org.eclipse.gef.AutoexposeHelper; import org.eclipse.gef.EditPart; import org.eclipse.gef.EditPartViewer; +import org.eclipse.gef.GraphicalEditPart; import org.eclipse.gef.Request; import org.eclipse.gef.commands.Command; @@ -77,12 +81,10 @@ public AbstractTransferDropTargetListener(EditPartViewer viewer, Transfer xfer) } private EditPart calculateTargetEditPart() { - EditPart ep = getViewer().findObjectAtExcluding(getDropLocation(), getExclusionSet(), - new EditPartViewer.Conditional() { - public boolean evaluate(EditPart editpart) { - return editpart.getTargetEditPart(getTargetRequest()) != null; - } - }); + List exclusionFigures = getExclusionSet().stream().filter(GraphicalEditPart.class::isInstance) + .map(ep -> ((GraphicalEditPart) ep).getFigure()).collect(Collectors.toList()); + EditPart ep = getViewer().findObjectAtExcluding(getDropLocation(), exclusionFigures, + editpart -> editpart.getTargetEditPart(getTargetRequest()) != null); if (ep != null) ep = ep.getTargetEditPart(getTargetRequest()); return ep; @@ -236,8 +238,9 @@ protected Point getDropLocation() { * * @return A Collection of EditParts to be excluded */ - protected Collection getExclusionSet() { - return Collections.EMPTY_LIST; + @SuppressWarnings("static-method") // allow children to override this method + protected Collection getExclusionSet() { + return Collections.emptySet(); } /** @@ -540,7 +543,7 @@ protected void updateAutoexposeHelper() { return; AutoexposeHelper.Search search; search = new AutoexposeHelper.Search(getDropLocation()); - getViewer().findObjectAtExcluding(getDropLocation(), Collections.EMPTY_LIST, search); + getViewer().findObjectAtExcluding(getDropLocation(), Collections.emptyList(), search); setAutoexposeHelper(search.result); } diff --git a/org.eclipse.gef/src/org/eclipse/gef/tools/ConnectionEndpointTracker.java b/org.eclipse.gef/src/org/eclipse/gef/tools/ConnectionEndpointTracker.java index 08650ea8a..fb658d6f2 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/tools/ConnectionEndpointTracker.java +++ b/org.eclipse.gef/src/org/eclipse/gef/tools/ConnectionEndpointTracker.java @@ -19,6 +19,7 @@ import org.eclipse.swt.graphics.Cursor; import org.eclipse.draw2d.Connection; +import org.eclipse.draw2d.IFigure; import org.eclipse.draw2d.PositionConstants; import org.eclipse.draw2d.geometry.Point; @@ -42,7 +43,7 @@ public class ConnectionEndpointTracker extends TargetingTool implements DragTrac protected static final int MAX_FLAG = FLAG_SOURCE_FEEBBACK; private String commandName; - private List exclusionSet; + private List exclusionSet; private ConnectionEditPart connectionEditPart; @@ -148,9 +149,9 @@ protected String getDebugName() { /** * @see org.eclipse.gef.tools.TargetingTool#getExclusionSet() */ - protected Collection getExclusionSet() { + protected Collection getExclusionSet() { if (exclusionSet == null) { - exclusionSet = new ArrayList(); + exclusionSet = new ArrayList<>(1); exclusionSet.add(getConnection()); } return exclusionSet; diff --git a/org.eclipse.gef/src/org/eclipse/gef/tools/DragEditPartsTracker.java b/org.eclipse.gef/src/org/eclipse/gef/tools/DragEditPartsTracker.java index 67f02f65f..8e0381992 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/tools/DragEditPartsTracker.java +++ b/org.eclipse.gef/src/org/eclipse/gef/tools/DragEditPartsTracker.java @@ -69,7 +69,7 @@ public class DragEditPartsTracker extends SelectEditPartTracker { private static final int FLAG_SOURCE_FEEDBACK = SelectEditPartTracker.MAX_FLAG << 1; /** Max flag */ protected static final int MAX_FLAG = FLAG_SOURCE_FEEDBACK; - private List exclusionSet; + private List exclusionSet; private PrecisionPoint sourceRelativeStartPoint; private SnapToHelper snapToHelper; private PrecisionRectangle sourceRectangle, compoundSrcRect; @@ -338,10 +338,10 @@ protected String getDebugName() { * * @see org.eclipse.gef.tools.TargetingTool#getExclusionSet() */ - protected Collection getExclusionSet() { + protected Collection getExclusionSet() { if (exclusionSet == null) { List set = getOperationSet(); - exclusionSet = new ArrayList(set.size() + 1); + exclusionSet = new ArrayList<>(set.size() + 1); for (int i = 0; i < set.size(); i++) { GraphicalEditPart editpart = (GraphicalEditPart) set.get(i); exclusionSet.add(editpart.getFigure()); diff --git a/org.eclipse.gef/src/org/eclipse/gef/tools/TargetingTool.java b/org.eclipse.gef/src/org/eclipse/gef/tools/TargetingTool.java index ef90192e8..9ea3374c9 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/tools/TargetingTool.java +++ b/org.eclipse.gef/src/org/eclipse/gef/tools/TargetingTool.java @@ -15,6 +15,7 @@ import org.eclipse.swt.widgets.Display; +import org.eclipse.draw2d.IFigure; import org.eclipse.draw2d.geometry.Point; import org.eclipse.gef.AutoexposeHelper; @@ -124,13 +125,14 @@ protected Command getCommand() { } /** - * Returns a List of objects that should be excluded as potential targets for + * Returns a List of figures that should be excluded as potential targets for * the operation. * - * @return the list of objects to be excluded as targets + * @return the list of figures to be excluded as targets */ - protected Collection getExclusionSet() { - return Collections.EMPTY_LIST; + @SuppressWarnings("static-method") // to be overridden by children + protected Collection getExclusionSet() { + return Collections.emptyList(); } /** @@ -391,7 +393,7 @@ protected void updateAutoexposeHelper() { return; AutoexposeHelper.Search search; search = new AutoexposeHelper.Search(getLocation()); - getCurrentViewer().findObjectAtExcluding(getLocation(), Collections.EMPTY_LIST, search); + getCurrentViewer().findObjectAtExcluding(getLocation(), Collections.emptyList(), search); setAutoexposeHelper(search.result); } diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/parts/AbstractEditPartViewer.java b/org.eclipse.gef/src/org/eclipse/gef/ui/parts/AbstractEditPartViewer.java index a2e69f775..b3f5b63a5 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/parts/AbstractEditPartViewer.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/parts/AbstractEditPartViewer.java @@ -41,6 +41,7 @@ import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.SelectionChangedEvent; +import org.eclipse.draw2d.IFigure; import org.eclipse.draw2d.geometry.Point; import org.eclipse.gef.AccessibleEditPart; @@ -222,13 +223,13 @@ protected void handleDispose(DisposeEvent e) { * @see EditPartViewer#findObjectAt(Point) */ public final EditPart findObjectAt(Point pt) { - return findObjectAtExcluding(pt, Collections.EMPTY_SET); + return findObjectAtExcluding(pt, Collections.emptySet()); } /** * @see EditPartViewer#findObjectAtExcluding(Point, Collection) */ - public final EditPart findObjectAtExcluding(Point pt, Collection exclude) { + public final EditPart findObjectAtExcluding(Point pt, Collection exclude) { return findObjectAtExcluding(pt, exclude, null); } diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/parts/GraphicalViewerImpl.java b/org.eclipse.gef/src/org/eclipse/gef/ui/parts/GraphicalViewerImpl.java index c62bb098c..c1f183467 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/parts/GraphicalViewerImpl.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/parts/GraphicalViewerImpl.java @@ -138,7 +138,7 @@ public Handle findHandleAt(Point p) { LayerManager layermanager = (LayerManager) getEditPartRegistry().get(LayerManager.ID); if (layermanager == null) return null; - List list = new ArrayList(3); + List list = new ArrayList<>(3); list.add(layermanager.getLayer(LayerConstants.PRIMARY_LAYER)); list.add(layermanager.getLayer(LayerConstants.CONNECTION_LAYER)); list.add(layermanager.getLayer(LayerConstants.FEEDBACK_LAYER)); @@ -152,12 +152,14 @@ public Handle findHandleAt(Point p) { * @see EditPartViewer#findObjectAtExcluding(Point, Collection, * EditPartViewer.Conditional) */ - public EditPart findObjectAtExcluding(Point pt, Collection exclude, final Conditional condition) { + @Override + public EditPart findObjectAtExcluding(Point pt, Collection exclude, final Conditional condition) { class ConditionalTreeSearch extends ExclusionSearch { - ConditionalTreeSearch(Collection coll) { + ConditionalTreeSearch(Collection coll) { super(coll); } + @Override public boolean accept(IFigure figure) { EditPart editpart = null; while (editpart == null && figure != null) { diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/parts/TreeViewerTransferDropListener.java b/org.eclipse.gef/src/org/eclipse/gef/ui/parts/TreeViewerTransferDropListener.java index 3fbb69a48..a3bbdc7d6 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/parts/TreeViewerTransferDropListener.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/parts/TreeViewerTransferDropListener.java @@ -71,13 +71,15 @@ protected String getCommandName() { return RequestConstants.REQ_ADD; } - protected Collection getExclusionSet() { + @Override + protected Collection getExclusionSet() { List selection = getViewer().getSelectedEditParts(); - List exclude = new ArrayList(selection); + List exclude = new ArrayList<>(selection); exclude.addAll(includeChildren(selection)); return exclude; } + @Override protected void handleDragOver() { if (TreeViewerTransfer.getInstance().getViewer() != getViewer()) { getCurrentEvent().detail = DND.DROP_NONE; diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/rulers/RulerComposite.java b/org.eclipse.gef/src/org/eclipse/gef/ui/rulers/RulerComposite.java index d359e403a..d22034d09 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/rulers/RulerComposite.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/rulers/RulerComposite.java @@ -12,7 +12,7 @@ import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; -import java.util.ArrayList; +import java.util.Collections; import org.eclipse.swt.SWT; import org.eclipse.swt.events.DisposeEvent; @@ -473,8 +473,9 @@ public void appendSelection(EditPart editpart) { /** * @see org.eclipse.gef.GraphicalViewer#findHandleAt(org.eclipse.draw2d.geometry.Point) */ + @Override public Handle findHandleAt(org.eclipse.draw2d.geometry.Point p) { - final GraphicalEditPart gep = (GraphicalEditPart) findObjectAtExcluding(p, new ArrayList()); + final GraphicalEditPart gep = (GraphicalEditPart) findObjectAtExcluding(p, Collections.emptyList()); if (gep == null || !(gep instanceof GuideEditPart)) return null; return new Handle() { @@ -491,6 +492,7 @@ public org.eclipse.draw2d.geometry.Point getAccessibleLocation() { /** * @see org.eclipse.gef.ui.parts.AbstractEditPartViewer#init() */ + @Override protected void init() { setContextMenu(new RulerContextMenuProvider(this)); setKeyHandler(new RulerKeyHandler(this));