Project

General

Profile

Download (8.41 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.model.taxon.Classification;
32
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
33
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
34
import eu.etaxonomy.taxeditor.editor.EditorUtil;
35
import eu.etaxonomy.taxeditor.model.MessagingUtils;
36
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
37
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
38
import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingDeleteTaxonNodeOperation;
39
import eu.etaxonomy.taxeditor.operation.e4.RemotingCdmHandlerE4;
40
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
41

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

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

    
53
    private Set<TaxonNode> taxonNodes;
54
    private Set<Classification> classificationList;
55

    
56
    public RemotingDeleteTaxonNodeHandlerE4() {
57
        super(TaxonNavigatorLabels.DELETE_TAXON_NODE_LABEL);
58
    }
59

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

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

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

    
87
        config = new TaxonDeletionConfigurator();
88
        Iterator it = treeNodes.iterator();
89

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

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

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

    
107

    
108
        }
109

    
110
        for (TaxonNodeDto rootNode: classifications){
111

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

    
116
        }
117

    
118
			if (containsClassification && !containsTaxonNodes) {
119
				String message;
120
				if (containsClassification && containsNodeWithChildren) {
121
					message = DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION+DeleteHandlerE4.THE_TREE_HAS_CHILDREN_THEY_WILL_BE_DELETED_TOO;
122
				} else {
123
					message = DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION;
124
				}
125
				if (!MessageDialog.openConfirm(
126
						shell, DeleteHandlerE4.CONFIRM_DELETION,
127
						message)) {
128
					return Status.CANCEL_STATUS;
129
				}
130
			}
131
            else {
132
            	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;
133
                if (containsNodeWithChildren){
134

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

    
145
                    if (result == 0){
146
                        //delete all children
147
                        config.getTaxonNodeConfig().setChildHandling(ChildHandling.DELETE);
148
                    } else if (result == 1){
149
                        //move children
150
                        config.getTaxonNodeConfig().setChildHandling(ChildHandling.MOVE_TO_PARENT);
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

    
167
                        config.getTaxonNodeConfig().setChildHandling(ChildHandling.DELETE);
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
        canExecute = selection.size()==1
197
                && (selection.getFirstElement() instanceof TaxonNodeDto)
198
                ||
199
                (selection.getFirstElement() instanceof TaxonNode
200
                        && ((TaxonNode)selection.getFirstElement()).getTaxon() == null);
201
        menuItem.setVisible(canExecute);
202
        return canExecute;
203
    }
204

    
205
    @Override
206
    public void onComplete() {
207

    
208
//        CdmApplicationState.getCurrentDataChangeService()
209
//        .fireChangeEvent(new CdmChangeEvent(Action.Delete, treeNodes, this), true);
210
    }
211

    
212
    /**
213
     * {@inheritDoc}
214
     */
215
    @Override
216
    protected Object getTrigger() {
217
        return this;
218
    }
219

    
220
}
(12-12/24)