Project

General

Profile

Download (5.29 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.ArrayList;
13
import java.util.List;
14
import java.util.UUID;
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.swt.widgets.Shell;
23
import org.eclipse.ui.handlers.HandlerUtil;
24

    
25
import eu.etaxonomy.cdm.api.conversation.ConversationHolderMock;
26
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
27
import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
28
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
29
import eu.etaxonomy.taxeditor.navigation.navigator.TreeNodeDropAdapter.MovingType;
30
import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingMoveTaxonOperation;
31
import eu.etaxonomy.taxeditor.operation.RemotingCdmHandler;
32
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
33
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
34

    
35
/**
36
 * @author cmathew
37
 * @date 19 Jun 2015
38
 *
39
 */
40
public class RemotingMoveTaxonNodeHandler extends RemotingCdmHandler {
41

    
42
    private TaxonNode oldTaxonNode;
43

    
44
    public RemotingMoveTaxonNodeHandler() {
45
        super(TaxonNavigatorLabels.MOVE_TAXON_LABEL);
46
    }
47

    
48
    /* (non-Javadoc)
49
     * @see eu.etaxonomy.taxeditor.operation.RemotingCdmHandler#allowOperations(org.eclipse.core.commands.ExecutionEvent)
50
     */
51
    @Override
52
    public IStatus allowOperations(ExecutionEvent event) {
53
        TreeSelection selection = (TreeSelection) HandlerUtil.getCurrentSelection(event);
54
        // check that only a single taxon tree node has been selected
55
        if(selection.size() > 1) {
56
            return new Status(IStatus.ERROR,
57
                    "unknown",
58
                    TaxonNavigatorLabels.SINGLE_TAXON_SELECTION_MESSAGE);
59
        }
60

    
61
        // check for no taxon tree node selected
62
        if(selection.size() == 0) {
63
            return new Status(IStatus.ERROR,
64
                    "unknown",
65
                    TaxonNavigatorLabels.NO_TAXON_SELECTION_MESSAGE);
66
        }
67
        // check that selected object is a taxon node
68
        Object obj = selection.iterator().next();
69
        if(obj instanceof TaxonNode) {
70
            oldTaxonNode = (TaxonNode)obj;
71
        } else {
72
            return new Status(IStatus.ERROR,
73
                    "unknown",
74
                    TaxonNavigatorLabels.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE);
75
        }
76
        return Status.OK_STATUS;
77
    }
78

    
79

    
80
    /* (non-Javadoc)
81
     * @see eu.etaxonomy.taxeditor.operation.RemotingCdmHandler#prepareOperation(org.eclipse.core.commands.ExecutionEvent)
82
     */
83
    @Override
84
    public AbstractOperation prepareOperation(ExecutionEvent event) {
85
        Shell activeShell = HandlerUtil.getActiveShell(event);
86
        TaxonNode parentTaxonNode;
87

    
88
        List<UUID> excludeTaxa = new ArrayList<UUID>();
89
        excludeTaxa.add(oldTaxonNode.getTaxon().getUuid());
90

    
91
        MovingType moveToNewParent = MovingType.CHILD;
92

    
93
        if (PreferencesUtil.getSortNodesNaturally()){
94

    
95

    
96
            parentTaxonNode = TaxonNodeSelectionDialog.select(activeShell,
97
                    new ConversationHolderMock(),
98
                    "Choose the taxon above the moved taxon.",
99
                    excludeTaxa,
100
                    null,
101
                    oldTaxonNode.getClassification());
102
            String[] buttonLables = {"Child", "Behind","Cancel"};
103
            MessageDialog dialog = new MessageDialog(null, "Target node", null, "Do you want to move the Taxonnode as child or behind the target node.", MessageDialog.QUESTION_WITH_CANCEL, buttonLables, 0);
104
            dialog.open();
105
            int returnCode = dialog.getReturnCode();
106
            if (returnCode == 0){
107
                moveToNewParent = MovingType.CHILD;
108
            } else if (returnCode == 1){
109
                moveToNewParent = MovingType.BEHIND;
110
            }
111
        } else {
112
            parentTaxonNode = TaxonNodeSelectionDialog.select(activeShell,
113
                    new ConversationHolderMock(),
114
                    "Choose new parent",
115
                    excludeTaxa,
116
                    null,
117
                    oldTaxonNode.getClassification());
118
        }
119

    
120

    
121
        if(parentTaxonNode != null){
122
            if(NavigationUtil.isDirty(parentTaxonNode)){
123
                MessageDialog.openWarning(activeShell,
124
                        "Unsaved Parent Taxon",
125
                        "There are unsaved changes in the parent taxon. Please save first.");
126
                return null;
127
            }
128

    
129
            return new RemotingMoveTaxonOperation(event.getTrigger(),
130
                    false,
131
                    oldTaxonNode.getUuid(),
132
                    parentTaxonNode.getUuid(),
133
                    moveToNewParent);
134
        }
135

    
136
        return null;
137
    }
138

    
139
    /* (non-Javadoc)
140
     * @see eu.etaxonomy.taxeditor.operation.RemotingCdmHandler#onComplete()
141
     */
142
    @Override
143
    public void onComplete() {
144
        // TODO Auto-generated method stub
145

    
146
    }
147

    
148
}
(15-15/16)