Project

General

Profile

Download (7.9 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.application.CdmApplicationState;
30
import eu.etaxonomy.cdm.api.service.config.NodeDeletionConfigurator.ChildHandling;
31
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
32
import eu.etaxonomy.cdm.api.service.config.TaxonNodeDeletionConfigurator;
33
import eu.etaxonomy.cdm.model.taxon.Classification;
34
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
35
import eu.etaxonomy.cdm.model.taxon.Synonym;
36
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
37
import eu.etaxonomy.taxeditor.editor.EditorUtil;
38
import eu.etaxonomy.taxeditor.model.MessagingUtils;
39
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
40
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
41
import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingDeleteTaxonNodeOperation;
42
import eu.etaxonomy.taxeditor.operation.e4.RemotingCdmHandlerE4;
43
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
44

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

    
52
    private TaxonDeletionConfigurator config;
53
    private Set<ITaxonTreeNode> treeNodes;
54

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

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

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

    
68
        while (selectionIterator.hasNext()){
69
            Object object = selectionIterator.next();
70
            if(object instanceof ITaxonTreeNode) {
71
                treeNodes.add((ITaxonTreeNode) object);
72
            }
73
        }
74
        for (ITaxonTreeNode treeNode : treeNodes) {
75
            if(treeNode instanceof TaxonNode) {
76
                EditorUtil.closeObsoleteEditor((TaxonNode) treeNode, partService);
77
            }
78
        }
79

    
80
        config = new TaxonDeletionConfigurator();
81

    
82
        if (treeNodes.size() == 1 ){
83
        	ITaxonTreeNode treeNode = treeNodes.iterator().next();
84
            ITaxonTreeNode taxonNode = treeNode;
85
            if (taxonNode instanceof TaxonNode && !((TaxonNode)taxonNode).hasTaxon()){
86
                taxonNode = CdmApplicationState.getCurrentAppConfig().getClassificationService().load(((TaxonNode)taxonNode).getClassification().getUuid());
87
            }else{
88
                taxonNode = CdmApplicationState.getCurrentAppConfig().getTaxonNodeService().load(taxonNode.getUuid());
89
            }
90
            treeNodes = new HashSet<ITaxonTreeNode>();
91
            treeNodes.add(taxonNode);
92
            if (taxonNode == null){
93
            	MessagingUtils.informationDialog(Messages.RemotingDeleteTaxonNodeHandler_NODE_DELETED, Messages.RemotingDeleteTaxonNodeHandler_NODE_DELETED_MESSAGE);
94
            	return Status.CANCEL_STATUS;
95

    
96
            }
97
            TaxonNodeDeletionConfigurator configNodes = new TaxonNodeDeletionConfigurator();
98
			if (taxonNode instanceof Classification) {
99
				String message;
100
				if (((Classification)taxonNode).getRootNode() != null &&taxonNode.hasChildNodes()) {
101
					message = DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION+DeleteHandlerE4.THE_TREE_HAS_CHILDREN_THEY_WILL_BE_DELETED_TOO;
102
				} else {
103
					message = DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION;
104
				}
105
				if (!MessageDialog.openConfirm(
106
						shell, DeleteHandlerE4.CONFIRM_DELETION,
107
						message)) {
108
					return Status.CANCEL_STATUS;
109
				}
110
			}
111
            else {
112

    
113
                if (taxonNode.hasChildNodes()){
114
                    DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(
115
                            config,
116
                            shell,
117
                            DeleteHandlerE4.CONFIRM_DELETION,
118
                            null,
119
                            DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S+DeleteHandlerE4.THERE_ARE_CHILDNODES_WHICH_WILL_BE_DELETED_TOO,
120
                            MessageDialog.WARNING, new String[] { DeleteHandlerE4.DELETE_ALL_CHILDREN,
121
                                    DeleteHandlerE4.MOVE_CHILDREN_TO_PARENT_NODE, DeleteHandlerE4.SKIP }, 0);
122
                    int result = dialog.open();
123

    
124
                    if (result == 0){
125
                        //delete all children
126
                        configNodes.setChildHandling(ChildHandling.DELETE);
127
                        config.setTaxonNodeConfig(configNodes);
128
                    } else if (result == 1){
129
                        //move children
130
                        configNodes.setChildHandling(ChildHandling.MOVE_TO_PARENT);
131
                        config.setTaxonNodeConfig(configNodes);
132
                    } else if (result == 2){
133
                        return Status.CANCEL_STATUS;
134
                    }
135
                } else{
136

    
137
                    DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(
138
                            config,
139
                            shell,
140
                            DeleteHandlerE4.CONFIRM_DELETION,
141
                            null,
142
                            DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S,
143
                            MessageDialog.WARNING,  new String[] { DeleteHandlerE4.DELETE, DeleteHandlerE4.SKIP }, 0);
144
                    int result = dialog.open();
145
                    if (result == 0){
146
                        //delete all children
147
                        configNodes.setChildHandling(ChildHandling.DELETE);
148
                        config.setTaxonNodeConfig(configNodes);
149
                    } else {
150
                        return Status.CANCEL_STATUS;
151
                    }
152
//                    if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(configNodes, shell, DeleteHandlerE4.CONFIRM_DELETION, DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S)){
153
//                        return Status.CANCEL_STATUS;
154
//                    }
155
//                    config.setTaxonNodeConfig(configNodes);
156
                }
157
            }
158
        }
159
        return Status.OK_STATUS;
160
    }
161

    
162
    @Override
163
    public AbstractOperation prepareOperation(IStructuredSelection selection,
164
            Shell shell,
165
            MPart activePart,
166
            MHandledMenuItem menuItem) {
167
        return new RemotingDeleteTaxonNodeOperation(getTrigger(),
168
                false,
169
                treeNodes,
170
                config);
171
    }
172

    
173
    @CanExecute
174
    private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
175
        boolean canExecute = false;
176
        canExecute = !selection.isEmpty() && !(selection.getFirstElement() instanceof Synonym);
177
        menuItem.setVisible(canExecute);
178
        return canExecute;
179
    }
180

    
181
    @Override
182
    public void onComplete() {
183
    }
184

    
185
    /**
186
     * {@inheritDoc}
187
     */
188
    @Override
189
    protected Object getTrigger() {
190
        return this;
191
    }
192

    
193
}
(13-13/17)