2 * Copyright (C) 2014 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.operation
;
11 import java
.util
.UUID
;
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
;
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
.Messages
;
33 import eu
.etaxonomy
.taxeditor
.editor
.internal
.TaxeditorEditorPlugin
;
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
;
45 public class MoveDerivateOperation
extends AbstractPostOperation
<CdmBase
> {
47 private final TreeNode draggedNode
;
48 private TreeNode fromNode
;
49 private TreeNode targetNode
;
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
;
59 public IStatus
execute(IProgressMonitor monitor
, IAdaptable info
) throws ExecutionException
{
60 DerivateView derivateView
= null;
61 if(getPostOperationEnabled() instanceof DerivateView
){
62 derivateView
= (DerivateView
) getPostOperationEnabled();
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
;
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
;
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());
80 derivateView
.getConversationHolder().commit();
81 LocalSelectionTransfer
.getTransfer().setSelection(null);
82 return postExecute(null);
85 MessagingUtils
.warningDialog(Messages
.MoveDerivateOperation_MOVE_NOT_POSSIBLE
, derivateView
, String
.format(Messages
.MoveDerivateOperation_MOVE_TO_NOT_POSSIBLE
, derivateView
.getLabelProvider().getDerivateText(draggedNode
), derivateView
.getLabelProvider().getDerivateText(targetNode
)));
87 return Status
.CANCEL_STATUS
;
90 private boolean moveTreeNode(TreeNode draggedNode
, TreeNode fromNode
, TreeNode targetNode
) {
91 Object draggedNodeValue
= draggedNode
.getValue();
92 Object targetNodeValue
= targetNode
.getValue();
93 Object fromParentSpecimen
= null;
95 fromParentSpecimen
= fromNode
.getValue();
98 // drag'n'drop for SpecimenOrObservationBase
99 IOccurrenceService occurrenceService
= CdmStore
.getService(IOccurrenceService
.class);
100 if(draggedNodeValue
instanceof DerivedUnit
&& targetNodeValue
instanceof SpecimenOrObservationBase
<?
>){
101 DerivedUnit draggedSpecimen
= (DerivedUnit
) draggedNodeValue
;
102 SpecimenOrObservationBase
<?
> targetSpecimen
= (SpecimenOrObservationBase
<?
>) targetNodeValue
;
103 //check if type is a sub derivate type
104 if(fromNode
==null || !fromNode
.equals(targetNode
)){//don't drag on direct parent node)
105 UUID fromUuid
= null;
106 if(fromParentSpecimen
!=null){
107 fromUuid
= ((SpecimenOrObservationBase
<?
>)fromParentSpecimen
).getUuid();
109 return occurrenceService
.moveDerivate(fromUuid
,
110 targetSpecimen
.getUuid(),
111 draggedSpecimen
.getUuid()).isOk();
115 // drag'n'drop for SingleRead
116 else if(draggedNodeValue
instanceof SingleRead
&& targetNodeValue
instanceof Sequence
){
117 SingleRead singleRead
= (SingleRead
) draggedNodeValue
;
118 if(fromParentSpecimen
instanceof Sequence
){
119 return CdmStore
.getService(ISequenceService
.class).moveSingleRead(((Sequence
)fromParentSpecimen
).getUuid(),
120 ((Sequence
)targetNodeValue
).getUuid(),
121 singleRead
.getUuid()).isOk();
126 // drag'n'drop for Sequence
127 else if(draggedNodeValue
instanceof Sequence
&& targetNodeValue
instanceof DnaSample
&& ((SpecimenOrObservationBase
<?
>) targetNodeValue
).getRecordBasis()==SpecimenOrObservationType
.DnaSample
){
128 Sequence sequence
= (Sequence
)draggedNodeValue
;
129 if(fromParentSpecimen
instanceof DnaSample
&& ((SpecimenOrObservationBase
<?
>) targetNodeValue
).getRecordBasis()==SpecimenOrObservationType
.DnaSample
){
130 return occurrenceService
.moveSequence(((DnaSample
)fromParentSpecimen
).getUuid(),
131 ((DnaSample
)targetNodeValue
).getUuid(),
132 sequence
.getUuid()).isOk();
140 public IStatus
redo(IProgressMonitor monitor
, IAdaptable info
) throws ExecutionException
{
141 //swap fromNode and targetNode
142 this.targetNode
= this.fromNode
;
143 return execute(new NullProgressMonitor(), null);
147 public IStatus
undo(IProgressMonitor monitor
, IAdaptable info
) throws ExecutionException
{
148 //swap fromNode and targetNode
149 this.targetNode
= this.fromNode
;
150 return execute(new NullProgressMonitor(), null);