3 * Copyright (C) 2014 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.operation
;
12 import java
.util
.UUID
;
14 import org
.eclipse
.core
.commands
.ExecutionException
;
15 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
16 import org
.eclipse
.core
.runtime
.IAdaptable
;
17 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
18 import org
.eclipse
.core
.runtime
.IStatus
;
19 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
20 import org
.eclipse
.core
.runtime
.Status
;
21 import org
.eclipse
.jface
.util
.LocalSelectionTransfer
;
22 import org
.eclipse
.jface
.viewers
.TreeNode
;
24 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
25 import eu
.etaxonomy
.cdm
.api
.service
.molecular
.ISequenceService
;
26 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
27 import eu
.etaxonomy
.cdm
.model
.molecular
.DnaSample
;
28 import eu
.etaxonomy
.cdm
.model
.molecular
.Sequence
;
29 import eu
.etaxonomy
.cdm
.model
.molecular
.SingleRead
;
30 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
31 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
32 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationType
;
33 import eu
.etaxonomy
.taxeditor
.editor
.Messages
;
34 import eu
.etaxonomy
.taxeditor
.editor
.internal
.TaxeditorEditorPlugin
;
35 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateView
;
36 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
37 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
38 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
39 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
46 public class MoveDerivateOperation
extends AbstractPostOperation
<CdmBase
> {
48 private final TreeNode draggedNode
;
49 private TreeNode fromNode
;
50 private TreeNode targetNode
;
52 public MoveDerivateOperation(String label
, IUndoContext undoContext
,
53 IPostOperationEnabled postOperationEnabled
, TreeNode draggedNode
, TreeNode targetNode
) {
54 super(label
, undoContext
, null, postOperationEnabled
);
55 this.draggedNode
= draggedNode
;
56 this.targetNode
= targetNode
;
60 public IStatus
execute(IProgressMonitor monitor
, IAdaptable info
) throws ExecutionException
{
61 DerivateView derivateView
= null;
62 if(getPostOperationEnabled() instanceof DerivateView
){
63 derivateView
= (DerivateView
) getPostOperationEnabled();
65 if(derivateView
==null){
66 MessagingUtils
.operationDialog(this, new NullPointerException("Derivative Editor was null"), TaxeditorEditorPlugin
.PLUGIN_ID
, this.getLabel(), getLabel()); //$NON-NLS-1$
67 return Status
.CANCEL_STATUS
;
69 if(derivateView
.isDirty()){
70 MessagingUtils
.warningDialog(DerivateView
.VIEW_HAS_UNSAVED_CHANGES
, this, DerivateView
.YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION
);
71 return Status
.CANCEL_STATUS
;
74 fromNode
= draggedNode
.getParent();
75 if(moveTreeNode(draggedNode
, fromNode
, targetNode
)){
76 //special case if derivative with no parent was moved
77 if(fromNode
==null && draggedNode
!=null && draggedNode
.getValue()!=null){
78 derivateView
.remove(draggedNode
.getValue());
81 derivateView
.getConversationHolder().commit();
82 LocalSelectionTransfer
.getTransfer().setSelection(null);
83 return postExecute(null);
86 MessagingUtils
.warningDialog(Messages
.MoveDerivateOperation_MOVE_NOT_POSSIBLE
, derivateView
, String
.format(Messages
.MoveDerivateOperation_MOVE_TO_NOT_POSSIBLE
, derivateView
.getLabelProvider().getDerivateText(draggedNode
), derivateView
.getLabelProvider().getDerivateText(targetNode
)));
88 return Status
.CANCEL_STATUS
;
91 private boolean moveTreeNode(TreeNode draggedNode
, TreeNode fromNode
, TreeNode targetNode
) {
92 Object draggedNodeValue
= draggedNode
.getValue();
93 Object targetNodeValue
= targetNode
.getValue();
94 Object fromParentSpecimen
= null;
96 fromParentSpecimen
= fromNode
.getValue();
99 // drag'n'drop for SpecimenOrObservationBase
100 IOccurrenceService occurrenceService
= CdmStore
.getService(IOccurrenceService
.class);
101 if(draggedNodeValue
instanceof DerivedUnit
&& targetNodeValue
instanceof SpecimenOrObservationBase
<?
>){
102 DerivedUnit draggedSpecimen
= (DerivedUnit
) draggedNodeValue
;
103 SpecimenOrObservationBase
<?
> targetSpecimen
= (SpecimenOrObservationBase
<?
>) targetNodeValue
;
104 //check if type is a sub derivate type
105 if(fromNode
==null || !fromNode
.equals(targetNode
)){//don't drag on direct parent node)
106 UUID fromUuid
= null;
107 if(fromParentSpecimen
!=null){
108 fromUuid
= ((SpecimenOrObservationBase
<?
>)fromParentSpecimen
).getUuid();
110 return occurrenceService
.moveDerivate(fromUuid
,
111 targetSpecimen
.getUuid(),
112 draggedSpecimen
.getUuid()).isOk();
116 // drag'n'drop for SingleRead
117 else if(draggedNodeValue
instanceof SingleRead
&& targetNodeValue
instanceof Sequence
){
118 SingleRead singleRead
= (SingleRead
) draggedNodeValue
;
119 if(fromParentSpecimen
instanceof Sequence
){
120 return CdmStore
.getService(ISequenceService
.class).moveSingleRead(((Sequence
)fromParentSpecimen
).getUuid(),
121 ((Sequence
)targetNodeValue
).getUuid(),
122 singleRead
.getUuid()).isOk();
127 // drag'n'drop for Sequence
128 else if(draggedNodeValue
instanceof Sequence
&& targetNodeValue
instanceof DnaSample
&& ((SpecimenOrObservationBase
<?
>) targetNodeValue
).getRecordBasis()==SpecimenOrObservationType
.DnaSample
){
129 Sequence sequence
= (Sequence
)draggedNodeValue
;
130 if(fromParentSpecimen
instanceof DnaSample
&& ((SpecimenOrObservationBase
<?
>) targetNodeValue
).getRecordBasis()==SpecimenOrObservationType
.DnaSample
){
131 return occurrenceService
.moveSequence(((DnaSample
)fromParentSpecimen
).getUuid(),
132 ((DnaSample
)targetNodeValue
).getUuid(),
133 sequence
.getUuid()).isOk();
141 public IStatus
redo(IProgressMonitor monitor
, IAdaptable info
) throws ExecutionException
{
142 //swap fromNode and targetNode
143 this.targetNode
= this.fromNode
;
144 return execute(new NullProgressMonitor(), null);
148 public IStatus
undo(IProgressMonitor monitor
, IAdaptable info
) throws ExecutionException
{
149 //swap fromNode and targetNode
150 this.targetNode
= this.fromNode
;
151 return execute(new NullProgressMonitor(), null);