Project

General

Profile

Download (8.45 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2015 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
package eu.etaxonomy.taxeditor.navigation.navigator.e4.handler;
10

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

    
15
import javax.inject.Named;
16

    
17
import org.eclipse.core.commands.operations.AbstractOperation;
18
import org.eclipse.core.runtime.IStatus;
19
import org.eclipse.core.runtime.Status;
20
import org.eclipse.e4.core.di.annotations.CanExecute;
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.IStructuredSelection;
26
import org.eclipse.jface.viewers.TreeSelection;
27
import org.eclipse.swt.widgets.Shell;
28

    
29
import eu.etaxonomy.cdm.api.service.config.NodeDeletionConfigurator.ChildHandling;
30
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
31
import eu.etaxonomy.cdm.api.service.config.TaxonNodeDeletionConfigurator;
32
import eu.etaxonomy.cdm.model.taxon.Classification;
33
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
34
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
35
import eu.etaxonomy.taxeditor.editor.EditorUtil;
36
import eu.etaxonomy.taxeditor.model.MessagingUtils;
37
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
38
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
39
import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingDeleteTaxonNodeOperation;
40
import eu.etaxonomy.taxeditor.operation.e4.RemotingCdmHandlerE4;
41
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
42

    
43
/**
44
 * @author cmathew
45
 * @date 22 Jun 2015
46
 *
47
 */
48
public class RemotingDeleteTaxonNodeHandlerE4 extends RemotingCdmHandlerE4 {
49

    
50
    private TaxonDeletionConfigurator config;
51
    private Set<TaxonNodeDto> treeNodes;
52
    private Set<TaxonNodeDto> classifications;
53

    
54
    public RemotingDeleteTaxonNodeHandlerE4() {
55
        super(TaxonNavigatorLabels.DELETE_TAXON_NODE_LABEL);
56
    }
57

    
58
    @Override
59
    public IStatus allowOperations(IStructuredSelection selection,
60
            Shell shell,
61
            MPart activePart,
62
            MHandledMenuItem menuItem) {
63

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

    
67
        boolean containsNodeWithChildren = false;
68
        boolean containsClassification = false;
69
        boolean containsTaxonNodes = false;
70
        classifications = new HashSet();
71
        while (selectionIterator.hasNext()){
72
            Object object = selectionIterator.next();
73
            if(object instanceof TaxonNodeDto) {
74
                treeNodes.add((TaxonNodeDto) object);
75
            }else if (object instanceof TaxonNode && !((TaxonNode)object).hasTaxon()){
76
                TaxonNodeDto dto = new TaxonNodeDto(Classification.class,(TaxonNode)object);
77
                classifications.add(dto);
78
                containsClassification = true;
79
            }
80
        }
81
        for (TaxonNodeDto treeNode : treeNodes) {
82
           EditorUtil.closeObsoleteEditor(treeNode, partService);
83
        }
84

    
85
        config = new TaxonDeletionConfigurator();
86
        Iterator it = treeNodes.iterator();
87

    
88
        treeNodes = new HashSet();
89
        while (it.hasNext()){
90
            TaxonNodeDto treeNode = (TaxonNodeDto) it.next();
91

    
92
            treeNodes.add(treeNode);
93
            if (treeNode == null){
94
            	MessagingUtils.informationDialog(Messages.RemotingDeleteTaxonNodeHandler_NODE_DELETED, Messages.RemotingDeleteTaxonNodeHandler_NODE_DELETED_MESSAGE);
95
            	return Status.CANCEL_STATUS;
96

    
97
            }
98
           if (treeNode instanceof TaxonNodeDto){
99
            	containsTaxonNodes = true;
100
            	if (treeNode.getTaxonomicChildrenCount()>0){
101
            		containsNodeWithChildren = true;
102
            	}
103
            }
104

    
105

    
106
        }
107

    
108
        for (TaxonNodeDto rootNode: classifications){
109

    
110
            if (rootNode.getTaxonomicChildrenCount() > 0) {
111
                containsNodeWithChildren = true;
112
            }
113

    
114
        }
115
            TaxonNodeDeletionConfigurator configNodes = new TaxonNodeDeletionConfigurator();
116
			if (containsClassification && !containsTaxonNodes) {
117
				String message;
118
				if (containsClassification && containsNodeWithChildren) {
119
					message = DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION+DeleteHandlerE4.THE_TREE_HAS_CHILDREN_THEY_WILL_BE_DELETED_TOO;
120
				} else {
121
					message = DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION;
122
				}
123
				if (!MessageDialog.openConfirm(
124
						shell, DeleteHandlerE4.CONFIRM_DELETION,
125
						message)) {
126
					return Status.CANCEL_STATUS;
127
				}
128
			}
129
            else {
130
            	String confirmMessage= treeNodes.size() == 1?DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE:DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S;
131
                if (containsNodeWithChildren){
132

    
133
                	DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(
134
                            config,
135
                            shell,
136
                            DeleteHandlerE4.CONFIRM_DELETION,
137
                            null,
138
                            DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE+DeleteHandlerE4.THERE_ARE_CHILDNODES_WHICH_WILL_BE_DELETED_TOO,
139
                            MessageDialog.WARNING, new String[] { DeleteHandlerE4.DELETE_ALL_CHILDREN,
140
                                    DeleteHandlerE4.MOVE_CHILDREN_TO_PARENT_NODE, DeleteHandlerE4.SKIP }, 0);
141
                    int result = dialog.open();
142

    
143
                    if (result == 0){
144
                        //delete all children
145
                        configNodes.setChildHandling(ChildHandling.DELETE);
146
                        config.setTaxonNodeConfig(configNodes);
147
                    } else if (result == 1){
148
                        //move children
149
                        configNodes.setChildHandling(ChildHandling.MOVE_TO_PARENT);
150
                        config.setTaxonNodeConfig(configNodes);
151
                    } else {
152
                        return Status.CANCEL_STATUS;
153
                    }
154
                } else{
155

    
156
                    DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(
157
                            config,
158
                            shell,
159
                            DeleteHandlerE4.CONFIRM_DELETION,
160
                            null,
161
                            confirmMessage,
162
                            MessageDialog.WARNING,  new String[] { DeleteHandlerE4.DELETE, DeleteHandlerE4.SKIP }, 0);
163
                    int result = dialog.open();
164
                    if (result == 0){
165
                        //delete all children
166
                        configNodes.setChildHandling(ChildHandling.DELETE);
167
                        config.setTaxonNodeConfig(configNodes);
168
                    } else {
169
                        return Status.CANCEL_STATUS;
170
                    }
171
//                    if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(configNodes, shell, DeleteHandlerE4.CONFIRM_DELETION, DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S)){
172
//                        return Status.CANCEL_STATUS;
173
//                    }
174
//                    config.setTaxonNodeConfig(configNodes);
175
                }
176
            }
177
//        }
178
        return Status.OK_STATUS;
179
    }
180

    
181
    @Override
182
    public AbstractOperation prepareOperation(IStructuredSelection selection,
183
            Shell shell,
184
            MPart activePart,
185
            MHandledMenuItem menuItem) {
186
        return new RemotingDeleteTaxonNodeOperation(getTrigger(),
187
                false,
188
                treeNodes,
189
                classifications,
190
                config);
191
    }
192

    
193
    @CanExecute
194
    private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
195
        boolean canExecute = false;
196
        Object selectionElement =  selection.getFirstElement();
197

    
198
        canExecute = (selectionElement instanceof TaxonNodeDto) ||
199
                (selectionElement instanceof TaxonNode && ((TaxonNode)selectionElement).getTaxon() == null);
200
        menuItem.setVisible(canExecute);
201
        return canExecute;
202
    }
203

    
204
    @Override
205
    public void onComplete() {
206
    }
207

    
208
    /**
209
     * {@inheritDoc}
210
     */
211
    @Override
212
    protected Object getTrigger() {
213
        return this;
214
    }
215

    
216
}
(10-10/14)