Project

General

Profile

Download (6.95 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.TreeSelection;
26
import org.eclipse.swt.widgets.Shell;
27

    
28
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
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.ITaxonTreeNode;
34
import eu.etaxonomy.cdm.model.taxon.Synonym;
35
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
36
import eu.etaxonomy.taxeditor.editor.EditorUtil;
37
import eu.etaxonomy.taxeditor.model.MessagingUtils;
38
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
39
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
40
import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingDeleteTaxonNodeOperation;
41
import eu.etaxonomy.taxeditor.operation.e4.RemotingCdmHandlerE4;
42
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
43

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

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

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

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

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

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

    
79
        config = new TaxonDeletionConfigurator();
80

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

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

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

    
123
                    if (result == 0){
124
                        //delete all children
125
                        configNodes.setChildHandling(ChildHandling.DELETE);
126
                        config.setTaxonNodeConfig(configNodes);
127
                    } else if (result == 1){
128
                        //move children
129
                        configNodes.setChildHandling(ChildHandling.MOVE_TO_PARENT);
130
                        config.setTaxonNodeConfig(configNodes);
131
                    } else if (result == 2){
132
                        return Status.CANCEL_STATUS;
133
                    }
134
                } else{
135
                    if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(configNodes, shell, DeleteHandlerE4.CONFIRM_DELETION, DeleteHandlerE4.DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S)){
136
                        return Status.CANCEL_STATUS;
137
                    }
138
                    config.setTaxonNodeConfig(configNodes);
139
                }
140
            }
141
        }
142
        return Status.OK_STATUS;
143
    }
144

    
145
    @Override
146
    public AbstractOperation prepareOperation(TreeSelection selection,
147
            Shell shell,
148
            MPart activePart,
149
            MHandledMenuItem menuItem) {
150
        return new RemotingDeleteTaxonNodeOperation(getTrigger(),
151
                false,
152
                treeNodes,
153
                config);
154
    }
155

    
156
    @CanExecute
157
    private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
158
        boolean canExecute = false;
159
        canExecute = !selection.isEmpty() && !(selection.getFirstElement() instanceof Synonym);
160
        menuItem.setVisible(canExecute);
161
        return canExecute;
162
    }
163

    
164
    @Override
165
    public void onComplete() {
166
    }
167

    
168
    /**
169
     * {@inheritDoc}
170
     */
171
    @Override
172
    protected Object getTrigger() {
173
        return this;
174
    }
175

    
176
}
(12-12/15)