Project

General

Profile

Download (6.15 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.TaxonNavigatorE4;
36
import eu.etaxonomy.taxeditor.navigation.navigator.e4.TreeNodeDropAdapterE4.MovingType;
37
import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingMoveTaxonOperation;
38
import eu.etaxonomy.taxeditor.operation.e4.RemotingCdmHandlerE4;
39
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
40
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
41

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

    
49
    private Set<UUID> oldTaxonNodeUUIDs = new HashSet<>();
50
    private TaxonNavigatorE4 navigator;
51

    
52
    public RemotingMoveTaxonNodeHandlerE4() {
53
        super(TaxonNavigatorLabels.MOVE_TAXON_LABEL);
54
    }
55

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

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

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

    
99
        if (PreferencesUtil.getSortNodesNaturally()){
100

    
101

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

    
126

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

    
135
            return new RemotingMoveTaxonOperation(getTrigger(),
136
                    false,
137
                    oldTaxonNodeUUIDs,
138
                    parentTaxonNode.getUuid(),
139
                    moveToNewParent);
140
        }
141

    
142
        return null;
143
    }
144

    
145
    @CanExecute
146
    private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
147
        boolean canExecute = false;
148
        canExecute = selection.size()==1
149
                && selection.getFirstElement() instanceof TaxonNodeDto
150
                && ((TaxonNodeDto)selection.getFirstElement()).getTaxonUuid() != null;
151
        menuItem.setVisible(canExecute);
152
        return canExecute;
153
    }
154

    
155
    @Override
156
    public void onComplete() {
157
    }
158

    
159
    /**
160
     * {@inheritDoc}
161
     */
162
    @Override
163
    protected Object getTrigger() {
164
        return this;
165
    }
166
}
(12-12/14)