93107ebfe0781ea3402d489115f7d4ef7a5410e4
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / derivate / operation / MoveDerivateOperation.java
1 // $Id$
2 /**
3 * Copyright (C) 2014 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10 package eu.etaxonomy.taxeditor.editor.view.derivate.operation;
11
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;
21
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.Messages;
32 import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
33 import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
34 import eu.etaxonomy.taxeditor.model.MessagingUtils;
35 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
36 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
37 import eu.etaxonomy.taxeditor.store.CdmStore;
38
39 /**
40 * @author pplitzner
41 * @date Nov 18, 2014
42 *
43 */
44 public class MoveDerivateOperation extends AbstractPostOperation<CdmBase> {
45
46 private final TreeNode draggedNode;
47 private TreeNode fromNode;
48 private TreeNode targetNode;
49
50 public MoveDerivateOperation(String label, IUndoContext undoContext,
51 IPostOperationEnabled postOperationEnabled, TreeNode draggedNode, TreeNode targetNode) {
52 super(label, undoContext, null, postOperationEnabled);
53 this.draggedNode = draggedNode;
54 this.targetNode = targetNode;
55 }
56
57 @Override
58 public IStatus execute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
59 DerivateView derivateView = null;
60 if(getPostOperationEnabled() instanceof DerivateView){
61 derivateView = (DerivateView) getPostOperationEnabled();
62 }
63 if(derivateView==null){
64 MessagingUtils.operationDialog(this, new NullPointerException("Derivative Editor was null"), TaxeditorEditorPlugin.PLUGIN_ID, this.getLabel(), getLabel()); //$NON-NLS-1$
65 return Status.CANCEL_STATUS;
66 }
67 if(derivateView.isDirty()){
68 MessagingUtils.warningDialog(DerivateView.VIEW_HAS_UNSAVED_CHANGES, this, DerivateView.YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION);
69 return Status.CANCEL_STATUS;
70 }
71
72 fromNode = draggedNode.getParent();
73 if(moveTreeNode(draggedNode, fromNode, targetNode)){
74 //update DerivateView
75 derivateView.getConversationHolder().commit();
76 LocalSelectionTransfer.getTransfer().setSelection(null);
77 return postExecute(null);
78 }
79 else{
80 MessagingUtils.warningDialog(Messages.MoveDerivateOperation_MOVE_NOT_POSSIBLE, derivateView, String.format(Messages.MoveDerivateOperation_MOVE_TO_NOT_POSSIBLE, derivateView.getLabelProvider().getDerivateText(draggedNode), derivateView.getLabelProvider().getDerivateText(targetNode)));
81 }
82 return Status.CANCEL_STATUS;
83 }
84
85 private boolean moveTreeNode(TreeNode draggedNode, TreeNode fromNode, TreeNode targetNode) {
86 Object draggedNodeValue = draggedNode.getValue();
87 Object targetNodeValue = targetNode.getValue();
88 Object fromParentSpecimen = null;
89 if(fromNode!=null){
90 fromParentSpecimen = fromNode.getValue();
91 }
92
93 // drag'n'drop for SpecimenOrObservationBase
94 IOccurrenceService occurrenceService = CdmStore.getService(IOccurrenceService.class);
95 if(draggedNodeValue instanceof DerivedUnit && targetNodeValue instanceof SpecimenOrObservationBase<?>){
96 DerivedUnit draggedSpecimen = (DerivedUnit) draggedNodeValue;
97 SpecimenOrObservationBase<?> targetSpecimen = (SpecimenOrObservationBase<?>) targetNodeValue;
98 //check if type is a sub derivate type
99 if(fromParentSpecimen instanceof SpecimenOrObservationBase<?>
100 && fromNode!=null
101 && !fromNode.equals(targetNode)){//don't drag on direct parent node)
102 return occurrenceService.moveDerivate(((SpecimenOrObservationBase<?>)fromParentSpecimen).getUuid(),
103 targetSpecimen.getUuid(),
104 draggedSpecimen.getUuid()).isOk();
105 }
106 }
107
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).getUuid(),
113 ((Sequence)targetNodeValue).getUuid(),
114 singleRead.getUuid()).isOk();
115
116 }
117 }
118
119 // drag'n'drop for Sequence
120 else if(draggedNodeValue instanceof Sequence && targetNodeValue instanceof DnaSample && ((SpecimenOrObservationBase<?>) targetNodeValue).getRecordBasis()==SpecimenOrObservationType.DnaSample){
121 Sequence sequence = (Sequence)draggedNodeValue;
122 if(fromParentSpecimen instanceof DnaSample && ((SpecimenOrObservationBase<?>) targetNodeValue).getRecordBasis()==SpecimenOrObservationType.DnaSample){
123 return occurrenceService.moveSequence(((DnaSample)fromParentSpecimen).getUuid(),
124 ((DnaSample)targetNodeValue).getUuid(),
125 sequence.getUuid()).isOk();
126
127 }
128 }
129 return false;
130 }
131
132 @Override
133 public IStatus redo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
134 //swap fromNode and targetNode
135 this.targetNode = this.fromNode;
136 return execute(new NullProgressMonitor(), null);
137 }
138
139 @Override
140 public IStatus undo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
141 //swap fromNode and targetNode
142 this.targetNode = this.fromNode;
143 return execute(new NullProgressMonitor(), null);
144 }
145
146 }