Project

General

Profile

Download (7.23 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.deleteConfigurator.DeleteConfiguratorDialog;
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
		Iterator selectionIterator = selection.iterator();
68
		Set<ITaxonTreeNode> treeNodes = new HashSet<ITaxonTreeNode>();
69

    
70
		while (selectionIterator.hasNext()){
71
			Object object = selectionIterator.next();
72
			if(object instanceof ITaxonTreeNode) {
73
                treeNodes.add((ITaxonTreeNode) object);
74
            }
75
		}
76
		boolean allEditorsClosed = true;
77
		for (ITaxonTreeNode treeNode : treeNodes){
78
			if(treeNode instanceof TaxonNode) {
79
				allEditorsClosed &= closeObsoleteEditor((TaxonNode) treeNode);
80
			}
81
		}
82
		AbstractPostOperation operation = null;
83
		TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
84
		config.setDeleteInAllClassifications(false);
85

    
86
		if (treeNodes.size() == 1 ){
87
			try {
88

    
89
				ITaxonTreeNode treeNode = treeNodes.iterator().next();
90
				ITaxonTreeNode taxonNode =treeNode;
91
				TaxonNodeDeletionConfigurator configNodes = new TaxonNodeDeletionConfigurator();
92

    
93
				//configNodes.setDeleteTaxon(false);
94
				if (taxonNode instanceof Classification && taxonNode.hasChildNodes()){
95
					if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the classification? The tree has children, they will be deleted, too.")){
96
						return null;
97
					}
98
				} else if (taxonNode instanceof Classification && !taxonNode.hasChildNodes()){
99
					if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the classification?")){
100
						return null;
101
					}
102
				} else {
103

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

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

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

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

    
144
						AbstractUtility.executeOperation(operation);
145
						//}
146
				}
147

    
148

    
149

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

    
165
					AbstractUtility.executeOperation(operation);
166
				}
167
			}catch (NotDefinedException e) {
168
				MessagingUtils.warn(getClass(), "Command name not set");
169
			} catch (Exception e){
170
                MessagingUtils.error(getClass(), e);
171
            }
172
		}
173
		return null;
174
	}
175

    
176
	protected boolean closeObsoleteEditor(TaxonNode taxonNode){
177
		boolean result = true;
178
		for (IEditorReference ref : activePage.getEditorReferences()) {
179
			try {
180
				String treeIndex = ((ITreeNode)taxonNode).treeIndex();
181

    
182

    
183
				IEditorInput input = ref.getEditorInput();
184
				if (input instanceof TaxonEditorInput) {
185
					TaxonNode node = ((TaxonEditorInput) input).getTaxonNode();
186
					//if node is a child of taxonNode then close the editor
187
					if( ((ITreeNode) node).treeIndex().startsWith(treeIndex)){
188
					//if (taxonNode.equals(node)) {
189
						result &= activePage.closeEditor(ref.getEditor(false), true);
190

    
191
					}
192
				}
193
			} catch (PartInitException e) {
194
				continue;
195
			}
196
		}
197
		return result;
198
	}
199
}
(4-4/11)