Project

General

Profile

Download (5.42 KB) Statistics
| Branch: | Tag: | Revision:
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
		
66
		
67

    
68
		Iterator selectionIterator = selection.iterator();
69
		Set<ITaxonTreeNode> treeNodes = new HashSet<ITaxonTreeNode>();
70

    
71
		while (selectionIterator.hasNext()){
72
			Object object = selectionIterator.next();
73
			if(object instanceof ITaxonTreeNode) {
74
                treeNodes.add((ITaxonTreeNode) object);
75
            }
76
		}
77
		boolean allEditorsClosed = true;
78
		for (ITaxonTreeNode treeNode : treeNodes){
79
			if(treeNode instanceof TaxonNode) {
80
				allEditorsClosed &= closeObsoleteEditor((TaxonNode) treeNode);
81
			}
82
		}
83
		AbstractPostOperation operation = null;
84
		if (treeNodes.size() == 1 ){
85
			try {
86
				
87
				ITaxonTreeNode treeNode = treeNodes.iterator().next();
88
				ITaxonTreeNode taxonNode =treeNode;
89
				
90
				if (taxonNode instanceof Classification && taxonNode.hasChildNodes()){
91
					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")){
92
						return null;
93
					}
94
				} else if (taxonNode instanceof Classification && !taxonNode.hasChildNodes()){
95
					if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the classification?")){
96
						return null;
97
					}
98
				} else {
99
					if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected node?")){
100
						return null;
101
					}
102
				}
103
	
104
				if (allEditorsClosed){
105
					/*if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
106
						return null;
107
					}*/
108
						operation = new DeleteOperation(
109
								event.getCommand().getName(), NavigationUtil.getUndoContext(),
110
								taxonNode, new TaxonDeletionConfigurator(), taxonNavigator, taxonNavigator);
111
			
112
						NavigationUtil.executeOperation(operation);
113
						//}
114
				}
115
	
116
		
117
	
118
			} catch (NotDefinedException e) {
119
				NavigationUtil.warn(getClass(), "Command name not set");
120
			}
121
		} else{
122
			try{
123
				if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
124
					return null;
125
				}
126
				if (allEditorsClosed){
127
					operation = new DeleteOperation(
128
							event.getCommand().getName(), NavigationUtil.getUndoContext(),
129
							treeNodes, new TaxonDeletionConfigurator(), taxonNavigator, taxonNavigator);
130
		
131
					NavigationUtil.executeOperation(operation);
132
				}
133
			}catch (NotDefinedException e) {
134
				NavigationUtil.warn(getClass(), "Command name not set");
135
			}
136
		}
137
		return null;
138
	}
139

    
140
	private boolean closeObsoleteEditor(TaxonNode taxonNode){
141
		boolean result = true;
142
		for (IEditorReference ref : activePage.getEditorReferences()) {
143
			try {
144
				String treeIndex = ((ITreeNode)taxonNode).treeIndex();
145
				
146
				
147
				IEditorInput input = ref.getEditorInput();
148
				if (input instanceof TaxonEditorInput) {
149
					TaxonNode node = ((TaxonEditorInput) input).getTaxonNode();
150
					//if node is a child of taxonNode then close the editor
151
					if( ((ITreeNode) node).treeIndex().startsWith(treeIndex)){
152
					//if (taxonNode.equals(node)) {
153
						result &= activePage.closeEditor(ref.getEditor(false), true);
154
						
155
					}
156
				}
157
			} catch (PartInitException e) {
158
				continue;
159
			}
160
		}
161
		return result;
162
	}
163
}
(2-2/7)