Skip to content

Commit

Permalink
[Modernize Code] Clean generic usage in gef actions
Browse files Browse the repository at this point in the history
  • Loading branch information
azoitl authored and ptziegler committed Jan 20, 2024
1 parent e9839e7 commit 2ca0069
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 76 deletions.
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 Down Expand Up @@ -34,8 +34,8 @@ public abstract class ActionBarContributor extends EditorActionBarContributor {
* handlers. We need to hold on to these so that we can remove them as
* PartListeners in dispose().
*/
private List retargetActions = new ArrayList();
private List globalActionKeys = new ArrayList();
private List<RetargetAction> retargetActions = new ArrayList<>();
private final List<String> globalActionKeys = new ArrayList<>();

/**
* Adds the given action to the action registry.
Expand Down Expand Up @@ -105,10 +105,9 @@ protected void addRetargetAction(RetargetAction action) {
*/
@Override
public void dispose() {
for (Object retargetAction : retargetActions) {
RetargetAction action = (RetargetAction) retargetAction;
getPage().removePartListener(action);
action.dispose();
for (RetargetAction retargetAction : retargetActions) {
getPage().removePartListener(retargetAction);
retargetAction.dispose();
}
registry.dispose();
retargetActions = null;
Expand Down Expand Up @@ -149,11 +148,10 @@ public void init(IActionBars bars) {
*/
@Override
public void setActiveEditor(IEditorPart editor) {
ActionRegistry registry = editor.getAdapter(ActionRegistry.class);
ActionRegistry editorRegistry = editor.getAdapter(ActionRegistry.class);
IActionBars bars = getActionBars();
for (Object globalActionKey : globalActionKeys) {
String id = (String) globalActionKey;
IAction handler = registry != null ? registry.getAction(id) : null;
for (String id : globalActionKeys) {
IAction handler = editorRegistry != null ? editorRegistry.getAction(id) : null;
bars.setGlobalActionHandler(id, handler);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2023 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 Down Expand Up @@ -56,7 +56,7 @@ public IAction getAction(Object key) {
*
* @return an iterator over all actions
*/
public Iterator getActions() {
public Iterator<IAction> getActions() {
return map.values().iterator();
}

Expand Down
26 changes: 12 additions & 14 deletions org.eclipse.gef/src/org/eclipse/gef/ui/actions/AlignmentAction.java
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 Down Expand Up @@ -70,15 +70,16 @@ public final class AlignmentAction extends SelectionAction {
* Indicates that the top edges should be aligned.
*/
public static final String ID_ALIGN_TOP = GEFActionConstants.ALIGN_TOP;
private int alignment;
private final int alignment;

private List operationSet;
private List<EditPart> operationSet;

/**
* @deprecated use AlignmentAction(IWorkbenchPart, int align)
* @param editor the editor
* @param align the alignment ID
*/
@Deprecated
public AlignmentAction(IEditorPart editor, int align) {
this((IWorkbenchPart) editor, align);
}
Expand Down Expand Up @@ -112,7 +113,7 @@ public AlignmentAction(IWorkbenchPart part, int align) {
* @return the alignment rectangle
*/
protected Rectangle calculateAlignmentRectangle(Request request) {
List editparts = getOperationSet(request);
List<? extends EditPart> editparts = getOperationSet(request);
if (editparts == null || editparts.isEmpty()) {
return null;
}
Expand All @@ -139,17 +140,14 @@ private Command createAlignmentCommand() {
AlignmentRequest request = new AlignmentRequest(RequestConstants.REQ_ALIGN);
request.setAlignmentRectangle(calculateAlignmentRectangle(request));
request.setAlignment(alignment);
List editparts = getOperationSet(request);
List<? extends EditPart> editparts = getOperationSet(request);
if (editparts.size() < 2) {
return null;
}

CompoundCommand command = new CompoundCommand();
command.setDebugLabel(getText());
for (Object editpart2 : editparts) {
EditPart editpart = (EditPart) editpart2;
command.add(editpart.getCommand(request));
}
editparts.forEach(ep -> command.add(ep.getCommand(request)));
return command;
}

Expand All @@ -158,7 +156,7 @@ private Command createAlignmentCommand() {
*/
@Override
public void dispose() {
operationSet = Collections.EMPTY_LIST;
operationSet = Collections.emptyList();
super.dispose();
}

Expand All @@ -168,25 +166,25 @@ public void dispose() {
* @param request the alignment request
* @return the list of parts which will be aligned
*/
protected List getOperationSet(Request request) {
protected List<? extends EditPart> getOperationSet(Request request) {
if (operationSet != null) {
return operationSet;
}
List editparts = new ArrayList(getSelectedObjects());
if (editparts.isEmpty() || !(editparts.get(0) instanceof GraphicalEditPart)) {
return Collections.EMPTY_LIST;
return Collections.emptyList();
}
Object primary = editparts.get(editparts.size() - 1);
editparts = ToolUtilities.getSelectionWithoutDependants(editparts);
ToolUtilities.filterEditPartsUnderstanding(editparts, request);
if (editparts.size() < 2 || !editparts.contains(primary)) {
return Collections.EMPTY_LIST;
return Collections.emptyList();
}
EditPart parent = ((EditPart) editparts.get(0)).getParent();
for (int i = 1; i < editparts.size(); i++) {
EditPart part = (EditPart) editparts.get(i);
if (part.getParent() != parent) {
return Collections.EMPTY_LIST;
return Collections.emptyList();
}
}
return editparts;
Expand Down
9 changes: 6 additions & 3 deletions org.eclipse.gef/src/org/eclipse/gef/ui/actions/Clipboard.java
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 Down Expand Up @@ -31,8 +31,8 @@ public class Clipboard {

private static Clipboard defaultClipboard = new Clipboard();
private static final SimpleObjectTransfer TRANSFER = new SimpleObjectTransfer() {
private final String TYPE_NAME = "org.eclipse.gef.clipboard.transfer"; //$NON-NLS-1$
private final int TYPE_ID = registerType(TYPE_NAME);
private static final String TYPE_NAME = "org.eclipse.gef.clipboard.transfer"; //$NON-NLS-1$
private static final int TYPE_ID = registerType(TYPE_NAME);

@Override
protected int[] getTypeIds() {
Expand Down Expand Up @@ -62,6 +62,7 @@ public static Clipboard getDefault() {
* created. Use {@link #getDefault()}. This method will be removed
* in future releases.
*/
@Deprecated
public Clipboard() {
}

Expand All @@ -70,6 +71,7 @@ public Clipboard() {
*
* @return contents of the clipboard
*/
@SuppressWarnings("static-method")
public Object getContents() {
org.eclipse.swt.dnd.Clipboard cb = new org.eclipse.swt.dnd.Clipboard(null);
Object contents = cb.getContents(TRANSFER);
Expand All @@ -84,6 +86,7 @@ public Object getContents() {
*
* @param contents the new contents
*/
@SuppressWarnings("static-method")
public void setContents(Object contents) {
org.eclipse.swt.dnd.Clipboard cb = new org.eclipse.swt.dnd.Clipboard(null);
cb.setContents(new Object[] { contents }, new Transfer[] { TRANSFER });
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 @@ -12,7 +12,6 @@
*******************************************************************************/
package org.eclipse.gef.ui.actions;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
Expand Down Expand Up @@ -82,19 +81,14 @@ public void run() {
*/
@Override
public void selectionChanged(SelectionChangedEvent event) {
ISelection s = event.getSelection();
if (!(s instanceof IStructuredSelection selection)) {
return;
}
template = null;
if (selection != null && selection.size() == 1) {
if (event.getSelection() instanceof IStructuredSelection selection && selection.size() == 1) {
template = null;
Object obj = selection.getFirstElement();
if (obj instanceof EditPart) {
Object model = ((EditPart) obj).getModel();
if (model instanceof CombinedTemplateCreationEntry) {
template = ((CombinedTemplateCreationEntry) model).getTemplate();
} else if (model instanceof PaletteTemplateEntry) {
template = ((PaletteTemplateEntry) model).getTemplate();
if (obj instanceof EditPart ep) {
if (ep.getModel() instanceof CombinedTemplateCreationEntry combinedCreationEntry) {
template = combinedCreationEntry.getTemplate();
} else if (ep.getModel() instanceof PaletteTemplateEntry paletteEntry) {
template = paletteEntry.getTemplate();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@
public class DeleteAction extends SelectionAction {

/** @deprecated Use ActionFactory.DELETE.getId() instead. */
@Deprecated
public static final String ID = ActionFactory.DELETE.getId();

/**
* @deprecated use DeleteAction(IWorkbenchPart part)
* @param editor The editor this action will be associated with.
*/
@Deprecated
public DeleteAction(IEditorPart editor) {
this((IWorkbenchPart) editor);
}
Expand All @@ -50,6 +52,7 @@ public DeleteAction(IEditorPart editor) {
* @param editor The editor this action will be associated with.
* @param label The label to be displayed for this action.
*/
@Deprecated
public DeleteAction(IEditorPart editor, String label) {
this((IWorkbenchPart) editor);
setText(label);
Expand Down Expand Up @@ -87,6 +90,7 @@ protected boolean calculateEnabled() {
* @param objects The objects to be deleted.
* @return The command to remove the selected objects.
*/
@SuppressWarnings("static-method")
public Command createDeleteCommand(List objects) {
if (objects.isEmpty()) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
public class DirectEditAction extends SelectionAction {

/** @deprecated */
@Deprecated
public static final String ID = GEFActionConstants.DIRECT_EDIT;

private Request directEditRequest = new Request(RequestConstants.REQ_DIRECT_EDIT);
Expand Down Expand Up @@ -74,8 +75,7 @@ public DirectEditAction(IWorkbenchPart part) {
*/
@Override
protected boolean calculateEnabled() {
if (getSelectedObjects().size() == 1 && (getSelectedObjects().get(0) instanceof EditPart)) {
EditPart part = (EditPart) getSelectedObjects().get(0);
if (getSelectedObjects().size() == 1 && (getSelectedObjects().get(0) instanceof EditPart part)) {
return part.understandsRequest(getDirectEditRequest());
}
return false;
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 Down Expand Up @@ -128,12 +128,13 @@ private Command createMatchSizeCommand(List objects) {
* EditPart's Figure
* @return the delta between the two heights to be used in the Request.
*/
@SuppressWarnings("static-method")
protected double getPreciseHeightDelta(PrecisionRectangle precisePartBounds,
PrecisionRectangle precisePrimaryBounds) {
return precisePrimaryBounds.preciseHeight() - precisePartBounds.preciseHeight();
}

private GraphicalEditPart getPrimarySelectionEditPart(List editParts) {
private static GraphicalEditPart getPrimarySelectionEditPart(List editParts) {
GraphicalEditPart part = null;
for (Object editPart : editParts) {
part = (GraphicalEditPart) editPart;
Expand All @@ -154,6 +155,7 @@ private GraphicalEditPart getPrimarySelectionEditPart(List editParts) {
* EditPart's Figure
* @return the delta between the two widths to be used in the Request.
*/
@SuppressWarnings("static-method")
protected double getPreciseWidthDelta(PrecisionRectangle precisePartBounds,
PrecisionRectangle precisePrimaryBounds) {
return precisePrimaryBounds.preciseWidth() - precisePartBounds.preciseWidth();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ protected Command createPasteCommand() {
*
* @return the clipboard's contents
*/
@SuppressWarnings("static-method")
protected Object getClipboardContents() {
return Clipboard.getDefault().getContents();
}
Expand All @@ -106,11 +107,9 @@ protected Object getClipboardContents() {
* @param template the template Object; it will never be <code>null</code>
* @return a Factory
*/
@SuppressWarnings("static-method")
protected CreationFactory getFactory(Object template) {
if (template instanceof CreationFactory) {
return (CreationFactory) template;
}
return null;
return (template instanceof CreationFactory cf) ? cf : null;
}

/**
Expand Down
12 changes: 5 additions & 7 deletions org.eclipse.gef/src/org/eclipse/gef/ui/actions/RedoAction.java
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, 2023 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 Down Expand Up @@ -61,8 +61,8 @@ protected boolean calculateEnabled() {
@Override
protected void init() {
super.init();
setToolTipText(MessageFormat.format(GEFMessages.RedoAction_Tooltip, new Object[] { "" }).trim()); //$NON-NLS-1$
setText(MessageFormat.format(GEFMessages.RedoAction_Label, new Object[] { "" }).trim() //$NON-NLS-1$
setToolTipText(MessageFormat.format(GEFMessages.RedoAction_Tooltip, "").trim()); //$NON-NLS-1$
setText(MessageFormat.format(GEFMessages.RedoAction_Label, "").trim() //$NON-NLS-1$
);
setId(ActionFactory.REDO.getId());

Expand All @@ -77,10 +77,8 @@ protected void init() {
@Override
protected void refresh() {
Command redoCmd = getCommandStack().getRedoCommand();
setToolTipText(MessageFormat
.format(GEFMessages.RedoAction_Tooltip, new Object[] { getLabelForCommand(redoCmd) }).trim());
setText(MessageFormat.format(GEFMessages.RedoAction_Label, new Object[] { getLabelForCommand(redoCmd) })
.trim());
setToolTipText(MessageFormat.format(GEFMessages.RedoAction_Tooltip, getLabelForCommand(redoCmd)).trim());
setText(MessageFormat.format(GEFMessages.RedoAction_Label, getLabelForCommand(redoCmd)).trim());
super.refresh();
}

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, 2023 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 Down Expand Up @@ -30,8 +30,7 @@ public class RedoRetargetAction extends LabelRetargetAction {
* Constructs a new RedoRetargetAction with the default ID, label and image.
*/
public RedoRetargetAction() {
super(ActionFactory.REDO.getId(),
MessageFormat.format(GEFMessages.RedoAction_Label, new Object[] { "" }).trim()); //$NON-NLS-1$
super(ActionFactory.REDO.getId(), MessageFormat.format(GEFMessages.RedoAction_Label, "").trim()); //$NON-NLS-1$
ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_REDO));
setDisabledImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_REDO_DISABLED));
Expand Down

0 comments on commit 2ca0069

Please sign in to comment.