Project

General

Profile

Download (8.32 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.core.di.annotations.CanExecute;
19
import org.eclipse.e4.core.di.annotations.Execute;
20
import org.eclipse.e4.ui.di.UISynchronize;
21
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
22
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
23
import org.eclipse.e4.ui.services.IServiceConstants;
24
import org.eclipse.jface.dialogs.MessageDialog;
25
import org.eclipse.jface.viewers.TreeSelection;
26
import org.eclipse.swt.widgets.Shell;
27

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

    
43
/**
44
 *
45
 * @author pplitzner
46
 * @date 05.09.2017
47
 *
48
 */
49
public class DeleteHandlerE4 {
50

    
51
    protected static final String SKIP = Messages.DeleteHandler_SKIP;
52
    protected static final String MOVE_CHILDREN_TO_PARENT_NODE = Messages.DeleteHandler_MOVE_TO_PARENT;
53
    protected static final String DELETE_ALL_CHILDREN = Messages.DeleteHandler_DELETE_ALL;
54
    protected static final String THERE_ARE_CHILDNODES_WHICH_WILL_BE_DELETED_TOO = Messages.DeleteHandler_THERE_ARE_CHILDNODES;
55
    protected static final String THE_TREE_HAS_CHILDREN_THEY_WILL_BE_DELETED_TOO = Messages.DeleteHandler_THERE_ARE_CHILDREN;
56

    
57
    protected static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S = Messages.DeleteHandler_DELETE_NODES;
58
    protected static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE = Messages.DeleteHandler_DELETE_NODE;
59

    
60
    protected static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION = Messages.DeleteHandler_DELETE_CLASSIFICATION;
61
    protected static final String CONFIRM_DELETION = Messages.DeleteHandler_CONFIRM_DELETE;
62
    protected static final String DELETE = Messages.DeleteHandler_DELETE;
63

    
64
    @Execute
65
    public void execute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection,
66
            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell,
67
            @Named(IServiceConstants.ACTIVE_PART)MPart activePart,
68
            MHandledMenuItem menuItem, UISynchronize sync) {
69

    
70
        TaxonNavigatorE4 taxonNavigator = (TaxonNavigatorE4) activePart.getObject();
71

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

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

    
91
		if (treeNodes.size() == 1 ){
92
			try {
93

    
94
				ITaxonTreeNode treeNode = treeNodes.iterator().next();
95
				ITaxonTreeNode taxonNode =treeNode;
96
				TaxonNodeDeletionConfigurator configNodes = new TaxonNodeDeletionConfigurator();
97

    
98
				//configNodes.setDeleteTaxon(false);
99
                if (taxonNode instanceof TaxonNode && !((TaxonNode)taxonNode).hasTaxon() && taxonNode.hasChildNodes()){
100
					if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, shell, CONFIRM_DELETION, DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION+THE_TREE_HAS_CHILDREN_THEY_WILL_BE_DELETED_TOO)){
101
						return;
102
					}
103
				} else if (taxonNode instanceof TaxonNode && !((TaxonNode)taxonNode).hasTaxon() && !taxonNode.hasChildNodes()){
104
					if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, shell, CONFIRM_DELETION, DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION)){
105
						return;
106
					}
107
				} else {
108

    
109
					if (taxonNode.hasChildNodes()){
110
                        DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(
111
                                config,
112
                                shell,
113
                                CONFIRM_DELETION,
114
                                null,
115
                                DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE+THERE_ARE_CHILDNODES_WHICH_WILL_BE_DELETED_TOO,
116
                                MessageDialog.WARNING, new String[] { DELETE_ALL_CHILDREN,
117
                                        MOVE_CHILDREN_TO_PARENT_NODE, SKIP }, 0);
118
						int dialog_result = dialog.open();
119

    
120
						if (dialog_result == 0){
121
							//delete all children
122
							configNodes.setChildHandling(ChildHandling.DELETE);
123
							config.setTaxonNodeConfig(configNodes);
124
						} else if (dialog_result == 1){
125
							//move children
126
							configNodes.setChildHandling(ChildHandling.MOVE_TO_PARENT);
127
							config.setTaxonNodeConfig(configNodes);
128
						} else if (dialog_result == 2){
129
							//skip
130
							return;
131

    
132
						}
133
					}else{
134
						if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(configNodes, shell, CONFIRM_DELETION, DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S)){
135
							return;
136
						}
137
						config.setTaxonNodeConfig(configNodes);
138
					}
139
				}
140

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

    
146
						operation = new DeleteOperation(menuItem.getLocalizedLabel(),
147
						        NavigationUtil.getUndoContext(),
148
								taxonNode,
149
								config,
150
								taxonNavigator,
151
								taxonNavigator,
152
								taxonNavigator);
153

    
154
						AbstractUtility.executeOperation(operation, sync);
155

    
156
						//}
157
				}
158

    
159

    
160

    
161
			} catch (Exception e){
162
			    MessagingUtils.error(getClass(), e);
163
			}
164
		} else{
165
			try{
166
				if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, shell, CONFIRM_DELETION, DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S)){
167
					return;
168
				}
169
				if (allEditorsClosed){
170

    
171
					operation = new DeleteOperation(menuItem.getLocalizedLabel(),
172
					        NavigationUtil.getUndoContext(),
173
							treeNodes,
174
							new TaxonDeletionConfigurator(),
175
							taxonNavigator,
176
							taxonNavigator,
177
							taxonNavigator);
178

    
179
					AbstractUtility.executeOperation(operation, sync);
180

    
181
				}
182
			}catch (Exception e){
183
                MessagingUtils.error(getClass(), e);
184
            }
185
		}
186
		return;
187
	}
188

    
189
    @CanExecute
190
    private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
191
        boolean canExecute = false;
192
        canExecute = !selection.isEmpty() && !(selection.getFirstElement() instanceof Synonym);
193
        menuItem.setVisible(canExecute);
194
        return canExecute;
195
    }
196

    
197
	protected boolean closeObsoleteEditor(TaxonNode taxonNode){
198
	    //FIXME E4 migrate
199
		boolean result = true;
200
//		for (IEditorReference ref : activePage.getEditorReferences()) {
201
//			try {
202
//				String treeIndex = ((ITreeNode)taxonNode).treeIndex();
203
//
204
//
205
//				IEditorInput input = ref.getEditorInput();
206
//				if (input instanceof TaxonEditorInput) {
207
//					TaxonNode node = ((TaxonEditorInput) input).getTaxonNode();
208
//					//if node is a child of taxonNode then close the editor
209
//					if( ((ITreeNode) node).treeIndex().startsWith(treeIndex)){
210
//					//if (taxonNode.equals(node)) {
211
//						result &= activePage.closeEditor(ref.getEditor(false), true);
212
//
213
//					}
214
//				}
215
//			} catch (PartInitException e) {
216
//				continue;
217
//			}
218
//		}
219
		return result;
220
	}
221
}
(5-5/17)