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 org
.eclipse
.core
.commands
.ExecutionException
;
13 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
14 import org
.eclipse
.core
.runtime
.IAdaptable
;
15 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
16 import org
.eclipse
.core
.runtime
.IStatus
;
17 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
18 import org
.eclipse
.core
.runtime
.Status
;
19 import org
.eclipse
.jface
.util
.LocalSelectionTransfer
;
20 import org
.eclipse
.jface
.viewers
.TreeNode
;
22 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
23 import eu
.etaxonomy
.cdm
.api
.service
.molecular
.ISequenceService
;
24 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
25 import eu
.etaxonomy
.cdm
.model
.molecular
.DnaSample
;
26 import eu
.etaxonomy
.cdm
.model
.molecular
.Sequence
;
27 import eu
.etaxonomy
.cdm
.model
.molecular
.SingleRead
;
28 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
29 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
30 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationType
;
31 import eu
.etaxonomy
.taxeditor
.editor
.internal
.TaxeditorEditorPlugin
;
32 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateView
;
33 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
34 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
35 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
36 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
43 public class MoveDerivateOperation
extends AbstractPostOperation
<CdmBase
> {
45 private final TreeNode draggedNode
;
46 private TreeNode fromNode
;
47 private TreeNode targetNode
;
49 public MoveDerivateOperation(String label
, IUndoContext undoContext
,
50 IPostOperationEnabled postOperationEnabled
, TreeNode draggedNode
, TreeNode targetNode
) {
51 super(label
, undoContext
, null, postOperationEnabled
);
52 this.draggedNode
= draggedNode
;
53 this.targetNode
= targetNode
;
57 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
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());
67 return Status
.CANCEL_STATUS
;
69 if(derivateView
.isDirty()){
70 MessagingUtils
.warningDialog("View has unsaved changes", this, "You need to save before performing this action");
71 return Status
.CANCEL_STATUS
;
74 fromNode
= draggedNode
.getParent();
75 if(moveTreeNode(draggedNode
, fromNode
, targetNode
)){
77 derivateView
.getConversationHolder().commit();
78 LocalSelectionTransfer
.getTransfer().setSelection(null);
79 return postExecute(null);
82 MessagingUtils
.warningDialog("Moving derivatives not possible!", derivateView
, "Moving \""+derivateView
.getLabelProvider().getDerivateText(draggedNode
)+"\" to \""+derivateView
.getLabelProvider().getDerivateText(targetNode
)+"\" is not possible!");
84 return Status
.CANCEL_STATUS
;
87 private boolean moveTreeNode(TreeNode draggedNode
, TreeNode fromNode
, TreeNode targetNode
) {
88 Object draggedNodeValue
= draggedNode
.getValue();
89 Object targetNodeValue
= targetNode
.getValue();
90 Object fromParentSpecimen
= null;
92 fromParentSpecimen
= fromNode
.getValue();
95 // drag'n'drop for SpecimenOrObservationBase
96 IOccurrenceService occurrenceService
= CdmStore
.getService(IOccurrenceService
.class);
97 if(draggedNodeValue
instanceof DerivedUnit
&& targetNodeValue
instanceof SpecimenOrObservationBase
<?
>){
98 DerivedUnit draggedSpecimen
= (DerivedUnit
) draggedNodeValue
;
99 SpecimenOrObservationBase
<?
> targetSpecimen
= (SpecimenOrObservationBase
<?
>) targetNodeValue
;
100 //check if type is a sub derivate type
101 if(fromParentSpecimen
instanceof SpecimenOrObservationBase
<?
>
103 && !fromNode
.equals(targetNode
)){//don't drag on direct parent node)
104 return occurrenceService
.moveDerivate((SpecimenOrObservationBase
<?
>)fromParentSpecimen
, targetSpecimen
, draggedSpecimen
);
108 // drag'n'drop for SingleRead
109 else if(draggedNodeValue
instanceof SingleRead
&& targetNodeValue
instanceof Sequence
){
110 SingleRead singleRead
= (SingleRead
) draggedNodeValue
;
111 if(fromParentSpecimen
instanceof Sequence
){
112 return CdmStore
.getService(ISequenceService
.class).moveSingleRead((Sequence
)fromParentSpecimen
, (Sequence
)targetNodeValue
, singleRead
).isOk();
116 // drag'n'drop for Sequence
117 else if(draggedNodeValue
instanceof Sequence
&& targetNodeValue
instanceof DnaSample
&& ((SpecimenOrObservationBase
<?
>) targetNodeValue
).getRecordBasis()==SpecimenOrObservationType
.DnaSample
){
118 Sequence sequence
= (Sequence
)draggedNodeValue
;
119 if(fromParentSpecimen
instanceof DnaSample
&& ((SpecimenOrObservationBase
<?
>) targetNodeValue
).getRecordBasis()==SpecimenOrObservationType
.DnaSample
){
120 return occurrenceService
.moveSequence((DnaSample
)fromParentSpecimen
, (DnaSample
)targetNodeValue
, sequence
).isOk();
128 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
131 public IStatus
redo(IProgressMonitor monitor
, IAdaptable info
) throws ExecutionException
{
132 //swap fromNode and targetNode
133 this.targetNode
= this.fromNode
;
134 return execute(new NullProgressMonitor(), null);
138 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
141 public IStatus
undo(IProgressMonitor monitor
, IAdaptable info
) throws ExecutionException
{
142 //swap fromNode and targetNode
143 this.targetNode
= this.fromNode
;
144 return execute(new NullProgressMonitor(), null);