Project

General

Profile

Download (6.75 KB) Statistics
| Branch: | Tag: | Revision:
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.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;
37

    
38
/**
39
 * @author pplitzner
40
 * @date Nov 18, 2014
41
 *
42
 */
43
public class MoveDerivateOperation extends AbstractPostOperation<CdmBase>  {
44

    
45
    private final TreeNode draggedNode;
46
    private TreeNode fromNode;
47
    private TreeNode targetNode;
48

    
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;
54
    }
55

    
56
    /* (non-Javadoc)
57
     * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
58
     */
59
    @Override
60
    public IStatus execute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
61
        DerivateView derivateView = null;
62
        if(getPostOperationEnabled() instanceof DerivateView){
63
            derivateView = (DerivateView) getPostOperationEnabled();
64
        }
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;
68
        }
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;
72
        }
73

    
74
        fromNode = draggedNode.getParent();
75
        if(moveTreeNode(draggedNode, fromNode, targetNode)){
76
            //update DerivateView
77
            derivateView.getConversationHolder().commit();
78
            LocalSelectionTransfer.getTransfer().setSelection(null);
79
            return postExecute(null);
80
        }
81
        else{
82
            MessagingUtils.warningDialog("Moving derivatives not possible!", derivateView, "Moving \""+derivateView.getLabelProvider().getDerivateText(draggedNode)+"\" to \""+derivateView.getLabelProvider().getDerivateText(targetNode)+"\" is not possible!");
83
        }
84
        return Status.CANCEL_STATUS;
85
    }
86

    
87
    private boolean moveTreeNode(TreeNode draggedNode, TreeNode fromNode, TreeNode targetNode) {
88
        Object draggedNodeValue = draggedNode.getValue();
89
        Object targetNodeValue = targetNode.getValue();
90
        Object fromParentSpecimen = null;
91
        if(fromNode!=null){
92
            fromParentSpecimen = fromNode.getValue();
93
        }
94

    
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<?>
102
            && fromNode!=null
103
            && !fromNode.equals(targetNode)){//don't drag on direct parent node)
104
                return occurrenceService.moveDerivate((SpecimenOrObservationBase<?>)fromParentSpecimen, targetSpecimen, draggedSpecimen);
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, (Sequence)targetNodeValue, singleRead);
113
            }
114
        }
115

    
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);
121
            }
122
        }
123
        return false;
124
    }
125

    
126

    
127
    /* (non-Javadoc)
128
     * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
129
     */
130
    @Override
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);
135
    }
136

    
137
    /* (non-Javadoc)
138
     * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
139
     */
140
    @Override
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);
145
    }
146

    
147
}
(2-2/2)