Project

General

Profile

Download (4.73 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.editor.handler;
2

    
3

    
4
import org.eclipse.core.expressions.PropertyTester;
5
import org.eclipse.jface.util.LocalSelectionTransfer;
6
import org.eclipse.jface.viewers.IStructuredSelection;
7
import org.eclipse.jface.viewers.TreeNode;
8
import org.eclipse.ui.IEditorPart;
9
import org.eclipse.ui.IWorkbenchWindow;
10
import org.eclipse.ui.PlatformUI;
11

    
12
import eu.etaxonomy.cdm.model.molecular.Sequence;
13
import eu.etaxonomy.cdm.model.molecular.SingleRead;
14
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
15
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
16
import eu.etaxonomy.taxeditor.editor.EditorUtil;
17
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
18

    
19

    
20

    
21
/**
22
 * Tests types of specimens to add items to the context menu.
23
 *
24
 * @author pplitzner
25
 * @author BenStoever
26
 */
27
public class SpecimenPropertyTester extends PropertyTester {
28
    private static final String SEQUENCE = "isSequence"; //$NON-NLS-1$
29
    private static final String SINGLE_READ = "isSingleRead"; //$NON-NLS-1$
30
    private static final String IS_SINGLEREAD_REUSABLE_HERE = "isSingleReadReusableHere"; //$NON-NLS-1$
31
    private static final String IS_SINGLEREAD_REUSED = "isSingleReadReused"; //$NON-NLS-1$
32
    private static final String IS_SPECIMEN_OR_OBSERVATION_BASE = "isSpecimenOrObservationBase"; //$NON-NLS-1$
33
    private static final String IS_ADD_DERIVATE_ALLOWED = "isAddDerivateAllowed"; //$NON-NLS-1$
34
    private static final String IS_ADD_MEDIA_ALLOWED = "isAddMediaAllowed"; //$NON-NLS-1$
35

    
36

    
37
    public SpecimenPropertyTester() {}
38

    
39
    /** {@inheritDoc} */
40
    @Override
41
    public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
42
        if (receiver instanceof IStructuredSelection) {
43
            IStructuredSelection selection = (IStructuredSelection) receiver;
44
            TreeNode treeNodeOfSelection = EditorUtil.getTreeNodeOfSelection(selection);
45
            if (treeNodeOfSelection!=null) {
46
                if (SEQUENCE.equals(property)) {
47
                    return isSequence(treeNodeOfSelection.getValue());
48
                }
49
                else if (SINGLE_READ.equals(property)) {
50
                	return isSingleReadAlignment(treeNodeOfSelection.getValue());
51
                }
52
                else if (IS_SINGLEREAD_REUSABLE_HERE.equals(property)) {
53
                    return isSingleReadReusableHere(treeNodeOfSelection.getValue());
54
                }
55
                else if (IS_SINGLEREAD_REUSED.equals(property)) {
56
                    return isSingleReadReused(treeNodeOfSelection.getValue());
57
                }
58
                else if (IS_SPECIMEN_OR_OBSERVATION_BASE.equals(property)) {
59
                    return isSpecimenOrObservationBase(treeNodeOfSelection.getValue());
60
                }
61
                else if (IS_ADD_DERIVATE_ALLOWED.equals(property)) {
62
                    return isAddDerivateAllowed(treeNodeOfSelection.getValue());
63
                }
64
                else if (IS_ADD_MEDIA_ALLOWED.equals(property)) {
65
                    return isAddMediaAllowed(treeNodeOfSelection.getValue());
66
                }
67
            }
68
        }
69
        return false;
70
    }
71

    
72
    private boolean isSingleReadReused(Object value) {
73
        final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
74
        final IEditorPart activeEditor = window.getActivePage().getActiveEditor();
75
        if(value instanceof SingleRead){
76
            if(activeEditor instanceof DerivateView
77
                    && ((DerivateView) activeEditor).getMultiLinkSingleReads().contains(value)){
78
                return true;
79
            }
80
        }
81
        return false;
82
    }
83

    
84
    private boolean isSingleReadReusableHere(Object value) {
85
        TreeNode clipboardNode = EditorUtil.getTreeNodeOfSelection(LocalSelectionTransfer.getTransfer().getSelection());
86
        if(value instanceof Sequence && clipboardNode!=null && clipboardNode.getValue() instanceof SingleRead
87
                && !((Sequence) value).getSingleReads().contains(clipboardNode.getValue())){
88
            return true;
89
        }
90
        return false;
91
    }
92

    
93
    private boolean isSpecimenOrObservationBase(Object object) {
94
    	return (object instanceof SpecimenOrObservationBase);
95
	}
96

    
97
    private boolean isAddDerivateAllowed(Object object) {
98
        return (object instanceof SpecimenOrObservationBase || object instanceof Sequence)
99
                && !(object instanceof MediaSpecimen);
100
    }
101

    
102
    private boolean isAddMediaAllowed(Object object) {
103
        return object instanceof SpecimenOrObservationBase
104
                && !(object instanceof MediaSpecimen);
105
    }
106

    
107
    private boolean isSequence(Object object) {
108
        return (object instanceof Sequence);
109
    }
110

    
111
	private boolean isSingleReadAlignment(Object object) {
112
    	return (object instanceof SingleRead);
113
    }
114
}
(7-7/7)