merge-update from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / handler / DeleteHandler.java
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.model.MessagingUtils;
37 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
38 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator;
39 import eu.etaxonomy.taxeditor.navigation.navigator.operation.DeleteOperation;
40 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
41
42 /**
43 * <p>DeleteTreeNodeHandler class.</p>
44 *
45 * @author n.hoffmann
46 * @created 06.04.2009
47 * @version 1.0
48 */
49 public class DeleteHandler extends AbstractHandler implements IHandler{
50
51 protected IWorkbenchPage activePage;
52 protected TaxonNavigator taxonNavigator;
53
54 /** {@inheritDoc} */
55 @Override
56 public Object execute(ExecutionEvent event) throws ExecutionException {
57
58 activePage = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
59
60 taxonNavigator = NavigationUtil.showNavigator();
61
62 TreeSelection selection = (TreeSelection) HandlerUtil.getCurrentSelection(event);
63
64 String plural = selection.size() > 1 ? "s" : "";
65 // Prompt user for confirmation
66
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 ITaxonTreeNode treeNode = treeNodes.iterator().next();
89 ITaxonTreeNode taxonNode =treeNode;
90
91 if (taxonNode instanceof Classification && taxonNode.hasChildNodes()){
92 if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the classification? The tree has children, they will be deleted, too")){
93 return null;
94 }
95 } else if (taxonNode instanceof Classification && !taxonNode.hasChildNodes()){
96 if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the classification?")){
97 return null;
98 }
99 } else {
100 if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected node?")){
101 return null;
102 }
103 }
104
105 if (allEditorsClosed){
106 /*if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
107 return null;
108 }*/
109 operation = new DeleteOperation(
110 event.getCommand().getName(), NavigationUtil.getUndoContext(),
111 taxonNode, new TaxonDeletionConfigurator(), taxonNavigator, taxonNavigator);
112
113 NavigationUtil.executeOperation(operation);
114 //}
115 }
116
117
118
119 } catch (NotDefinedException e) {
120 MessagingUtils.warn(getClass(), "Command name not set");
121 }
122 } else{
123 try{
124 if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
125 return null;
126 }
127 if (allEditorsClosed){
128 operation = new DeleteOperation(
129 event.getCommand().getName(), NavigationUtil.getUndoContext(),
130 treeNodes, new TaxonDeletionConfigurator(), taxonNavigator, taxonNavigator);
131
132 NavigationUtil.executeOperation(operation);
133 }
134 }catch (NotDefinedException e) {
135 MessagingUtils.warn(getClass(), "Command name not set");
136 }
137 }
138 return null;
139 }
140
141 protected boolean closeObsoleteEditor(TaxonNode taxonNode){
142 boolean result = true;
143 for (IEditorReference ref : activePage.getEditorReferences()) {
144 try {
145 String treeIndex = ((ITreeNode)taxonNode).treeIndex();
146
147
148 IEditorInput input = ref.getEditorInput();
149 if (input instanceof TaxonEditorInput) {
150 TaxonNode node = ((TaxonEditorInput) input).getTaxonNode();
151 //if node is a child of taxonNode then close the editor
152 if( ((ITreeNode) node).treeIndex().startsWith(treeIndex)){
153 //if (taxonNode.equals(node)) {
154 result &= activePage.closeEditor(ref.getEditor(false), true);
155
156 }
157 }
158 } catch (PartInitException e) {
159 continue;
160 }
161 }
162 return result;
163 }
164 }