Project

General

Profile

Download (7.13 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.common.NotDefinedException;
21
import org.eclipse.jface.dialogs.MessageDialog;
22
import org.eclipse.jface.viewers.TreeSelection;
23
import org.eclipse.ui.IEditorInput;
24
import org.eclipse.ui.IEditorReference;
25
import org.eclipse.ui.IWorkbenchPage;
26
import org.eclipse.ui.PartInitException;
27
import org.eclipse.ui.handlers.HandlerUtil;
28

    
29
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
30
import eu.etaxonomy.cdm.api.service.config.TaxonNodeDeletionConfigurator;
31
import eu.etaxonomy.cdm.api.service.config.TaxonNodeDeletionConfigurator.ChildHandling;
32
import eu.etaxonomy.cdm.model.common.ITreeNode;
33
import eu.etaxonomy.cdm.model.taxon.Classification;
34
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
35
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
36
import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
37
import eu.etaxonomy.taxeditor.model.MessagingUtils;
38
import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
39
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator;
40
import eu.etaxonomy.taxeditor.navigation.navigator.operation.DeleteOperation;
41
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
42
import eu.etaxonomy.taxeditor.ui.dialog.DeleteConfiguratorDialog;
43
import eu.etaxonomy.taxeditor.ui.dialog.DeleteTaxonConfiguratorDialog;
44

    
45
/**
46
 * <p>DeleteTreeNodeHandler class.</p>
47
 *
48
 * @author n.hoffmann
49
 * @created 06.04.2009
50
 * @version 1.0
51
 */
52
public class DeleteHandler extends AbstractHandler{
53

    
54
	protected IWorkbenchPage activePage;
55
	protected TaxonNavigator taxonNavigator;
56

    
57
	/** {@inheritDoc} */
58
	@Override
59
    public Object execute(ExecutionEvent event) throws ExecutionException {
60

    
61
		activePage = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
62

    
63
		taxonNavigator = NavigationUtil.showNavigator();
64

    
65
		TreeSelection selection = (TreeSelection) HandlerUtil.getCurrentSelection(event);
66

    
67

    
68
		String plural = selection.size() > 1 ? "s" : "";
69
		// Prompt user for confirmation
70

    
71

    
72

    
73
		Iterator selectionIterator = selection.iterator();
74
		Set<ITaxonTreeNode> treeNodes = new HashSet<ITaxonTreeNode>();
75

    
76
		while (selectionIterator.hasNext()){
77
			Object object = selectionIterator.next();
78
			if(object instanceof ITaxonTreeNode) {
79
                treeNodes.add((ITaxonTreeNode) object);
80
            }
81
		}
82
		boolean allEditorsClosed = true;
83
		for (ITaxonTreeNode treeNode : treeNodes){
84
			if(treeNode instanceof TaxonNode) {
85
				allEditorsClosed &= closeObsoleteEditor((TaxonNode) treeNode);
86
			}
87
		}
88
		AbstractPostOperation operation = null;
89
		TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
90
		if (treeNodes.size() == 1 ){
91
			try {
92

    
93
				ITaxonTreeNode treeNode = treeNodes.iterator().next();
94
				ITaxonTreeNode taxonNode =treeNode;
95
				TaxonNodeDeletionConfigurator configNodes = new TaxonNodeDeletionConfigurator();
96
				if (taxonNode instanceof Classification && taxonNode.hasChildNodes()){
97
					if(!DeleteTaxonConfiguratorDialog.openConfirmWithConfigurator(config, HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the classification? The tree has children, they will be deleted, too.")){
98
						return null;
99
					}
100
				} else if (taxonNode instanceof Classification && !taxonNode.hasChildNodes()){
101
					if(!DeleteTaxonConfiguratorDialog.openConfirmWithConfigurator(config, HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the classification?")){
102
						return null;
103
					}
104
				} else {
105

    
106
					if (taxonNode.hasChildNodes()){
107
                        DeleteConfiguratorDialog dialog = new DeleteTaxonConfiguratorDialog(
108
                                config,
109
                                HandlerUtil.getActiveShell(event),
110
                                "Confirm Deletion",
111
                                null,
112
                                "Do you really want to delete the selected node? It has childnodes, they will be deleted, too.",
113
                                MessageDialog.WARNING, new String[] { "Delete all children",
114
                                        "Move children to parent node", "Skip" }, 0);
115
						int result = dialog.open();
116

    
117
						if (result == 0){
118
							//delete all children
119
							configNodes.setChildHandling(ChildHandling.DELETE);
120
							config.setTaxonNodeConfig(configNodes);
121
						} else if (result == 1){
122
							//move children
123
							configNodes.setChildHandling(ChildHandling.MOVE_TO_PARENT);
124
							config.setTaxonNodeConfig(configNodes);
125
						} else if (result == 2){
126
							//skip
127
							return null;
128

    
129
						}
130
					}else{
131
						if(!DeleteTaxonConfiguratorDialog.openConfirmWithConfigurator(config, HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected node?")){
132
							return null;
133
						}
134
					}
135
				}
136

    
137
				if (allEditorsClosed){
138
					/*if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
139
						return null;
140
					}*/
141

    
142
						operation = new DeleteOperation(event.getCommand().getName(),
143
						        NavigationUtil.getUndoContext(),
144
								taxonNode,
145
								config,
146
								taxonNavigator,
147
								taxonNavigator,
148
								taxonNavigator);
149

    
150
						NavigationUtil.executeOperation(operation);
151

    
152
						//}
153
				}
154

    
155

    
156

    
157
			} catch (NotDefinedException e) {
158
				MessagingUtils.warn(getClass(), "Command name not set");
159
			}
160
		} else{
161
			try{
162
				if(!DeleteTaxonConfiguratorDialog.openConfirmWithConfigurator(config, HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
163
					return null;
164
				}
165
				if (allEditorsClosed){
166

    
167
					operation = new DeleteOperation(event.getCommand().getName(),
168
					        NavigationUtil.getUndoContext(),
169
							treeNodes,
170
							new TaxonDeletionConfigurator(),
171
							taxonNavigator,
172
							taxonNavigator,
173
							taxonNavigator);
174

    
175
					NavigationUtil.executeOperation(operation);
176

    
177
				}
178
			}catch (NotDefinedException e) {
179
				MessagingUtils.warn(getClass(), "Command name not set");
180
			}
181
		}
182
		return null;
183
	}
184

    
185
	protected boolean closeObsoleteEditor(TaxonNode taxonNode){
186
		boolean result = true;
187
		for (IEditorReference ref : activePage.getEditorReferences()) {
188
			try {
189
				String treeIndex = ((ITreeNode)taxonNode).treeIndex();
190

    
191

    
192
				IEditorInput input = ref.getEditorInput();
193
				if (input instanceof TaxonEditorInput) {
194
					TaxonNode node = ((TaxonEditorInput) input).getTaxonNode();
195
					//if node is a child of taxonNode then close the editor
196
					if( ((ITreeNode) node).treeIndex().startsWith(treeIndex)){
197
					//if (taxonNode.equals(node)) {
198
						result &= activePage.closeEditor(ref.getEditor(false), true);
199

    
200
					}
201
				}
202
			} catch (PartInitException e) {
203
				continue;
204
			}
205
		}
206
		return result;
207
	}
208
}
(3-3/10)