From 5eefb4326484c666fb21174541fa4a2952254e8b Mon Sep 17 00:00:00 2001 From: Denis Knoepfle Date: Sun, 27 Jul 2014 00:03:10 +0300 Subject: [PATCH] Refactor delete in context menu to show individual labels Implement additional feature requested in #41. Change-Id: Ibf4e102c89fa5e20bbc097b6b500124aa59d352d Signed-off-by: Denis Knoepfle --- .../eclipse/ui/handlers/DeleteHandler.java | 89 +++++++++++++++---- .../eclipse/ui/navigator/IDeletable.java | 8 ++ .../ui/navigator/SpotterProjectParent.java | 6 ++ .../ui/navigator/SpotterProjectRunResult.java | 6 ++ 4 files changed, 92 insertions(+), 17 deletions(-) diff --git a/org.spotter.eclipse.ui/src/org/spotter/eclipse/ui/handlers/DeleteHandler.java b/org.spotter.eclipse.ui/src/org/spotter/eclipse/ui/handlers/DeleteHandler.java index b795857..1a961df 100644 --- a/org.spotter.eclipse.ui/src/org/spotter/eclipse/ui/handlers/DeleteHandler.java +++ b/org.spotter.eclipse.ui/src/org/spotter/eclipse/ui/handlers/DeleteHandler.java @@ -15,13 +15,18 @@ */ package org.spotter.eclipse.ui.handlers; +import java.util.ArrayList; import java.util.Iterator; +import java.util.List; +import java.util.Map; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.TreeViewer; +import org.eclipse.ui.commands.IElementUpdater; +import org.eclipse.ui.menus.UIElement; import org.spotter.eclipse.ui.Activator; import org.spotter.eclipse.ui.navigator.IDeletable; import org.spotter.eclipse.ui.util.SpotterUtils; @@ -33,23 +38,25 @@ * @author Denis Knoepfle * */ -public class DeleteHandler extends AbstractHandler { +public class DeleteHandler extends AbstractHandler implements IElementUpdater { /** * The id of the corresponding delete command. */ public static final String DELETE_COMMAND_ID = "org.spotter.eclipse.ui.commands.delete"; + /** + * The default label used for the command in the popup menu + */ + private static final String DEFAULT_LABEL = "Delete"; + @Override public Object execute(ExecutionEvent event) throws ExecutionException { - Activator activator = Activator.getDefault(); - TreeViewer viewer = activator.getNavigatorViewer(); - if (viewer == null) { + Iterator iter = getSelectionIterator(); + if (iter == null) { return null; } - IStructuredSelection selection = (IStructuredSelection) viewer.getSelection(); - Iterator iter = selection.iterator(); while (iter.hasNext()) { SpotterUtils.deleteNavigatorElement(iter.next()); } @@ -62,25 +69,73 @@ public Object execute(ExecutionEvent event) throws ExecutionException { */ @Override public boolean isEnabled() { - Activator activator = Activator.getDefault(); - TreeViewer viewer = activator.getNavigatorViewer(); - if (viewer == null) { - return false; + return !getSelectedDeletables().isEmpty(); + } + + @SuppressWarnings("rawtypes") + @Override + public void updateElement(UIElement element, Map parameters) { + List deletables = getSelectedDeletables(); + + String label = getLabelForDeletables(deletables); + element.setText(label); + } + + private String getLabelForDeletables(List deletables) { + IDeletable deletable = deletables.isEmpty() ? null : deletables.get(0); + if (deletable == null) { + return DEFAULT_LABEL; } - IStructuredSelection selection = (IStructuredSelection) viewer.getSelection(); - if (selection.isEmpty()) { - return false; + String pluralLetter = deletables.size() > 1 ? "s" : ""; + String label = "Delete " + deletable.getElementTypeName() + pluralLetter; + + return label; + } + + private List getSelectedDeletables() { + List deletables = new ArrayList<>(); + + Iterator iter = getSelectionIterator(); + if (iter == null) { + return deletables; } - Iterator iter = selection.iterator(); + + Class firstDeletableClazz = null; while (iter.hasNext()) { - if (!(iter.next() instanceof IDeletable)) { - return false; + Object selectedElement = iter.next(); + if (selectedElement instanceof IDeletable) { + Class clazz = selectedElement.getClass(); + if (firstDeletableClazz == null) { + firstDeletableClazz = clazz; + deletables.add((IDeletable) selectedElement); + } else if (!firstDeletableClazz.equals(clazz)) { + // only allow same types in one delete + deletables.clear(); + return deletables; + } else { + // just add the element so it can be counted afterwards + deletables.add((IDeletable) selectedElement); + } + } else { + // if any element not deletable return empty list + deletables.clear(); + return deletables; } } + return deletables; + } - return true; + private Iterator getSelectionIterator() { + Activator activator = Activator.getDefault(); + TreeViewer viewer = activator.getNavigatorViewer(); + if (viewer == null) { + return null; + } + + IStructuredSelection selection = (IStructuredSelection) viewer.getSelection(); + return selection.isEmpty() ? null : selection.iterator(); } } diff --git a/org.spotter.eclipse.ui/src/org/spotter/eclipse/ui/navigator/IDeletable.java b/org.spotter.eclipse.ui/src/org/spotter/eclipse/ui/navigator/IDeletable.java index 5eff60e..073e497 100644 --- a/org.spotter.eclipse.ui/src/org/spotter/eclipse/ui/navigator/IDeletable.java +++ b/org.spotter.eclipse.ui/src/org/spotter/eclipse/ui/navigator/IDeletable.java @@ -29,4 +29,12 @@ public interface IDeletable { */ public void delete(); + /** + * Returns the name of this element type that should be used within the + * label for the delete command. + * + * @return The name of this element type + */ + public String getElementTypeName(); + } diff --git a/org.spotter.eclipse.ui/src/org/spotter/eclipse/ui/navigator/SpotterProjectParent.java b/org.spotter.eclipse.ui/src/org/spotter/eclipse/ui/navigator/SpotterProjectParent.java index 9916c01..d7833e1 100644 --- a/org.spotter.eclipse.ui/src/org/spotter/eclipse/ui/navigator/SpotterProjectParent.java +++ b/org.spotter.eclipse.ui/src/org/spotter/eclipse/ui/navigator/SpotterProjectParent.java @@ -53,6 +53,7 @@ public class SpotterProjectParent implements ISpotterProjectElement, IDeletable, public static final String IMAGE_PATH = "icons/project.gif"; //$NON-NLS-1$ private static final Logger LOGGER = LoggerFactory.getLogger(SpotterProjectParent.class); + private static final String ELEMENT_TYPE_NAME = "Project"; private static final String DUPLICATE_DLG_TITLE = "Duplicate Project"; private static final String DELETE_DLG_TITLE = "Delete Resources"; private static final String MSG_SINGLE = "Are you sure you want to remove project '%s' from the workspace?\n\nWarning: Contents will be deleted from disk!"; @@ -213,6 +214,11 @@ public void delete() { } } + @Override + public String getElementTypeName() { + return ELEMENT_TYPE_NAME; + } + private String createErrorMessage(List projects, List detailErrorMessages) { StringBuilder sb = new StringBuilder(); sb.append("Error while deleting project(s): "); diff --git a/org.spotter.eclipse.ui/src/org/spotter/eclipse/ui/navigator/SpotterProjectRunResult.java b/org.spotter.eclipse.ui/src/org/spotter/eclipse/ui/navigator/SpotterProjectRunResult.java index cb9c94b..6edc570 100644 --- a/org.spotter.eclipse.ui/src/org/spotter/eclipse/ui/navigator/SpotterProjectRunResult.java +++ b/org.spotter.eclipse.ui/src/org/spotter/eclipse/ui/navigator/SpotterProjectRunResult.java @@ -39,6 +39,7 @@ public class SpotterProjectRunResult implements IOpenableProjectElement, IDeleta public static final String IMAGE_PATH = "icons/results.gif"; //$NON-NLS-1$ private static final String DELETE_DLG_TITLE = "Delete Resources"; + private static final String ELEMENT_TYPE_NAME = "Result Item"; private final ISpotterProjectElement parent; private final IFolder resultFolder; @@ -150,4 +151,9 @@ public void delete() { } } + @Override + public String getElementTypeName() { + return ELEMENT_TYPE_NAME; + } + }