Project

General

Profile

Download (5.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
import java.util.UUID;
15

    
16
import javax.inject.Named;
17

    
18
import org.eclipse.core.commands.operations.AbstractOperation;
19
import org.eclipse.core.runtime.IStatus;
20
import org.eclipse.core.runtime.Status;
21
import org.eclipse.e4.core.di.annotations.CanExecute;
22
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
23
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
24
import org.eclipse.e4.ui.services.IServiceConstants;
25
import org.eclipse.jface.dialogs.MessageDialog;
26
import org.eclipse.jface.viewers.IStructuredSelection;
27
import org.eclipse.jface.viewers.TreeSelection;
28
import org.eclipse.swt.widgets.Shell;
29

    
30
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
31
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
32
import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
33
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
34
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
35
import eu.etaxonomy.taxeditor.navigation.navigator.e4.TreeNodeDropAdapterE4.MovingType;
36
import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingMoveTaxonOperation;
37
import eu.etaxonomy.taxeditor.operation.e4.RemotingCdmHandlerE4;
38
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
39
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
40

    
41
/**
42
 * @author cmathew
43
 * @date 19 Jun 2015
44
 *
45
 */
46
public class RemotingMoveTaxonNodeHandlerE4 extends RemotingCdmHandlerE4 {
47

    
48
    private Set<UUID> oldTaxonNodeUUIDs = new HashSet<>();
49

    
50
    public RemotingMoveTaxonNodeHandlerE4() {
51
        super(TaxonNavigatorLabels.MOVE_TAXON_LABEL);
52
    }
53

    
54
    @Override
55
    public IStatus allowOperations(IStructuredSelection selection,
56
            Shell shell,
57
            MPart activePart,
58
            MHandledMenuItem menuItem) {
59
        // check that only a single taxon tree node has been selected
60
//        if(selection.size() > 1) {
61
//            return new Status(IStatus.ERROR,
62
//                    "unknown", //$NON-NLS-1$
63
//                    TaxonNavigatorLabels.SINGLE_TAXON_SELECTION_MESSAGE);
64
//        }
65

    
66
        // check for no taxon tree node selected
67
        if(selection.size() == 0) {
68
            return new Status(IStatus.ERROR,
69
                    "unknown", //$NON-NLS-1$
70
                    TaxonNavigatorLabels.NO_TAXON_SELECTION_MESSAGE);
71
        }
72
        // check that selected object is a taxon node
73
        Object obj ;
74
        Iterator iter = selection.iterator();
75
        while (iter.hasNext()){
76
            obj = iter.next();
77
            if(obj instanceof TaxonNodeDto) {
78
                oldTaxonNodeUUIDs.add(((TaxonNodeDto)obj).getUuid());
79
            } else {
80
                return new Status(IStatus.ERROR,
81
                        "unknown", //$NON-NLS-1$
82
                        TaxonNavigatorLabels.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE);
83
            }
84
        }
85
        return Status.OK_STATUS;
86
    }
87

    
88
    @Override
89
    public AbstractOperation prepareOperation(IStructuredSelection selection,
90
            Shell shell,
91
            MPart activePart,
92
            MHandledMenuItem menuItem) {
93
        TaxonNode parentTaxonNode;
94
        MovingType moveToNewParent = MovingType.CHILD;
95

    
96
        if (PreferencesUtil.getSortNodesNaturally()){
97

    
98

    
99
            parentTaxonNode = TaxonNodeSelectionDialog.select(shell,
100
//                    new ConversationHolderMock(),
101
                    Messages.RemotingMoveTaxonNodeHandler_CHOOSE_TAXON,
102
                    oldTaxonNodeUUIDs,
103
                    null,
104
                    null, true);
105
            String[] buttonLables = {Messages.RemotingMoveTaxonNodeHandler_CHILD, Messages.RemotingMoveTaxonNodeHandler_BEHIND,Messages.RemotingMoveTaxonNodeHandler_CANCEL};
106
            MessageDialog dialog = new MessageDialog(null, Messages.RemotingMoveTaxonNodeHandler_TARGET_NODE, null, Messages.RemotingMoveTaxonNodeHandler_TARGET_NODE_MESSAGE, MessageDialog.QUESTION_WITH_CANCEL, buttonLables, 0);
107
            dialog.open();
108
            int returnCode = dialog.getReturnCode();
109
            if (returnCode == 0){
110
                moveToNewParent = MovingType.CHILD;
111
            } else if (returnCode == 1){
112
                moveToNewParent = MovingType.BEHIND;
113
            }
114
        } else {
115
            parentTaxonNode = TaxonNodeSelectionDialog.select(shell,
116
//                    new ConversationHolderMock(),
117
                    Messages.RemotingMoveTaxonNodeHandler_CHOOSE_PARENT,
118
                    oldTaxonNodeUUIDs,
119
                    null,
120
                    null, true);
121
        }
122

    
123

    
124
        if(parentTaxonNode != null){
125
            if(NavigationUtil.isDirty(parentTaxonNode, partService)){
126
                MessageDialog.openWarning(shell,
127
                        Messages.RemotingMoveTaxonNodeHandler_UNSAVED_PARENT,
128
                        Messages.RemotingMoveTaxonNodeHandler_UNSAVED_PARENT_MESSAGE);
129
                return null;
130
            }
131

    
132
            return new RemotingMoveTaxonOperation(getTrigger(),
133
                    false,
134
                    oldTaxonNodeUUIDs,
135
                    parentTaxonNode.getUuid(),
136
                    moveToNewParent);
137
        }
138

    
139
        return null;
140
    }
141

    
142
    @CanExecute
143
    private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
144
        boolean canExecute = false;
145
        canExecute = (selection.getFirstElement() instanceof TaxonNodeDto) && ((TaxonNodeDto)selection.getFirstElement()).getTaxonUuid()  != null;
146
        menuItem.setVisible(canExecute);
147
        return canExecute;
148
    }
149

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

    
154
    /**
155
     * {@inheritDoc}
156
     */
157
    @Override
158
    protected Object getTrigger() {
159
        return this;
160
    }
161
}
(12-12/14)