Project

General

Profile

Download (7.53 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.taxeditor.navigation.navigator.e4.handler;
11

    
12
import java.util.HashSet;
13
import java.util.Iterator;
14
import java.util.Set;
15

    
16
import javax.inject.Named;
17

    
18
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
19
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
20
import org.eclipse.e4.ui.services.IServiceConstants;
21
import org.eclipse.jface.dialogs.MessageDialog;
22
import org.eclipse.jface.viewers.TreeSelection;
23
import org.eclipse.swt.widgets.Shell;
24

    
25
import eu.etaxonomy.cdm.api.service.config.NodeDeletionConfigurator.ChildHandling;
26
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
27
import eu.etaxonomy.cdm.api.service.config.TaxonNodeDeletionConfigurator;
28
import eu.etaxonomy.cdm.model.taxon.Classification;
29
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
30
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
31
import eu.etaxonomy.taxeditor.model.AbstractUtility;
32
import eu.etaxonomy.taxeditor.model.MessagingUtils;
33
import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
34
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
35
import eu.etaxonomy.taxeditor.navigation.navigator.e4.TaxonNavigatorE4;
36
import eu.etaxonomy.taxeditor.navigation.navigator.operation.DeleteOperation;
37
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
38
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
39

    
40
/**
41
 *
42
 * @author pplitzner
43
 * @date 05.09.2017
44
 *
45
 */
46
public class DeleteHandlerE4 {
47

    
48
    protected static final String SKIP = Messages.DeleteHandler_SKIP;
49
    protected static final String MOVE_CHILDREN_TO_PARENT_NODE = Messages.DeleteHandler_MOVE_TO_PARENT;
50
    protected static final String DELETE_ALL_CHILDREN = Messages.DeleteHandler_DELETE_ALL;
51
    protected static final String THERE_ARE_CHILDNODES_WHICH_WILL_BE_DELETED_TOO = Messages.DeleteHandler_THERE_ARE_CHILDNODES;
52
    protected static final String THE_TREE_HAS_CHILDREN_THEY_WILL_BE_DELETED_TOO = Messages.DeleteHandler_THERE_ARE_CHILDREN;
53
    protected static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S = Messages.DeleteHandler_DELETE_NODE;
54
    protected static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION = Messages.DeleteHandler_DELETE_CLASSIFICATION;
55
    protected static final String CONFIRM_DELETION = Messages.DeleteHandler_CONFIRM_DELETE;
56

    
57
    public void execute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection,
58
            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell,
59
            @Named(IServiceConstants.ACTIVE_PART)MPart activePart,
60
            MHandledMenuItem menuItem) {
61

    
62
        TaxonNavigatorE4 taxonNavigator = (TaxonNavigatorE4) activePart.getObject();
63

    
64
		Iterator selectionIterator = selection.iterator();
65
		Set<ITaxonTreeNode> treeNodes = new HashSet<ITaxonTreeNode>();
66

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

    
83
		if (treeNodes.size() == 1 ){
84
			try {
85

    
86
				ITaxonTreeNode treeNode = treeNodes.iterator().next();
87
				ITaxonTreeNode taxonNode =treeNode;
88
				TaxonNodeDeletionConfigurator configNodes = new TaxonNodeDeletionConfigurator();
89

    
90
				//configNodes.setDeleteTaxon(false);
91
				if (taxonNode instanceof Classification && taxonNode.hasChildNodes()){
92
					if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, shell, CONFIRM_DELETION, DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION+THE_TREE_HAS_CHILDREN_THEY_WILL_BE_DELETED_TOO)){
93
						return;
94
					}
95
				} else if (taxonNode instanceof Classification && !taxonNode.hasChildNodes()){
96
					if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, shell, CONFIRM_DELETION, DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION)){
97
						return;
98
					}
99
				} else {
100

    
101
					if (taxonNode.hasChildNodes()){
102
                        DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(
103
                                config,
104
                                shell,
105
                                CONFIRM_DELETION,
106
                                null,
107
                                DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S+THERE_ARE_CHILDNODES_WHICH_WILL_BE_DELETED_TOO,
108
                                MessageDialog.WARNING, new String[] { DELETE_ALL_CHILDREN,
109
                                        MOVE_CHILDREN_TO_PARENT_NODE, SKIP }, 0);
110
						int dialog_result = dialog.open();
111

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

    
124
						}
125
					}else{
126
						if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(configNodes, shell, CONFIRM_DELETION, DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S)){
127
							return;
128
						}
129
						config.setTaxonNodeConfig(configNodes);
130
					}
131
				}
132

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

    
138
						operation = new DeleteOperation(menuItem.getLocalizedLabel(),
139
						        NavigationUtil.getUndoContext(),
140
								taxonNode,
141
								config,
142
								taxonNavigator,
143
								taxonNavigator,
144
								taxonNavigator);
145

    
146
						AbstractUtility.executeOperation(operation);
147

    
148
						//}
149
				}
150

    
151

    
152

    
153
			} catch (Exception e){
154
			    MessagingUtils.error(getClass(), e);
155
			}
156
		} else{
157
			try{
158
				if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, shell, CONFIRM_DELETION, DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S)){
159
					return;
160
				}
161
				if (allEditorsClosed){
162

    
163
					operation = new DeleteOperation(menuItem.getLocalizedLabel(),
164
					        NavigationUtil.getUndoContext(),
165
							treeNodes,
166
							new TaxonDeletionConfigurator(),
167
							taxonNavigator,
168
							taxonNavigator,
169
							taxonNavigator);
170

    
171
					AbstractUtility.executeOperation(operation);
172

    
173
				}
174
			}catch (Exception e){
175
                MessagingUtils.error(getClass(), e);
176
            }
177
		}
178
		return;
179
	}
180

    
181
	protected boolean closeObsoleteEditor(TaxonNode taxonNode){
182
	    //FIXME E4 migrate
183
		boolean result = true;
184
//		for (IEditorReference ref : activePage.getEditorReferences()) {
185
//			try {
186
//				String treeIndex = ((ITreeNode)taxonNode).treeIndex();
187
//
188
//
189
//				IEditorInput input = ref.getEditorInput();
190
//				if (input instanceof TaxonEditorInput) {
191
//					TaxonNode node = ((TaxonEditorInput) input).getTaxonNode();
192
//					//if node is a child of taxonNode then close the editor
193
//					if( ((ITreeNode) node).treeIndex().startsWith(treeIndex)){
194
//					//if (taxonNode.equals(node)) {
195
//						result &= activePage.closeEditor(ref.getEditor(false), true);
196
//
197
//					}
198
//				}
199
//			} catch (PartInitException e) {
200
//				continue;
201
//			}
202
//		}
203
		return result;
204
	}
205
}
(5-5/11)