Project

General

Profile

Download (6.99 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.AbstractUtility;
38
import eu.etaxonomy.taxeditor.model.MessagingUtils;
39
import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
40
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator;
41
import eu.etaxonomy.taxeditor.navigation.navigator.operation.DeleteOperation;
42
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
43
import eu.etaxonomy.taxeditor.ui.dialog.DeleteConfiguratorDialog;
44
import eu.etaxonomy.taxeditor.ui.dialog.DeleteTaxonConfiguratorDialog;
45

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

    
55
	protected IWorkbenchPage activePage;
56
	protected TaxonNavigator taxonNavigator;
57

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

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

    
64
		taxonNavigator = NavigationUtil.showNavigator();
65

    
66
		TreeSelection selection = (TreeSelection) HandlerUtil.getCurrentSelection(event);
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
		TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
85
		if (treeNodes.size() == 1 ){
86
			try {
87

    
88
				ITaxonTreeNode treeNode = treeNodes.iterator().next();
89
				ITaxonTreeNode taxonNode =treeNode;
90
				TaxonNodeDeletionConfigurator configNodes = new TaxonNodeDeletionConfigurator();
91
				if (taxonNode instanceof Classification && taxonNode.hasChildNodes()){
92
					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.")){
93
						return null;
94
					}
95
				} else if (taxonNode instanceof Classification && !taxonNode.hasChildNodes()){
96
					if(!DeleteTaxonConfiguratorDialog.openConfirmWithConfigurator(config, HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the classification?")){
97
						return null;
98
					}
99
				} else {
100

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

    
112
						if (result == 0){
113
							//delete all children
114
							configNodes.setChildHandling(ChildHandling.DELETE);
115
							config.setTaxonNodeConfig(configNodes);
116
						} else if (result == 1){
117
							//move children
118
							configNodes.setChildHandling(ChildHandling.MOVE_TO_PARENT);
119
							config.setTaxonNodeConfig(configNodes);
120
						} else if (result == 2){
121
							//skip
122
							return null;
123

    
124
						}
125
					}else{
126
						if(!DeleteTaxonConfiguratorDialog.openConfirmWithConfigurator(config, HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected node?")){
127
							return null;
128
						}
129
					}
130
				}
131

    
132
				if (allEditorsClosed){
133
					/*if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
134
						return null;
135
					}*/
136
						operation = new DeleteOperation(
137
								event.getCommand().getName(), NavigationUtil.getUndoContext(),
138
								taxonNode, config, taxonNavigator, taxonNavigator);
139

    
140
						AbstractUtility.executeOperation(operation);
141
						//}
142
				}
143

    
144

    
145

    
146
			} catch (NotDefinedException e) {
147
				MessagingUtils.warn(getClass(), "Command name not set");
148
			}
149
		} else{
150
			try{
151
				if(!DeleteTaxonConfiguratorDialog.openConfirmWithConfigurator(config, HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
152
					return null;
153
				}
154
				if (allEditorsClosed){
155
					operation = new DeleteOperation(
156
							event.getCommand().getName(), NavigationUtil.getUndoContext(),
157
							treeNodes, new TaxonDeletionConfigurator(), taxonNavigator, taxonNavigator);
158

    
159
					AbstractUtility.executeOperation(operation);
160
				}
161
			}catch (NotDefinedException e) {
162
				MessagingUtils.warn(getClass(), "Command name not set");
163
			}
164
		}
165
		return null;
166
	}
167

    
168
	protected boolean closeObsoleteEditor(TaxonNode taxonNode){
169
		boolean result = true;
170
		for (IEditorReference ref : activePage.getEditorReferences()) {
171
			try {
172
				String treeIndex = ((ITreeNode)taxonNode).treeIndex();
173

    
174

    
175
				IEditorInput input = ref.getEditorInput();
176
				if (input instanceof TaxonEditorInput) {
177
					TaxonNode node = ((TaxonEditorInput) input).getTaxonNode();
178
					//if node is a child of taxonNode then close the editor
179
					if( ((ITreeNode) node).treeIndex().startsWith(treeIndex)){
180
					//if (taxonNode.equals(node)) {
181
						result &= activePage.closeEditor(ref.getEditor(false), true);
182

    
183
					}
184
				}
185
			} catch (PartInitException e) {
186
				continue;
187
			}
188
		}
189
		return result;
190
	}
191
}
(3-3/10)