From 78c3fed3ced2c035c5a495a752523bb4b758a916 Mon Sep 17 00:00:00 2001 From: Alois Zoitl Date: Sun, 29 Sep 2024 21:53:51 +0200 Subject: [PATCH] Added getSelectedEditParts to SelectionAction #155 The new method filters the selection given to the SelectionAction on EditParts. This can reduce the effort for implementing SelectionActions and at least makes them clearer. Addresses: https://github.com/eclipse/gef-classic/issues/155 --- .../actions/IncrementDecrementAction.java | 22 ++---- org.eclipse.gef/.settings/.api_filters | 14 ++++ org.eclipse.gef/META-INF/MANIFEST.MF | 2 +- .../gef/ui/actions/AlignmentAction.java | 10 +-- .../eclipse/gef/ui/actions/DeleteAction.java | 26 +++---- .../gef/ui/actions/DirectEditAction.java | 14 ++-- .../gef/ui/actions/MatchSizeAction.java | 68 +++++++++---------- .../gef/ui/actions/PasteTemplateAction.java | 23 +++---- .../gef/ui/actions/SelectionAction.java | 21 ++++-- 9 files changed, 99 insertions(+), 101 deletions(-) diff --git a/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/actions/IncrementDecrementAction.java b/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/actions/IncrementDecrementAction.java index 5e039cf66..654d32cd2 100644 --- a/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/actions/IncrementDecrementAction.java +++ b/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/actions/IncrementDecrementAction.java @@ -60,29 +60,17 @@ protected boolean calculateEnabled() { } private boolean canPerformAction() { - if (getSelectedObjects().isEmpty()) { + List selectedEditParts = getSelectedEditParts(); + if (selectedEditParts.isEmpty()) { return false; } - List parts = getSelectedObjects(); - for (Object o : parts) { - if (!(o instanceof EditPart part)) { - return false; - } - if (!(part.getModel() instanceof LED)) { - return false; - } - } - return true; + return selectedEditParts.stream().allMatch(ep -> ep.getModel() instanceof LED); } private Command getCommand() { - List editparts = getSelectedObjects(); - CompoundCommand cc = new CompoundCommand(); + final CompoundCommand cc = new CompoundCommand(); cc.setDebugLabel("Increment/Decrement LEDs");//$NON-NLS-1$ - for (Object editpart : editparts) { - EditPart part = (EditPart) editpart; - cc.add(part.getCommand(request)); - } + getSelectedEditParts().stream().map(ep -> ep.getCommand(request)).forEach(cc::add); return cc; } diff --git a/org.eclipse.gef/.settings/.api_filters b/org.eclipse.gef/.settings/.api_filters index 5a5c6b87e..c5dd0291c 100644 --- a/org.eclipse.gef/.settings/.api_filters +++ b/org.eclipse.gef/.settings/.api_filters @@ -72,6 +72,20 @@ + + + + + + + + + + + + + + diff --git a/org.eclipse.gef/META-INF/MANIFEST.MF b/org.eclipse.gef/META-INF/MANIFEST.MF index 784c7a56a..b407a5889 100644 --- a/org.eclipse.gef/META-INF/MANIFEST.MF +++ b/org.eclipse.gef/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %Plugin.name Bundle-SymbolicName: org.eclipse.gef; singleton:=true -Bundle-Version: 3.19.100.qualifier +Bundle-Version: 3.20.0.qualifier Bundle-Activator: org.eclipse.gef.internal.InternalGEFPlugin Bundle-Vendor: %Plugin.providerName Bundle-Localization: plugin diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/actions/AlignmentAction.java b/org.eclipse.gef/src/org/eclipse/gef/ui/actions/AlignmentAction.java index 7b08cd430..08b2c6c50 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/actions/AlignmentAction.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/actions/AlignmentAction.java @@ -170,19 +170,19 @@ protected List getOperationSet(Request request) { if (operationSet != null) { return operationSet; } - List editparts = new ArrayList(getSelectedObjects()); + List editparts = new ArrayList<>(getSelectedEditParts()); if (editparts.isEmpty() || !(editparts.get(0) instanceof GraphicalEditPart)) { return Collections.emptyList(); } - Object primary = editparts.get(editparts.size() - 1); + EditPart primary = editparts.get(editparts.size() - 1); editparts = ToolUtilities.getSelectionWithoutDependants(editparts); ToolUtilities.filterEditPartsUnderstanding(editparts, request); if (editparts.size() < 2 || !editparts.contains(primary)) { return Collections.emptyList(); } - EditPart parent = ((EditPart) editparts.get(0)).getParent(); + EditPart parent = editparts.get(0).getParent(); for (int i = 1; i < editparts.size(); i++) { - EditPart part = (EditPart) editparts.get(i); + EditPart part = editparts.get(i); if (part.getParent() != parent) { return Collections.emptyList(); } @@ -242,6 +242,8 @@ protected void initUI() { setImageDescriptor(InternalImages.DESC_VERT_ALIGN_MIDDLE); setDisabledImageDescriptor(InternalImages.DESC_VERT_ALIGN_MIDDLE_DIS); break; + default: + break; } } diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/actions/DeleteAction.java b/org.eclipse.gef/src/org/eclipse/gef/ui/actions/DeleteAction.java index a3f48c658..98268a92c 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/actions/DeleteAction.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/actions/DeleteAction.java @@ -77,7 +77,7 @@ public DeleteAction(IWorkbenchPart part) { */ @Override protected boolean calculateEnabled() { - Command cmd = createDeleteCommand(getSelectedObjects()); + Command cmd = createDeleteCommand(getSelectedEditParts()); if (cmd == null) { return false; } @@ -85,32 +85,22 @@ protected boolean calculateEnabled() { } /** - * Create a command to remove the selected objects. + * Create a command to remove the selected EditParts. * - * @param objects The objects to be deleted. + * @param objects The EditParts to be deleted. * @return The command to remove the selected objects. */ @SuppressWarnings("static-method") - public Command createDeleteCommand(List objects) { + public Command createDeleteCommand(List objects) { if (objects.isEmpty()) { return null; } - if (!(objects.get(0) instanceof EditPart)) { - return null; - } - GroupRequest deleteReq = new GroupRequest(RequestConstants.REQ_DELETE); + final GroupRequest deleteReq = new GroupRequest(RequestConstants.REQ_DELETE); deleteReq.setEditParts(objects); - CompoundCommand compoundCmd = new CompoundCommand(GEFMessages.DeleteAction_ActionDeleteCommandName); - for (Object object2 : objects) { - EditPart object = (EditPart) object2; - Command cmd = object.getCommand(deleteReq); - if (cmd != null) { - compoundCmd.add(cmd); - } - } - + final CompoundCommand compoundCmd = new CompoundCommand(GEFMessages.DeleteAction_ActionDeleteCommandName); + objects.stream().map(ep -> ep.getCommand(deleteReq)).forEach(compoundCmd::add); return compoundCmd; } @@ -134,7 +124,7 @@ protected void init() { */ @Override public void run() { - execute(createDeleteCommand(getSelectedObjects())); + execute(createDeleteCommand(getSelectedEditParts())); } } diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/actions/DirectEditAction.java b/org.eclipse.gef/src/org/eclipse/gef/ui/actions/DirectEditAction.java index 65b6dc262..8b7c47346 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/actions/DirectEditAction.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/actions/DirectEditAction.java @@ -12,6 +12,8 @@ *******************************************************************************/ package org.eclipse.gef.ui.actions; +import java.util.List; + import org.eclipse.swt.widgets.Display; import org.eclipse.ui.IEditorPart; @@ -75,8 +77,9 @@ public DirectEditAction(IWorkbenchPart part) { */ @Override protected boolean calculateEnabled() { - if (getSelectedObjects().size() == 1 && (getSelectedObjects().get(0) instanceof EditPart part)) { - return part.understandsRequest(getDirectEditRequest()); + List selectedEditParts = getSelectedEditParts(); + if (selectedEditParts.size() == 1) { + return selectedEditParts.get(0).understandsRequest(getDirectEditRequest()); } return false; } @@ -96,11 +99,8 @@ protected Request getDirectEditRequest() { @Override public void run() { try { - EditPart part = (EditPart) getSelectedObjects().get(0); - part.performRequest(getDirectEditRequest()); - } catch (ClassCastException e) { - Display.getCurrent().beep(); - } catch (IndexOutOfBoundsException e) { + getSelectedEditParts().get(0).performRequest(getDirectEditRequest()); + } catch (ClassCastException | IndexOutOfBoundsException e) { Display.getCurrent().beep(); } } diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/actions/MatchSizeAction.java b/org.eclipse.gef/src/org/eclipse/gef/ui/actions/MatchSizeAction.java index 6736de1ef..3990c8c98 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/actions/MatchSizeAction.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/actions/MatchSizeAction.java @@ -56,7 +56,7 @@ public MatchSizeAction(IWorkbenchPart part) { */ @Override protected boolean calculateEnabled() { - Command cmd = createMatchSizeCommand(getSelectedObjects()); + Command cmd = createMatchSizeCommand(getSelectedEditParts()); if (cmd == null) { return false; } @@ -66,54 +66,50 @@ protected boolean calculateEnabled() { /** * Create a command to resize the selected objects. * - * @param objects The objects to be resized. + * @param editParts The objects to be resized. * @return The command to resize the selected objects. */ - private Command createMatchSizeCommand(List objects) { - if (objects.isEmpty()) { + private Command createMatchSizeCommand(List editParts) { + if (editParts.isEmpty()) { return null; } - if (!(objects.get(0) instanceof GraphicalEditPart)) { + if (!(editParts.get(0) instanceof GraphicalEditPart)) { return null; } - GraphicalEditPart primarySelection = getPrimarySelectionEditPart(getSelectedObjects()); + GraphicalEditPart primarySelection = getPrimarySelectionEditPart(editParts); if (primarySelection == null) { return null; } - GraphicalEditPart part = null; - ChangeBoundsRequest request = null; - PrecisionDimension preciseDimension = null; - PrecisionRectangle precisePartBounds = null; - Command cmd = null; - CompoundCommand command = new CompoundCommand(); - - PrecisionRectangle precisePrimaryBounds = new PrecisionRectangle( + final CompoundCommand command = new CompoundCommand(); + final PrecisionRectangle precisePrimaryBounds = new PrecisionRectangle( primarySelection.getFigure().getBounds().getCopy()); primarySelection.getFigure().translateToAbsolute(precisePrimaryBounds); - for (Object object : objects) { - part = (GraphicalEditPart) object; - if (!part.equals(primarySelection)) { - request = new ChangeBoundsRequest(RequestConstants.REQ_RESIZE); + editParts.stream().filter(GraphicalEditPart.class::isInstance).map(GraphicalEditPart.class::cast) + .forEach(part -> { + if (!part.equals(primarySelection)) { + ChangeBoundsRequest request = new ChangeBoundsRequest(RequestConstants.REQ_RESIZE); - precisePartBounds = new PrecisionRectangle(part.getFigure().getBounds().getCopy()); - part.getFigure().translateToAbsolute(precisePartBounds); + PrecisionRectangle precisePartBounds = new PrecisionRectangle( + part.getFigure().getBounds().getCopy()); + part.getFigure().translateToAbsolute(precisePartBounds); - preciseDimension = new PrecisionDimension(); - preciseDimension.setPreciseWidth(getPreciseWidthDelta(precisePartBounds, precisePrimaryBounds)); - preciseDimension.setPreciseHeight(getPreciseHeightDelta(precisePartBounds, precisePrimaryBounds)); + PrecisionDimension preciseDimension = new PrecisionDimension(); + preciseDimension.setPreciseWidth(getPreciseWidthDelta(precisePartBounds, precisePrimaryBounds)); + preciseDimension + .setPreciseHeight(getPreciseHeightDelta(precisePartBounds, precisePrimaryBounds)); - request.setSizeDelta(preciseDimension); + request.setSizeDelta(preciseDimension); - cmd = part.getCommand(request); - if (cmd != null) { - command.add(cmd); - } - } - } + Command cmd = part.getCommand(request); + if (cmd != null) { + command.add(cmd); + } + } + }); return command; } @@ -134,12 +130,10 @@ protected double getPreciseHeightDelta(PrecisionRectangle precisePartBounds, return precisePrimaryBounds.preciseHeight() - precisePartBounds.preciseHeight(); } - private static GraphicalEditPart getPrimarySelectionEditPart(List editParts) { - GraphicalEditPart part = null; - for (Object editPart : editParts) { - part = (GraphicalEditPart) editPart; - if (part.getSelected() == EditPart.SELECTED_PRIMARY) { - return part; + private static GraphicalEditPart getPrimarySelectionEditPart(List editParts) { + for (EditPart editPart : editParts) { + if (editPart.getSelected() == EditPart.SELECTED_PRIMARY && editPart instanceof GraphicalEditPart gEP) { + return gEP; } } return null; @@ -168,7 +162,7 @@ protected double getPreciseWidthDelta(PrecisionRectangle precisePartBounds, */ @Override public void run() { - execute(createMatchSizeCommand(getSelectedObjects())); + execute(createMatchSizeCommand(getSelectedEditParts())); } } diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/actions/PasteTemplateAction.java b/org.eclipse.gef/src/org/eclipse/gef/ui/actions/PasteTemplateAction.java index 1ad07f9d9..0ce497744 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/actions/PasteTemplateAction.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/actions/PasteTemplateAction.java @@ -66,19 +66,16 @@ protected boolean calculateEnabled() { */ protected Command createPasteCommand() { Command result = null; - List selection = getSelectedObjects(); - if (selection != null && selection.size() == 1) { - Object obj = selection.get(0); - if (obj instanceof GraphicalEditPart gep) { - Object template = getClipboardContents(); - if (template != null) { - CreationFactory factory = getFactory(template); - if (factory != null) { - CreateRequest request = new CreateRequest(); - request.setFactory(factory); - request.setLocation(getPasteLocation(gep)); - result = gep.getCommand(request); - } + List selection = getSelectedObjects(); + if (selection != null && selection.size() == 1 && selection.get(0) instanceof GraphicalEditPart gep) { + Object template = getClipboardContents(); + if (template != null) { + CreationFactory factory = getFactory(template); + if (factory != null) { + CreateRequest request = new CreateRequest(); + request.setFactory(factory); + request.setLocation(getPasteLocation(gep)); + result = gep.getCommand(request); } } } diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/actions/SelectionAction.java b/org.eclipse.gef/src/org/eclipse/gef/ui/actions/SelectionAction.java index 9a876d42e..214f1c4aa 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/actions/SelectionAction.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/actions/SelectionAction.java @@ -21,6 +21,8 @@ import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.ui.IWorkbenchPart; +import org.eclipse.gef.EditPart; + /** * Superclass for an action needing the current selection. */ @@ -39,7 +41,7 @@ public abstract class SelectionAction extends WorkbenchPartAction { * @param part The workbench part associated with this action * @param style the style for this action */ - public SelectionAction(IWorkbenchPart part, int style) { + protected SelectionAction(IWorkbenchPart part, int style) { super(part, style); } @@ -49,7 +51,7 @@ public SelectionAction(IWorkbenchPart part, int style) { * * @param part the workbench part */ - public SelectionAction(IWorkbenchPart part) { + protected SelectionAction(IWorkbenchPart part) { super(part); } @@ -67,13 +69,24 @@ public void dispose() { * * @return A List containing the currently selected objects. */ - protected List getSelectedObjects() { + protected List getSelectedObjects() { if (!(getSelection() instanceof IStructuredSelection)) { - return Collections.EMPTY_LIST; + return Collections.emptyList(); } return ((IStructuredSelection) getSelection()).toList(); } + /** + * Returns a List containing the currently selected + * EditParts. + * + * @return A List containing the currently selected EditParts. + * @since 3.20 + */ + protected final List getSelectedEditParts() { + return getSelectedObjects().stream().filter(EditPart.class::isInstance).map(EditPart.class::cast).toList(); + } + /** * Gets the current selection. *