Skip to content

Commit

Permalink
Added getSelectedEditParts to SelectionAction #155
Browse files Browse the repository at this point in the history
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: #155
  • Loading branch information
azoitl authored and ptziegler committed Sep 30, 2024
1 parent 84c468d commit 78c3fed
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,17 @@ protected boolean calculateEnabled() {
}

private boolean canPerformAction() {
if (getSelectedObjects().isEmpty()) {
List<EditPart> 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;
}

Expand Down
14 changes: 14 additions & 0 deletions org.eclipse.gef/.settings/.api_filters
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@
</message_arguments>
</filter>
</resource>
<resource path="src/org/eclipse/gef/ui/actions/SelectionAction.java" type="org.eclipse.gef.ui.actions.SelectionAction">
<filter id="354463860">
<message_arguments>
<message_argument value="org.eclipse.gef.ui.actions.SelectionAction"/>
<message_argument value="SelectionAction(IWorkbenchPart)"/>
</message_arguments>
</filter>
<filter id="354463860">
<message_arguments>
<message_argument value="org.eclipse.gef.ui.actions.SelectionAction"/>
<message_argument value="SelectionAction(IWorkbenchPart, int)"/>
</message_arguments>
</filter>
</resource>
<resource path="src/org/eclipse/gef/ui/actions/UndoRetargetAction.java" type="org.eclipse.gef.ui.actions.UndoRetargetAction">
<filter id="571473929">
<message_arguments>
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.gef/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,19 @@ protected List<? extends EditPart> getOperationSet(Request request) {
if (operationSet != null) {
return operationSet;
}
List editparts = new ArrayList(getSelectedObjects());
List<? extends EditPart> 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();
}
Expand Down Expand Up @@ -242,6 +242,8 @@ protected void initUI() {
setImageDescriptor(InternalImages.DESC_VERT_ALIGN_MIDDLE);
setDisabledImageDescriptor(InternalImages.DESC_VERT_ALIGN_MIDDLE_DIS);
break;
default:
break;
}
}

Expand Down
26 changes: 8 additions & 18 deletions org.eclipse.gef/src/org/eclipse/gef/ui/actions/DeleteAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,40 +77,30 @@ public DeleteAction(IWorkbenchPart part) {
*/
@Override
protected boolean calculateEnabled() {
Command cmd = createDeleteCommand(getSelectedObjects());
Command cmd = createDeleteCommand(getSelectedEditParts());
if (cmd == null) {
return false;
}
return cmd.canExecute();
}

/**
* 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<EditPart> 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;
}

Expand All @@ -134,7 +124,7 @@ protected void init() {
*/
@Override
public void run() {
execute(createDeleteCommand(getSelectedObjects()));
execute(createDeleteCommand(getSelectedEditParts()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<EditPart> selectedEditParts = getSelectedEditParts();
if (selectedEditParts.size() == 1) {
return selectedEditParts.get(0).understandsRequest(getDirectEditRequest());
}
return false;
}
Expand All @@ -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();
}
}
Expand Down
68 changes: 31 additions & 37 deletions org.eclipse.gef/src/org/eclipse/gef/ui/actions/MatchSizeAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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<EditPart> 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;
}
Expand All @@ -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<EditPart> editParts) {
for (EditPart editPart : editParts) {
if (editPart.getSelected() == EditPart.SELECTED_PRIMARY && editPart instanceof GraphicalEditPart gEP) {
return gEP;
}
}
return null;
Expand Down Expand Up @@ -168,7 +162,7 @@ protected double getPreciseWidthDelta(PrecisionRectangle precisePartBounds,
*/
@Override
public void run() {
execute(createMatchSizeCommand(getSelectedObjects()));
execute(createMatchSizeCommand(getSelectedEditParts()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> 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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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);
}

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

Expand All @@ -67,13 +69,24 @@ public void dispose() {
*
* @return A List containing the currently selected objects.
*/
protected List getSelectedObjects() {
protected List<Object> getSelectedObjects() {
if (!(getSelection() instanceof IStructuredSelection)) {
return Collections.EMPTY_LIST;
return Collections.emptyList();
}
return ((IStructuredSelection) getSelection()).toList();
}

/**
* Returns a <code>List<EditPart></code> containing the currently selected
* EditParts.
*
* @return A List containing the currently selected EditParts.
* @since 3.20
*/
protected final List<EditPart> getSelectedEditParts() {
return getSelectedObjects().stream().filter(EditPart.class::isInstance).map(EditPart.class::cast).toList();
}

/**
* Gets the current selection.
*
Expand Down

0 comments on commit 78c3fed

Please sign in to comment.