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.api.service.config.TaxonDeletionConfigurator;
|
31
|
import eu.etaxonomy.cdm.model.common.ITreeNode;
|
32
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
33
|
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
|
34
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
35
|
import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
|
36
|
import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
|
37
|
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator;
|
38
|
import eu.etaxonomy.taxeditor.navigation.navigator.operation.DeleteOperation;
|
39
|
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
|
40
|
|
41
|
/**
|
42
|
* <p>DeleteTreeNodeHandler class.</p>
|
43
|
*
|
44
|
* @author n.hoffmann
|
45
|
* @created 06.04.2009
|
46
|
* @version 1.0
|
47
|
*/
|
48
|
public class DeleteHandler extends AbstractHandler implements IHandler{
|
49
|
|
50
|
private IWorkbenchPage activePage;
|
51
|
private TaxonNavigator taxonNavigator;
|
52
|
|
53
|
/** {@inheritDoc} */
|
54
|
@Override
|
55
|
public Object execute(ExecutionEvent event) throws ExecutionException {
|
56
|
|
57
|
activePage = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
|
58
|
|
59
|
taxonNavigator = NavigationUtil.showNavigator();
|
60
|
|
61
|
TreeSelection selection = (TreeSelection) HandlerUtil.getCurrentSelection(event);
|
62
|
|
63
|
String plural = selection.size() > 1 ? "s" : "";
|
64
|
// Prompt user for confirmation
|
65
|
if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Are you sure you want to delete the selected node" + plural +"?")){
|
66
|
return null;
|
67
|
}
|
68
|
|
69
|
Iterator selectionIterator = selection.iterator();
|
70
|
Set<ITaxonTreeNode> treeNodes = new HashSet<ITaxonTreeNode>();
|
71
|
|
72
|
while (selectionIterator.hasNext()){
|
73
|
Object object = selectionIterator.next();
|
74
|
if(object instanceof ITaxonTreeNode) {
|
75
|
treeNodes.add((ITaxonTreeNode) object);
|
76
|
}
|
77
|
}
|
78
|
boolean allEditorsClosed = true;
|
79
|
for (ITaxonTreeNode treeNode : treeNodes){
|
80
|
if(treeNode instanceof TaxonNode) {
|
81
|
allEditorsClosed &= closeObsoleteEditor((TaxonNode) treeNode);
|
82
|
}
|
83
|
}
|
84
|
AbstractPostOperation operation = null;
|
85
|
if (treeNodes.size() == 1 ){
|
86
|
try {
|
87
|
|
88
|
|
89
|
if (allEditorsClosed){
|
90
|
ITaxonTreeNode treeNode = treeNodes.iterator().next();
|
91
|
/*if (treeNode instanceof Classification){
|
92
|
operation = new DeleteOperation(
|
93
|
event.getCommand().getName(), NavigationUtil.getUndoContext(),
|
94
|
treeNodes, new TaxonDeletionConfigurator(), taxonNavigator, taxonNavigator);
|
95
|
|
96
|
NavigationUtil.executeOperation(operation);
|
97
|
}else {*/
|
98
|
ITaxonTreeNode taxonNode =treeNode;
|
99
|
operation = new DeleteOperation(
|
100
|
event.getCommand().getName(), NavigationUtil.getUndoContext(),
|
101
|
taxonNode, new TaxonDeletionConfigurator(), taxonNavigator, taxonNavigator);
|
102
|
|
103
|
NavigationUtil.executeOperation(operation);
|
104
|
//}
|
105
|
}
|
106
|
|
107
|
|
108
|
|
109
|
} catch (NotDefinedException e) {
|
110
|
NavigationUtil.warn(getClass(), "Command name not set");
|
111
|
}
|
112
|
} else{
|
113
|
try{
|
114
|
if (allEditorsClosed){
|
115
|
operation = new DeleteOperation(
|
116
|
event.getCommand().getName(), NavigationUtil.getUndoContext(),
|
117
|
treeNodes, new TaxonDeletionConfigurator(), taxonNavigator, taxonNavigator);
|
118
|
|
119
|
NavigationUtil.executeOperation(operation);
|
120
|
}
|
121
|
}catch (NotDefinedException e) {
|
122
|
NavigationUtil.warn(getClass(), "Command name not set");
|
123
|
}
|
124
|
}
|
125
|
return null;
|
126
|
}
|
127
|
|
128
|
private boolean closeObsoleteEditor(TaxonNode taxonNode){
|
129
|
boolean result = true;
|
130
|
for (IEditorReference ref : activePage.getEditorReferences()) {
|
131
|
try {
|
132
|
String treeIndex = ((ITreeNode)taxonNode).treeIndex();
|
133
|
|
134
|
|
135
|
IEditorInput input = ref.getEditorInput();
|
136
|
if (input instanceof TaxonEditorInput) {
|
137
|
TaxonNode node = ((TaxonEditorInput) input).getTaxonNode();
|
138
|
//if node is a child of taxonNode then close the editor
|
139
|
if( ((ITreeNode) node).treeIndex().startsWith(treeIndex)){
|
140
|
//if (taxonNode.equals(node)) {
|
141
|
result &= activePage.closeEditor(ref.getEditor(false), true);
|
142
|
|
143
|
}
|
144
|
}
|
145
|
} catch (PartInitException e) {
|
146
|
continue;
|
147
|
}
|
148
|
}
|
149
|
return result;
|
150
|
}
|
151
|
}
|