Skip to content

Commit

Permalink
Refactor delete in context menu to show individual labels
Browse files Browse the repository at this point in the history
Implement additional feature requested in sopeco#41.

Change-Id: Ibf4e102c89fa5e20bbc097b6b500124aa59d352d
Signed-off-by: Denis Knoepfle <[email protected]>
  • Loading branch information
DenisKnoepfle authored and Alexander Wert committed Aug 5, 2014
1 parent fb4882e commit 7e1d29c
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
Expand All @@ -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<IDeletable> deletables = getSelectedDeletables();

String label = getLabelForDeletables(deletables);
element.setText(label);
}

private String getLabelForDeletables(List<IDeletable> 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<IDeletable> getSelectedDeletables() {
List<IDeletable> 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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}
Original file line number Diff line number Diff line change
Expand Up @@ -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!";
Expand Down Expand Up @@ -213,6 +214,11 @@ public void delete() {
}
}

@Override
public String getElementTypeName() {
return ELEMENT_TYPE_NAME;
}

private String createErrorMessage(List<IProject> projects, List<String> detailErrorMessages) {
StringBuilder sb = new StringBuilder();
sb.append("Error while deleting project(s): ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -150,4 +151,9 @@ public void delete() {
}
}

@Override
public String getElementTypeName() {
return ELEMENT_TYPE_NAME;
}

}

0 comments on commit 7e1d29c

Please sign in to comment.