Project

General

Profile

Download (6.23 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2015 EDIT
4
 * European Distributed Institute of Taxonomy
5
 * http://www.e-taxonomy.eu
6
 *
7
 * The contents of this file are subject to the Mozilla Public License Version 1.1
8
 * See LICENSE.TXT at the top of this package for the full license terms.
9
 */
10
package eu.etaxonomy.taxeditor.navigation.navigator.handler;
11

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

    
16
import org.eclipse.core.commands.ExecutionEvent;
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.jface.dialogs.MessageDialog;
21
import org.eclipse.jface.viewers.TreeSelection;
22
import org.eclipse.ui.IWorkbenchPage;
23
import org.eclipse.ui.handlers.HandlerUtil;
24

    
25
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
26
import eu.etaxonomy.cdm.api.service.config.NodeDeletionConfigurator.ChildHandling;
27
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
28
import eu.etaxonomy.cdm.api.service.config.TaxonNodeDeletionConfigurator;
29
import eu.etaxonomy.cdm.model.taxon.Classification;
30
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
31
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
32
import eu.etaxonomy.taxeditor.editor.EditorUtil;
33
import eu.etaxonomy.taxeditor.model.MessagingUtils;
34
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
35
import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingDeleteTaxonNodeOperation;
36
import eu.etaxonomy.taxeditor.operation.RemotingCdmHandler;
37
import eu.etaxonomy.taxeditor.ui.dialog.deleteConfigurator.DeleteConfiguratorDialog;
38

    
39
/**
40
 * @author cmathew
41
 * @date 22 Jun 2015
42
 *
43
 */
44
public class RemotingDeleteTaxonNodeHandler extends RemotingCdmHandler {
45

    
46
    private TaxonDeletionConfigurator config;
47
    private Set<ITaxonTreeNode> treeNodes;
48

    
49
    public RemotingDeleteTaxonNodeHandler() {
50
        super(TaxonNavigatorLabels.DELETE_TAXON_NODE_LABEL);
51
    }
52

    
53
    @Override
54
    public IStatus allowOperations(ExecutionEvent event) {
55
        TreeSelection selection = (TreeSelection) HandlerUtil.getCurrentSelection(event);
56

    
57
        Iterator<?> selectionIterator = selection.iterator();
58
        treeNodes = new HashSet<ITaxonTreeNode>();
59

    
60
        while (selectionIterator.hasNext()){
61
            Object object = selectionIterator.next();
62
            if(object instanceof ITaxonTreeNode) {
63
                treeNodes.add((ITaxonTreeNode) object);
64
            }
65
        }
66
        boolean allEditorsClosed = true;
67
        IWorkbenchPage activePage = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
68
        for (ITaxonTreeNode treeNode : treeNodes) {
69
            if(treeNode instanceof TaxonNode) {
70
                allEditorsClosed &= EditorUtil.closeObsoleteEditor((TaxonNode) treeNode, activePage);
71
            }
72
        }
73
        if(!allEditorsClosed) {
74
            return new Status(IStatus.WARNING,
75
                    "unknown",
76
                    TaxonNavigatorLabels.RELATED_EDITOR_NOT_CLOSED_MESSAGE);
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 Classification){
85
                taxonNode = CdmApplicationState.getCurrentAppConfig().getClassificationService().load(taxonNode.getUuid());
86
            }else{
87
                taxonNode = CdmApplicationState.getCurrentAppConfig().getTaxonNodeService().load(taxonNode.getUuid());
88
            }
89
            if (taxonNode == null){
90
            	MessagingUtils.informationDialog("Node already deleted", "The taxon node was already deleted. Please reopen the taxon navigator to refresh the view.");
91
            	return Status.CANCEL_STATUS;
92

    
93
            }
94
            TaxonNodeDeletionConfigurator configNodes = new TaxonNodeDeletionConfigurator();
95
			if (taxonNode instanceof Classification) {
96
				String message;
97
				if (taxonNode.hasChildNodes()) {
98
					message = "Do you really want to delete the classification? The tree has children, they will be deleted, too.";
99
				} else {
100
					message = "Do you really want to delete the classification?";
101
				}
102
				if (!DeleteConfiguratorDialog.openConfirm(
103
						HandlerUtil.getActiveShell(event), "Confirm Deletion",
104
						message)) {
105
					return Status.CANCEL_STATUS;
106
				}
107
			}
108
            else {
109

    
110
                if (taxonNode.hasChildNodes()){
111
                    DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(
112
                            config,
113
                            HandlerUtil.getActiveShell(event),
114
                            "Confirm Deletion",
115
                            null,
116
                            "Do you really want to delete the selected node? It has childnodes, they will be deleted, too.",
117
                            MessageDialog.WARNING, new String[] { "Delete all children",
118
                                "Move children to parent node", "Skip" }, 0);
119
                    int result = dialog.open();
120

    
121
                    if (result == 0){
122
                        //delete all children
123
                        configNodes.setChildHandling(ChildHandling.DELETE);
124
                        config.setTaxonNodeConfig(configNodes);
125
                    } else if (result == 1){
126
                        //move children
127
                        configNodes.setChildHandling(ChildHandling.MOVE_TO_PARENT);
128
                        config.setTaxonNodeConfig(configNodes);
129
                    } else if (result == 2){
130
                        return Status.CANCEL_STATUS;
131
                    }
132
                } else{
133
                    if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(configNodes, HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected node?")){
134
                        return Status.CANCEL_STATUS;
135
                    }
136
                    config.setTaxonNodeConfig(configNodes);
137
                }
138
            }
139
        }
140
        return Status.OK_STATUS;
141
    }
142

    
143
    @Override
144
    public AbstractOperation prepareOperation(ExecutionEvent event) {
145
        return new RemotingDeleteTaxonNodeOperation(event.getTrigger(),
146
                false,
147
                treeNodes,
148
                config);
149
    }
150

    
151
    @Override
152
    public void onComplete() {
153
    }
154

    
155
}
(12-12/15)