1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.taxeditor.navigation.navigator.handler;
|
12
|
|
13
|
import java.util.HashSet;
|
14
|
import java.util.Iterator;
|
15
|
import java.util.Set;
|
16
|
|
17
|
import org.eclipse.core.commands.AbstractHandler;
|
18
|
import org.eclipse.core.commands.ExecutionEvent;
|
19
|
import org.eclipse.core.commands.ExecutionException;
|
20
|
import org.eclipse.core.commands.IHandler;
|
21
|
import org.eclipse.core.commands.common.NotDefinedException;
|
22
|
import org.eclipse.jface.dialogs.MessageDialog;
|
23
|
import org.eclipse.jface.viewers.TreeSelection;
|
24
|
import org.eclipse.ui.IEditorInput;
|
25
|
import org.eclipse.ui.IEditorReference;
|
26
|
import org.eclipse.ui.IWorkbenchPage;
|
27
|
import org.eclipse.ui.PartInitException;
|
28
|
import org.eclipse.ui.handlers.HandlerUtil;
|
29
|
|
30
|
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
|
31
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
32
|
import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
|
33
|
import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
|
34
|
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator;
|
35
|
import eu.etaxonomy.taxeditor.navigation.navigator.operation.DeleteOperation;
|
36
|
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
|
37
|
|
38
|
/**
|
39
|
* <p>DeleteTreeNodeHandler class.</p>
|
40
|
*
|
41
|
* @author n.hoffmann
|
42
|
* @created 06.04.2009
|
43
|
* @version 1.0
|
44
|
*/
|
45
|
public class DeleteHandler extends AbstractHandler implements IHandler{
|
46
|
|
47
|
private IWorkbenchPage activePage;
|
48
|
private TaxonNavigator taxonNavigator;
|
49
|
|
50
|
/** {@inheritDoc} */
|
51
|
@Override
|
52
|
public Object execute(ExecutionEvent event) throws ExecutionException {
|
53
|
|
54
|
activePage = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
|
55
|
|
56
|
taxonNavigator = NavigationUtil.showNavigator();
|
57
|
|
58
|
TreeSelection selection = (TreeSelection) HandlerUtil.getCurrentSelection(event);
|
59
|
|
60
|
String plural = selection.size() > 1 ? "s" : "";
|
61
|
// Prompt user for confirmation
|
62
|
if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Are you sure you want to delete the selected node" + plural +"?")){
|
63
|
return null;
|
64
|
}
|
65
|
|
66
|
Iterator selectionIterator = selection.iterator();
|
67
|
Set<ITaxonTreeNode> treeNodes = new HashSet<ITaxonTreeNode>();
|
68
|
|
69
|
while (selectionIterator.hasNext()){
|
70
|
Object object = selectionIterator.next();
|
71
|
if(object instanceof ITaxonTreeNode) {
|
72
|
treeNodes.add((ITaxonTreeNode) object);
|
73
|
}
|
74
|
}
|
75
|
|
76
|
AbstractPostOperation operation = null;
|
77
|
try {
|
78
|
|
79
|
|
80
|
boolean allEditorsClosed = true;
|
81
|
for (ITaxonTreeNode treeNode : treeNodes){
|
82
|
if(treeNode instanceof TaxonNode) {
|
83
|
allEditorsClosed &= closeObsoleteEditor((TaxonNode) treeNode);
|
84
|
}
|
85
|
}
|
86
|
|
87
|
if (allEditorsClosed){
|
88
|
operation = new DeleteOperation(
|
89
|
event.getCommand().getName(), NavigationUtil.getUndoContext(),
|
90
|
treeNodes, taxonNavigator, taxonNavigator);
|
91
|
|
92
|
NavigationUtil.executeOperation(operation);
|
93
|
}
|
94
|
|
95
|
|
96
|
|
97
|
} catch (NotDefinedException e) {
|
98
|
NavigationUtil.warn(getClass(), "Command name not set");
|
99
|
}
|
100
|
return null;
|
101
|
}
|
102
|
|
103
|
private boolean closeObsoleteEditor(TaxonNode taxonNode){
|
104
|
boolean result = true;
|
105
|
for (IEditorReference ref : activePage.getEditorReferences()) {
|
106
|
try {
|
107
|
IEditorInput input = ref.getEditorInput();
|
108
|
if (input instanceof TaxonEditorInput) {
|
109
|
TaxonNode node = ((TaxonEditorInput) input).getTaxonNode();
|
110
|
if (taxonNode.equals(node)) {
|
111
|
result &= activePage.closeEditor(ref.getEditor(false), true);
|
112
|
}
|
113
|
}
|
114
|
} catch (PartInitException e) {
|
115
|
continue;
|
116
|
}
|
117
|
}
|
118
|
return result;
|
119
|
}
|
120
|
}
|