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
|
private UUID classificationUuid = null;
|
52
|
|
53
|
public RemotingMoveTaxonNodeHandlerE4() {
|
54
|
super(TaxonNavigatorLabels.MOVE_TAXON_LABEL);
|
55
|
}
|
56
|
|
57
|
@Override
|
58
|
public IStatus allowOperations(IStructuredSelection selection,
|
59
|
Shell shell,
|
60
|
MPart activePart,
|
61
|
MHandledMenuItem menuItem) {
|
62
|
// check for no taxon tree node selected
|
63
|
if(selection.size() == 0) {
|
64
|
return new Status(IStatus.ERROR,
|
65
|
"unknown", //$NON-NLS-1$
|
66
|
TaxonNavigatorLabels.NO_TAXON_SELECTION_MESSAGE);
|
67
|
}
|
68
|
// check that selected object is a taxon node
|
69
|
|
70
|
Object obj ;
|
71
|
Iterator iter = selection.iterator();
|
72
|
oldTaxonNodeUUIDs = new HashSet<>();
|
73
|
while (iter.hasNext()){
|
74
|
obj = iter.next();
|
75
|
if(obj instanceof TaxonNodeDto) {
|
76
|
oldTaxonNodeUUIDs.add(((TaxonNodeDto)obj).getUuid());
|
77
|
if (classificationUuid == null){
|
78
|
classificationUuid = ((TaxonNodeDto)obj).getClassificationUUID();
|
79
|
}
|
80
|
} else {
|
81
|
return new Status(IStatus.ERROR,
|
82
|
"unknown", //$NON-NLS-1$
|
83
|
TaxonNavigatorLabels.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE);
|
84
|
}
|
85
|
}
|
86
|
return Status.OK_STATUS;
|
87
|
}
|
88
|
|
89
|
@Override
|
90
|
public AbstractOperation prepareOperation(IStructuredSelection selection,
|
91
|
Shell shell,
|
92
|
MPart activePart,
|
93
|
MHandledMenuItem menuItem) {
|
94
|
TaxonNode parentTaxonNode;
|
95
|
MovingType moveToNewParent = MovingType.CHILD;
|
96
|
|
97
|
if (PreferencesUtil.isNodesSortedNaturally()){
|
98
|
parentTaxonNode = TaxonNodeSelectionDialog.select(shell,
|
99
|
Messages.RemotingMoveTaxonNodeHandler_CHOOSE_TAXON,
|
100
|
oldTaxonNodeUUIDs,
|
101
|
null,
|
102
|
classificationUuid, true);
|
103
|
String[] buttonLables = {Messages.RemotingMoveTaxonNodeHandler_CHILD, Messages.RemotingMoveTaxonNodeHandler_BEHIND,Messages.RemotingMoveTaxonNodeHandler_CANCEL};
|
104
|
MessageDialog dialog = new MessageDialog(null, Messages.RemotingMoveTaxonNodeHandler_TARGET_NODE, null, Messages.RemotingMoveTaxonNodeHandler_TARGET_NODE_MESSAGE, MessageDialog.QUESTION_WITH_CANCEL, buttonLables, 0);
|
105
|
dialog.open();
|
106
|
int returnCode = dialog.getReturnCode();
|
107
|
if (returnCode == 0){
|
108
|
moveToNewParent = MovingType.CHILD;
|
109
|
} else if (returnCode == 1){
|
110
|
moveToNewParent = MovingType.BEHIND;
|
111
|
}
|
112
|
} else {
|
113
|
parentTaxonNode = TaxonNodeSelectionDialog.select(shell,
|
114
|
Messages.RemotingMoveTaxonNodeHandler_CHOOSE_PARENT,
|
115
|
oldTaxonNodeUUIDs,
|
116
|
null,
|
117
|
classificationUuid, true);
|
118
|
}
|
119
|
|
120
|
|
121
|
if(parentTaxonNode != null){
|
122
|
if(NavigationUtil.isDirty(parentTaxonNode, partService)){
|
123
|
MessageDialog.openWarning(shell,
|
124
|
Messages.RemotingMoveTaxonNodeHandler_UNSAVED_PARENT,
|
125
|
Messages.RemotingMoveTaxonNodeHandler_UNSAVED_PARENT_MESSAGE);
|
126
|
return null;
|
127
|
}
|
128
|
|
129
|
return new RemotingMoveTaxonOperation(getTrigger(),
|
130
|
false,
|
131
|
oldTaxonNodeUUIDs,
|
132
|
parentTaxonNode.getUuid(),
|
133
|
moveToNewParent);
|
134
|
}
|
135
|
|
136
|
return null;
|
137
|
}
|
138
|
|
139
|
@CanExecute
|
140
|
private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
|
141
|
boolean canExecute = false;
|
142
|
canExecute = selection.getFirstElement() instanceof TaxonNodeDto
|
143
|
&& ((TaxonNodeDto)selection.getFirstElement()).getTaxonUuid() != null;
|
144
|
menuItem.setVisible(canExecute);
|
145
|
return canExecute;
|
146
|
}
|
147
|
|
148
|
@Override
|
149
|
public void onComplete() {
|
150
|
}
|
151
|
|
152
|
/**
|
153
|
* {@inheritDoc}
|
154
|
*/
|
155
|
@Override
|
156
|
protected Object getTrigger() {
|
157
|
return this;
|
158
|
}
|
159
|
}
|