Project

General

Profile

Download (7.91 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.model.taxon.Synonym;
31
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
32
import eu.etaxonomy.taxeditor.model.AbstractUtility;
33
import eu.etaxonomy.taxeditor.model.MessagingUtils;
34
import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
35
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
36
import eu.etaxonomy.taxeditor.navigation.navigator.e4.TaxonNavigatorE4;
37
import eu.etaxonomy.taxeditor.navigation.navigator.operation.DeleteOperation;
38
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
39
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
40

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

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

    
55
    protected static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S = Messages.DeleteHandler_DELETE_NODES;
56
    protected static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE = Messages.DeleteHandler_DELETE_NODE;
57

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

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

    
68
        TaxonNavigatorE4 taxonNavigator = (TaxonNavigatorE4) activePart.getObject();
69

    
70
		Iterator selectionIterator = selection.iterator();
71
		Set<TaxonNodeDto> treeNodes = new HashSet();
72

    
73
		while (selectionIterator.hasNext()){
74
			Object object = selectionIterator.next();
75
			if(object instanceof TaxonNodeDto) {
76
                treeNodes.add((TaxonNodeDto) object);
77
            }
78
		}
79
		boolean allEditorsClosed = true;
80
		for (TaxonNodeDto treeNode : treeNodes){
81
			allEditorsClosed &= closeObsoleteEditor(treeNode);
82
		}
83
		AbstractPostOperation operation = null;
84

    
85

    
86
		TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
87
		config.setDeleteInAllClassifications(false);
88

    
89
		if (treeNodes.size() == 1 ){
90
			try {
91

    
92
			    TaxonNodeDto taxonNode = treeNodes.iterator().next();
93
//			    TaxonNodeDto taxonNode = treeNode;
94
//				TaxonNodeDeletionConfigurator configNodes = new TaxonNodeDeletionConfigurator();
95

    
96
				//configNodes.setDeleteTaxon(false);
97
                if (taxonNode.getTaxonUuid() == null && taxonNode.getTaxonomicChildrenCount()>0){
98
					if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, shell, CONFIRM_DELETION, DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION+THE_TREE_HAS_CHILDREN_THEY_WILL_BE_DELETED_TOO)){
99
						return;
100
					}
101
				} else if (taxonNode.getTaxonUuid() != null && taxonNode.getTaxonomicChildrenCount()==0){
102
					if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, shell, CONFIRM_DELETION, DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION)){
103
						return;
104
					}
105
				} else {
106

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

    
118
						if (dialog_result == 0){
119
							//delete all children
120
							config.getTaxonNodeConfig().setChildHandling(ChildHandling.DELETE);
121

    
122
						} else if (dialog_result == 1){
123
							//move children
124
						    config.getTaxonNodeConfig().setChildHandling(ChildHandling.MOVE_TO_PARENT);
125

    
126
						} else if (dialog_result == 2){
127
							//skip
128
							return;
129

    
130
						}
131
					}
132
				}
133

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

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

    
147
						AbstractUtility.executeOperation(operation, sync);
148

    
149
						//}
150
				}
151

    
152

    
153

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

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

    
172
					AbstractUtility.executeOperation(operation, sync);
173

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

    
182
    @CanExecute
183
    private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
184
        boolean canExecute = false;
185
        canExecute = !selection.isEmpty();
186
        Object[] array = selection.toArray();
187
        for (Object object : array) {
188
            canExecute &= !(object instanceof Synonym);
189
        }
190
        menuItem.setVisible(canExecute);
191
        return canExecute;
192
    }
193

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