Project

General

Profile

Download (8.03 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.model.application.ui.basic.MPart;
21
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
22
import org.eclipse.e4.ui.services.IServiceConstants;
23
import org.eclipse.jface.dialogs.MessageDialog;
24
import org.eclipse.jface.viewers.TreeSelection;
25
import org.eclipse.swt.widgets.Shell;
26

    
27
import eu.etaxonomy.cdm.api.service.config.NodeDeletionConfigurator.ChildHandling;
28
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
29
import eu.etaxonomy.cdm.api.service.config.TaxonNodeDeletionConfigurator;
30
import eu.etaxonomy.cdm.model.taxon.Classification;
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
    protected static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S = Messages.DeleteHandler_DELETE_NODE;
57
    protected static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION = Messages.DeleteHandler_DELETE_CLASSIFICATION;
58
    protected static final String CONFIRM_DELETION = Messages.DeleteHandler_CONFIRM_DELETE;
59

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

    
66
        TaxonNavigatorE4 taxonNavigator = (TaxonNavigatorE4) activePart.getObject();
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
		config.setDeleteInAllClassifications(false);
86

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

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

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

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

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

    
128
						}
129
					}else{
130
						if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(configNodes, shell, CONFIRM_DELETION, DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S)){
131
							return;
132
						}
133
						config.setTaxonNodeConfig(configNodes);
134
					}
135
				}
136

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

    
142
						operation = new DeleteOperation(menuItem.getLocalizedLabel(),
143
						        NavigationUtil.getUndoContext(),
144
								taxonNode,
145
								config,
146
								taxonNavigator,
147
								taxonNavigator,
148
								taxonNavigator);
149

    
150
						AbstractUtility.executeOperation(operation);
151

    
152
						//}
153
				}
154

    
155

    
156

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

    
167
					operation = new DeleteOperation(menuItem.getLocalizedLabel(),
168
					        NavigationUtil.getUndoContext(),
169
							treeNodes,
170
							new TaxonDeletionConfigurator(),
171
							taxonNavigator,
172
							taxonNavigator,
173
							taxonNavigator);
174

    
175
					AbstractUtility.executeOperation(operation);
176

    
177
				}
178
			}catch (Exception e){
179
                MessagingUtils.error(getClass(), e);
180
            }
181
		}
182
		return;
183
	}
184

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

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