Project

General

Profile

Download (4.6 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 *
3
 */
4
package eu.etaxonomy.taxeditor.navigation.navigator.handler;
5

    
6
import java.util.ArrayList;
7
import java.util.HashSet;
8
import java.util.Iterator;
9
import java.util.List;
10
import java.util.Set;
11
import java.util.UUID;
12

    
13
import org.apache.log4j.Logger;
14
import org.eclipse.core.commands.ExecutionEvent;
15
import org.eclipse.core.runtime.IStatus;
16
import org.eclipse.core.runtime.Status;
17
import org.eclipse.jface.viewers.TreeSelection;
18
import org.eclipse.ui.handlers.HandlerUtil;
19

    
20
import eu.etaxonomy.cdm.api.conversation.ConversationHolderMock;
21
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
22
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
23
import eu.etaxonomy.taxeditor.operation.RemotingCdmHandler;
24
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
25

    
26
/**
27
 * <p>ChangeAcceptedTaxonToSynonymHandler class.</p>
28
 *
29
 * @author n.hoffmann
30
 * @created Jan 4, 2010
31
 * @version 1.0
32
 */
33
public class RemotingChangeAcceptedTaxonToSynonymHandler extends RemotingCdmHandler {
34

    
35
    private static final Logger logger = Logger
36
            .getLogger(RemotingChangeAcceptedTaxonToSynonymHandler.class);
37

    
38
    private static final String CHANGE_ACCEPTED_TAXON_TO_SYNONYM_LABEL = "Change Accepted Taxon to Synonym";
39
    private static final String SINGLE_TAXON_SELECTION_MESSAGE = "The operation move accepted taxon to synonymy is available only for a single taxon.";
40
    private static final String SOURCE_TAXON_HAS_CHILDREN_MESSAGE = "The accepted taxon must not have any childen. You need to move all childen to " +
41
            "another taxon node in the TaxonNavigator before attempting to turn the accepted " +
42
            "taxon into a synonym.";
43
    private static final String RELATED_EDITORS_NOT_CLOSED_MESSAGE = "Could not close related taxon anme editors. " +
44
            "Please close them manually.";
45

    
46
    private ITaxonTreeNode oldAcceptedTaxonNode;
47
	/**
48
     * @param label
49
     */
50
    public RemotingChangeAcceptedTaxonToSynonymHandler() {
51
        super(CHANGE_ACCEPTED_TAXON_TO_SYNONYM_LABEL);
52
    }
53
    /* (non-Javadoc)
54
     * @see eu.etaxonomy.taxeditor.operation.RemotingCdmHandler#allowOperations(org.eclipse.core.commands.ExecutionEvent)
55
     */
56
    @Override
57
    public IStatus allowOperations(ExecutionEvent event) {
58
        TreeSelection selection = (TreeSelection) HandlerUtil.getCurrentSelection(event);
59
        // check that only a single taxon has been selected
60
        if(selection.size() > 0) {
61
            return new Status(IStatus.ERROR,
62
                    "unknown",
63
                    SINGLE_TAXON_SELECTION_MESSAGE);
64
        }
65

    
66
        Iterator selectionIterator = selection.iterator();
67
        Set<ITaxonTreeNode> treeNodes = new HashSet<ITaxonTreeNode>();
68
        ITaxonTreeNode treeNode = treeNodes.iterator().next();
69
        oldAcceptedTaxonNode = treeNode;
70

    
71
        // check that the source taxon node does not have children
72
        if(((TaxonNode)oldAcceptedTaxonNode).getCountChildren() > 0) {
73
            return new Status(IStatus.ERROR,
74
                    "unknown",
75
                    SOURCE_TAXON_HAS_CHILDREN_MESSAGE);
76

    
77
        }
78

    
79
        // check all editors related to this taxon node are closed
80
        boolean allEditorsClosed = true;
81
        for (ITaxonTreeNode tn : treeNodes){
82
            if(treeNode instanceof TaxonNode) {
83
                allEditorsClosed &= NavigatorHandlerUtils.closeObsoleteEditor(event, (TaxonNode) tn);
84
            }
85
        }
86
        if(allEditorsClosed != true) {
87
            return new Status(IStatus.ERROR,
88
                    "unknown",
89
                    RELATED_EDITORS_NOT_CLOSED_MESSAGE);
90
        }
91

    
92
        return Status.OK_STATUS;
93
    }
94
    /* (non-Javadoc)
95
     * @see eu.etaxonomy.taxeditor.operation.RemotingCdmHandler#doOperations(org.eclipse.core.commands.ExecutionEvent)
96
     */
97
    @Override
98
    public IStatus doOperations(ExecutionEvent event) {
99
        List<UUID> excludeTaxa = new ArrayList<UUID>();
100
        excludeTaxa.add(oldAcceptedTaxonNode.getUuid());
101
        TaxonNode newAcceptedTaxonNode = TaxonNodeSelectionDialog.select(HandlerUtil.getActiveShell(event),
102
                new ConversationHolderMock(),
103
                "Choose the accepted taxon",
104
                excludeTaxa,
105
                null,
106
                ((TaxonNode)oldAcceptedTaxonNode).getClassification());
107

    
108
        if (newAcceptedTaxonNode == null) {
109
            return null;
110
        }
111

    
112
        // run operation
113
        return Status.OK_STATUS;
114
    }
115
    /* (non-Javadoc)
116
     * @see eu.etaxonomy.taxeditor.operation.RemotingCdmHandler#postOperations(org.eclipse.core.commands.ExecutionEvent)
117
     */
118
    @Override
119
    public void postOperations(ExecutionEvent event) {
120
        // TODO Auto-generated method stub
121

    
122
    }
123

    
124

    
125
}
(12-12/13)