1
|
/**
|
2
|
* Copyright (C) 2014 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.editor.view.derivate.operation;
|
10
|
|
11
|
import java.util.UUID;
|
12
|
|
13
|
import org.eclipse.core.commands.ExecutionException;
|
14
|
import org.eclipse.core.commands.operations.IUndoContext;
|
15
|
import org.eclipse.core.runtime.IAdaptable;
|
16
|
import org.eclipse.core.runtime.IProgressMonitor;
|
17
|
import org.eclipse.core.runtime.IStatus;
|
18
|
import org.eclipse.core.runtime.NullProgressMonitor;
|
19
|
import org.eclipse.core.runtime.Status;
|
20
|
import org.eclipse.jface.util.LocalSelectionTransfer;
|
21
|
import org.eclipse.jface.viewers.TreeNode;
|
22
|
|
23
|
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
|
24
|
import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
|
25
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
26
|
import eu.etaxonomy.cdm.model.molecular.DnaSample;
|
27
|
import eu.etaxonomy.cdm.model.molecular.Sequence;
|
28
|
import eu.etaxonomy.cdm.model.molecular.SingleRead;
|
29
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
30
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
31
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
|
32
|
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
|
33
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
34
|
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
|
35
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
36
|
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
|
37
|
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
|
38
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
39
|
|
40
|
/**
|
41
|
* @author pplitzner
|
42
|
* @date Nov 18, 2014
|
43
|
*
|
44
|
*/
|
45
|
public class MoveDerivateOperation extends AbstractPostOperation<CdmBase> {
|
46
|
|
47
|
private final TreeNode draggedNode;
|
48
|
private TreeNode fromNode;
|
49
|
private TreeNode targetNode;
|
50
|
|
51
|
public MoveDerivateOperation(String label, IUndoContext undoContext,
|
52
|
IPostOperationEnabled postOperationEnabled, TreeNode draggedNode, TreeNode targetNode) {
|
53
|
super(label, undoContext, null, postOperationEnabled);
|
54
|
this.draggedNode = draggedNode;
|
55
|
this.targetNode = targetNode;
|
56
|
}
|
57
|
|
58
|
@Override
|
59
|
public IStatus execute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
|
60
|
DerivateView derivateView = null;
|
61
|
if(getPostOperationEnabled() instanceof DerivateView){
|
62
|
derivateView = (DerivateView) getPostOperationEnabled();
|
63
|
}
|
64
|
if(derivateView==null){
|
65
|
MessagingUtils.operationDialog(this, new NullPointerException("Derivative Editor was null"), TaxeditorEditorPlugin.PLUGIN_ID, this.getLabel(), getLabel()); //$NON-NLS-1$
|
66
|
return Status.CANCEL_STATUS;
|
67
|
}
|
68
|
if(derivateView.isDirty()){
|
69
|
MessagingUtils.warningDialog(DerivateView.VIEW_HAS_UNSAVED_CHANGES, this, DerivateView.YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION);
|
70
|
return Status.CANCEL_STATUS;
|
71
|
}
|
72
|
|
73
|
fromNode = draggedNode.getParent();
|
74
|
if(moveTreeNode(draggedNode, fromNode, targetNode)){
|
75
|
//special case if derivative with no parent was moved
|
76
|
if(fromNode==null && draggedNode!=null && draggedNode.getValue()!=null){
|
77
|
derivateView.remove(draggedNode.getValue());
|
78
|
}
|
79
|
//update DerivateView
|
80
|
LocalSelectionTransfer.getTransfer().setSelection(null);
|
81
|
return postExecute(null);
|
82
|
}
|
83
|
else{
|
84
|
MessagingUtils.warningDialog(Messages.MoveDerivateOperation_MOVE_NOT_POSSIBLE, derivateView, String.format(Messages.MoveDerivateOperation_MOVE_TO_NOT_POSSIBLE, derivateView.getLabelProvider().getDerivateText(draggedNode), derivateView.getLabelProvider().getDerivateText(targetNode)));
|
85
|
}
|
86
|
return Status.CANCEL_STATUS;
|
87
|
}
|
88
|
|
89
|
private boolean moveTreeNode(TreeNode draggedNode, TreeNode fromNode, TreeNode targetNode) {
|
90
|
Object draggedNodeValue = draggedNode.getValue();
|
91
|
Object targetNodeValue = targetNode.getValue();
|
92
|
Object fromParentSpecimen = null;
|
93
|
if(fromNode!=null){
|
94
|
fromParentSpecimen = fromNode.getValue();
|
95
|
}
|
96
|
|
97
|
// drag'n'drop for SpecimenOrObservationBase
|
98
|
IOccurrenceService occurrenceService = CdmStore.getService(IOccurrenceService.class);
|
99
|
if(draggedNodeValue instanceof DerivedUnit && targetNodeValue instanceof SpecimenOrObservationBase<?>){
|
100
|
DerivedUnit draggedSpecimen = (DerivedUnit) draggedNodeValue;
|
101
|
SpecimenOrObservationBase<?> targetSpecimen = (SpecimenOrObservationBase<?>) targetNodeValue;
|
102
|
//check if type is a sub derivate type
|
103
|
if(fromNode==null || !fromNode.equals(targetNode)){//don't drag on direct parent node)
|
104
|
UUID fromUuid = null;
|
105
|
if(fromParentSpecimen!=null){
|
106
|
fromUuid = ((SpecimenOrObservationBase<?>)fromParentSpecimen).getUuid();
|
107
|
}
|
108
|
return occurrenceService.moveDerivate(fromUuid,
|
109
|
targetSpecimen.getUuid(),
|
110
|
draggedSpecimen.getUuid()).isOk();
|
111
|
}
|
112
|
}
|
113
|
|
114
|
// drag'n'drop for SingleRead
|
115
|
else if(draggedNodeValue instanceof SingleRead && targetNodeValue instanceof Sequence){
|
116
|
SingleRead singleRead = (SingleRead) draggedNodeValue;
|
117
|
if(fromParentSpecimen instanceof Sequence){
|
118
|
return CdmStore.getService(ISequenceService.class).moveSingleRead(((Sequence)fromParentSpecimen).getUuid(),
|
119
|
((Sequence)targetNodeValue).getUuid(),
|
120
|
singleRead.getUuid()).isOk();
|
121
|
|
122
|
}
|
123
|
}
|
124
|
|
125
|
// drag'n'drop for Sequence
|
126
|
else if(draggedNodeValue instanceof Sequence && targetNodeValue instanceof DnaSample && ((SpecimenOrObservationBase<?>) targetNodeValue).getRecordBasis()==SpecimenOrObservationType.DnaSample){
|
127
|
Sequence sequence = (Sequence)draggedNodeValue;
|
128
|
if(fromParentSpecimen instanceof DnaSample && ((SpecimenOrObservationBase<?>) targetNodeValue).getRecordBasis()==SpecimenOrObservationType.DnaSample){
|
129
|
return occurrenceService.moveSequence(((DnaSample)fromParentSpecimen).getUuid(),
|
130
|
((DnaSample)targetNodeValue).getUuid(),
|
131
|
sequence.getUuid()).isOk();
|
132
|
|
133
|
}
|
134
|
}
|
135
|
return false;
|
136
|
}
|
137
|
|
138
|
@Override
|
139
|
public IStatus redo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
|
140
|
//swap fromNode and targetNode
|
141
|
this.targetNode = this.fromNode;
|
142
|
return execute(new NullProgressMonitor(), null);
|
143
|
}
|
144
|
|
145
|
@Override
|
146
|
public IStatus undo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
|
147
|
//swap fromNode and targetNode
|
148
|
this.targetNode = this.fromNode;
|
149
|
return execute(new NullProgressMonitor(), null);
|
150
|
}
|
151
|
|
152
|
}
|