Skip to content

Commit

Permalink
Use Typed Generics for Handle Generation and Management #155
Browse files Browse the repository at this point in the history
  • Loading branch information
azoitl authored and ptziegler committed Sep 30, 2024
1 parent 78c3fed commit d554c5e
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 62 deletions.
16 changes: 16 additions & 0 deletions org.eclipse.gef/.settings/.api_filters
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<component id="org.eclipse.gef" version="2">
<resource path="src/org/eclipse/gef/handles/NonResizableHandleKit.java" type="org.eclipse.gef.handles.NonResizableHandleKit">
<filter id="354463860">
<message_arguments>
<message_argument value="org.eclipse.gef.handles.NonResizableHandleKit"/>
<message_argument value="NonResizableHandleKit()"/>
</message_arguments>
</filter>
</resource>
<resource path="src/org/eclipse/gef/handles/ResizableHandleKit.java" type="org.eclipse.gef.handles.ResizableHandleKit">
<filter id="354463860">
<message_arguments>
<message_argument value="org.eclipse.gef.handles.ResizableHandleKit"/>
<message_argument value="ResizableHandleKit()"/>
</message_arguments>
</filter>
</resource>
<resource path="src/org/eclipse/gef/ui/actions/AlignmentRetargetAction.java" type="org.eclipse.gef.ui.actions.AlignmentRetargetAction">
<filter id="571473929">
<message_arguments>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private List<BendpointHandle> createHandlesForUserBendpoints() {
* @see SelectionHandlesEditPolicy#createSelectionHandles()
*/
@Override
protected List createSelectionHandles() {
protected List<? extends BendpointHandle> createSelectionHandles() {
if (isAutomaticallyBending()) {
return createHandlesForAutomaticBendpoints();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void validate() {
* @see org.eclipse.gef.editpolicies.SelectionHandlesEditPolicy#createSelectionHandles()
*/
@Override
protected List createSelectionHandles() {
protected List<? extends ConnectionEndpointHandle> createSelectionHandles() {
List<ConnectionEndpointHandle> list = new ArrayList<>();
list.add(new ConnectionEndpointHandle((ConnectionEditPart) getHost(), ConnectionLocator.SOURCE));
list.add(new ConnectionEndpointHandle((ConnectionEditPart) getHost(), ConnectionLocator.TARGET));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.eclipse.gef.Handle;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.SharedCursors;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.handles.AbstractHandle;
import org.eclipse.gef.handles.HandleBounds;
Expand Down Expand Up @@ -82,7 +81,7 @@ protected IFigure createDragSourceFeedbackFigure() {
* @see org.eclipse.gef.editpolicies.SelectionHandlesEditPolicy#createSelectionHandles()
*/
@Override
protected List createSelectionHandles() {
protected List<? extends Handle> createSelectionHandles() {
List<Handle> list = new ArrayList<>();
createMoveHandle(list);
createDragHandle(list, PositionConstants.NORTH_EAST);
Expand All @@ -102,14 +101,14 @@ protected List createSelectionHandles() {
* handle for
* @since 3.7
*/
protected void createDragHandle(List handles, int direction) {
protected void createDragHandle(List<Handle> handles, int direction) {
if (isDragAllowed()) {
// display 'resize' handles to allow dragging (drag tracker)
NonResizableHandleKit.addHandle(getHost(), handles, direction, getDragTracker(), SharedCursors.SIZEALL);
NonResizableHandleKit.addHandle(getHost(), handles, direction, getDragTracker(), Cursors.SIZEALL);
} else {
// display 'resize' handles to indicate selection only (selection
// tracker)
NonResizableHandleKit.addHandle(getHost(), handles, direction, getSelectTracker(), SharedCursors.ARROW);
NonResizableHandleKit.addHandle(getHost(), handles, direction, getSelectTracker(), Cursors.ARROW);
}
}

Expand Down Expand Up @@ -141,13 +140,13 @@ protected DragEditPartsTracker getDragTracker() {
* @param handles The list of handles to add the move handle to.
* @since 3.7
*/
protected void createMoveHandle(List handles) {
protected void createMoveHandle(List<Handle> handles) {
if (isDragAllowed()) {
// display 'move' handle to allow dragging
ResizableHandleKit.addMoveHandle(getHost(), handles, getDragTracker(), Cursors.SIZEALL);
} else {
// display 'move' handle only to indicate selection
ResizableHandleKit.addMoveHandle(getHost(), handles, getSelectTracker(), SharedCursors.ARROW);
ResizableHandleKit.addMoveHandle(getHost(), handles, getSelectTracker(), Cursors.ARROW);
}
}

Expand Down Expand Up @@ -278,6 +277,7 @@ protected Command getMoveCommand(ChangeBoundsRequest request) {
* @param req the orphan request
* @return <code>null</code> by default
*/
@SuppressWarnings("static-method")
protected Command getOrphanCommand(Request req) {
return null;
}
Expand Down Expand Up @@ -324,16 +324,16 @@ public void setDragAllowed(boolean isDragAllowed) {
* @param request the request
*/
protected void showChangeBoundsFeedback(ChangeBoundsRequest request) {
IFigure feedback = getDragSourceFeedbackFigure();
IFigure dsFeedbackFigure = getDragSourceFeedbackFigure();

PrecisionRectangle rect = new PrecisionRectangle(getInitialFeedbackBounds().getCopy());
getHostFigure().translateToAbsolute(rect);
rect.translate(request.getMoveDelta());
rect.resize(request.getSizeDelta());

feedback.translateToRelative(rect);
feedback.setBounds(rect);
feedback.validate();
dsFeedbackFigure.translateToRelative(rect);
dsFeedbackFigure.setBounds(rect);
dsFeedbackFigure.validate();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,11 @@ public class ResizableEditPolicy extends NonResizableEditPolicy {

private int resizeDirections = PositionConstants.NSEW;

/**
* Constructs a new {@link ResizableEditPolicy}.
*
* @since 3.7
*/
public ResizableEditPolicy() {
}

/**
* @see org.eclipse.gef.editpolicies.SelectionHandlesEditPolicy#createSelectionHandles()
*/
@Override
protected List createSelectionHandles() {
protected List<? extends Handle> createSelectionHandles() {
if (resizeDirections == PositionConstants.NONE) {
// non resizable, so delegate to super implementation
return super.createSelectionHandles();
Expand Down Expand Up @@ -87,7 +79,7 @@ protected List createSelectionHandles() {
* handle for
* @since 3.7
*/
protected void createResizeHandle(List handles, int direction) {
protected void createResizeHandle(List<Handle> handles, int direction) {
if ((resizeDirections & direction) == direction) {
ResizableHandleKit.addHandle(getHost(), handles, direction, getResizeTracker(direction),
Cursors.getDirectionalCursor(direction, getHostFigure().isMirrored()));
Expand Down Expand Up @@ -195,8 +187,8 @@ public void showSourceFeedback(Request request) {
public boolean understandsRequest(Request request) {
if (REQ_RESIZE.equals(request.getType())) {
// check all resize directions of the request are supported
int resizeDirections = ((ChangeBoundsRequest) request).getResizeDirection();
return (resizeDirections & getResizeDirections()) == resizeDirections;
int reqResizeDirection = ((ChangeBoundsRequest) request).getResizeDirection();
return (reqResizeDirection & getResizeDirections()) == reqResizeDirection;
}
return super.understandsRequest(request);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2010 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -14,6 +14,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import org.eclipse.core.runtime.IAdaptable;

Expand Down Expand Up @@ -42,7 +43,7 @@ public abstract class SelectionHandlesEditPolicy extends SelectionEditPolicy imp
/**
* the List of handles
*/
protected List handles;
protected List<? extends Handle> handles;

/**
* Adds the handles to the handle layer.
Expand All @@ -51,43 +52,43 @@ protected void addSelectionHandles() {
removeSelectionHandles();
IFigure layer = getLayer(LayerConstants.HANDLE_LAYER);
handles = createSelectionHandles();
for (Object handle : handles) {
layer.add((IFigure) handle);
}
getHandleFigures().forEach(layer::add);
}

/**
* Subclasses must implement to provide the list of handles.
*
* @return List of handles; cannot be <code>null</code>
*/
protected abstract List createSelectionHandles();
protected abstract List<? extends Handle> createSelectionHandles();

/**
* @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class)
*/
@Override
public <T> T getAdapter(final Class<T> key) {
if (key == AccessibleHandleProvider.class) {
return key.cast(new AccessibleHandleProvider() {
@Override
public List<Point> getAccessibleHandleLocations() {
List<Point> result = new ArrayList<>();
if (handles != null) {
for (Object handle : handles) {
Point p = ((Handle) handle).getAccessibleLocation();
if (p != null) {
result.add(p);
}
return key.cast((AccessibleHandleProvider) () -> {
List<Point> result = new ArrayList<>();
if (handles != null) {
for (Handle handle : handles) {
Point p = handle.getAccessibleLocation();
if (p != null) {
result.add(p);
}
}
return result;
}
return result;
});
}
return null;
}

private Stream<IFigure> getHandleFigures() {
return (handles != null) ? handles.stream().filter(IFigure.class::isInstance).map(IFigure.class::cast)
: Stream.empty();
}

/**
* Implemented to remove the handles.
*
Expand All @@ -106,9 +107,7 @@ protected void removeSelectionHandles() {
return;
}
IFigure layer = getLayer(LayerConstants.HANDLE_LAYER);
for (Object handle : handles) {
layer.remove((IFigure) handle);
}
getHandleFigures().forEach(layer::remove);
handles = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

import org.eclipse.swt.graphics.Cursor;

import org.eclipse.draw2d.Cursors;
import org.eclipse.draw2d.PositionConstants;

import org.eclipse.gef.DragTracker;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Handle;
import org.eclipse.gef.SharedCursors;
import org.eclipse.gef.tools.DragEditPartsTracker;

/**
Expand All @@ -39,7 +39,8 @@ public class NonResizableHandleKit {
* @param tracker the handles' DragTracker
* @param cursor the handles' Cursor
*/
public static void addCornerHandles(GraphicalEditPart part, List handles, DragTracker tracker, Cursor cursor) {
public static void addCornerHandles(GraphicalEditPart part, List<Handle> handles, DragTracker tracker,
Cursor cursor) {
handles.add(createHandle(part, PositionConstants.SOUTH_EAST, tracker, cursor));
handles.add(createHandle(part, PositionConstants.SOUTH_WEST, tracker, cursor));
handles.add(createHandle(part, PositionConstants.NORTH_WEST, tracker, cursor));
Expand All @@ -52,7 +53,7 @@ public static void addCornerHandles(GraphicalEditPart part, List handles, DragTr
* @param part the handles' GraphicalEditPart
* @param handles the List to add the four corner handles to
*/
public static void addCornerHandles(GraphicalEditPart part, List handles) {
public static void addCornerHandles(GraphicalEditPart part, List<Handle> handles) {
handles.add(createHandle(part, PositionConstants.SOUTH_EAST));
handles.add(createHandle(part, PositionConstants.SOUTH_WEST));
handles.add(createHandle(part, PositionConstants.NORTH_WEST));
Expand All @@ -67,7 +68,7 @@ public static void addCornerHandles(GraphicalEditPart part, List handles) {
* @param direction the integer constant from PositionConstants that refers to
* the handle direction
*/
public static void addHandle(GraphicalEditPart part, List handles, int direction) {
public static void addHandle(GraphicalEditPart part, List<Handle> handles, int direction) {
handles.add(createHandle(part, direction));
}

Expand All @@ -81,7 +82,7 @@ public static void addHandle(GraphicalEditPart part, List handles, int direction
* the handle direction
* @param cursor the Cursor to use when hovering over this handle
*/
public static void addHandle(GraphicalEditPart part, List handles, int direction, DragTracker tracker,
public static void addHandle(GraphicalEditPart part, List<Handle> handles, int direction, DragTracker tracker,
Cursor cursor) {
handles.add(createHandle(part, direction, tracker, cursor));
}
Expand All @@ -93,7 +94,8 @@ public static void addHandle(GraphicalEditPart part, List handles, int direction
* @param handles the List to add the handles to
* @deprecated
*/
public static void addHandles(GraphicalEditPart part, List handles) {
@Deprecated
public static void addHandles(GraphicalEditPart part, List<Handle> handles) {
addMoveHandle(part, handles);
addCornerHandles(part, handles);
}
Expand All @@ -107,7 +109,8 @@ public static void addHandles(GraphicalEditPart part, List handles) {
* @param cursor the handles' Cursor
* @deprecated
*/
public static void addHandles(GraphicalEditPart part, List handles, DragTracker tracker, Cursor cursor) {
@Deprecated
public static void addHandles(GraphicalEditPart part, List<Handle> handles, DragTracker tracker, Cursor cursor) {
addMoveHandle(part, handles, tracker, cursor);
addCornerHandles(part, handles, tracker, cursor);
}
Expand All @@ -118,7 +121,7 @@ public static void addHandles(GraphicalEditPart part, List handles, DragTracker
* @param f the handles' GraphicalEditPart
* @param handles the List to add the handles to
*/
public static void addMoveHandle(GraphicalEditPart f, List handles) {
public static void addMoveHandle(GraphicalEditPart f, List<Handle> handles) {
handles.add(moveHandle(f));
}

Expand All @@ -130,13 +133,13 @@ public static void addMoveHandle(GraphicalEditPart f, List handles) {
* @param handles the List to add the handles to
* @param cursor the Cursor to use when hovering over this handle
*/
public static void addMoveHandle(GraphicalEditPart f, List handles, DragTracker tracker, Cursor cursor) {
public static void addMoveHandle(GraphicalEditPart f, List<Handle> handles, DragTracker tracker, Cursor cursor) {
handles.add(moveHandle(f, tracker, cursor));
}

static Handle createHandle(GraphicalEditPart owner, int direction) {
ResizeHandle handle = new ResizeHandle(owner, direction);
handle.setCursor(SharedCursors.SIZEALL);
handle.setCursor(Cursors.SIZEALL);
handle.setDragTracker(new DragEditPartsTracker(owner));
return handle;
}
Expand Down Expand Up @@ -173,4 +176,8 @@ public static Handle moveHandle(GraphicalEditPart owner, DragTracker tracker, Cu
return moveHandle;
}

private NonResizableHandleKit() {
throw new UnsupportedOperationException("Helper class should not be instantiated!"); //$NON-NLS-1$
}

}
Loading

0 comments on commit d554c5e

Please sign in to comment.