Merge branch 'release/4.6.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / handler / SpecimenPropertyTester.java
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.taxeditor.editor.EditorUtil;
15 import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
16
17
18
19 /**
20 * Tests types of specimens to add items to the context menu.
21 *
22 * @author pplitzner
23 * @author BenStoever
24 */
25 public class SpecimenPropertyTester extends PropertyTester {
26 private static final String SEQUENCE = "isSequence"; //$NON-NLS-1$
27 private static final String SINGLE_READ = "isSingleRead"; //$NON-NLS-1$
28 private static final String IS_SINGLEREAD_REUSABLE_HERE = "isSingleReadReusableHere"; //$NON-NLS-1$
29 private static final String IS_SINGLEREAD_REUSED = "isSingleReadReused"; //$NON-NLS-1$
30
31
32 public SpecimenPropertyTester() {}
33
34 /** {@inheritDoc} */
35 @Override
36 public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
37 if (receiver instanceof IStructuredSelection) {
38 IStructuredSelection selection = (IStructuredSelection) receiver;
39 TreeNode treeNodeOfSelection = EditorUtil.getTreeNodeOfSelection(selection);
40 if (treeNodeOfSelection!=null) {
41 if (SEQUENCE.equals(property)) {
42 return isSequence(treeNodeOfSelection.getValue());
43 }
44 else if (SINGLE_READ.equals(property)) {
45 return isSingleReadAlignment(treeNodeOfSelection.getValue());
46 }
47 else if (IS_SINGLEREAD_REUSABLE_HERE.equals(property)) {
48 return isSingleReadReusableHere(treeNodeOfSelection.getValue());
49 }
50 else if (IS_SINGLEREAD_REUSED.equals(property)) {
51 return isSingleReadReused(treeNodeOfSelection.getValue());
52 }
53 }
54 }
55 return false;
56 }
57
58 private boolean isSingleReadReused(Object value) {
59 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
60 final IEditorPart activeEditor = window.getActivePage().getActiveEditor();
61 if(value instanceof SingleRead){
62 if(activeEditor instanceof DerivateView
63 && ((DerivateView) activeEditor).getMultiLinkSingleReads().contains(value)){
64 return true;
65 }
66 }
67 return false;
68 }
69
70 private boolean isSingleReadReusableHere(Object value) {
71 TreeNode clipboardNode = EditorUtil.getTreeNodeOfSelection(LocalSelectionTransfer.getTransfer().getSelection());
72 if(value instanceof Sequence && clipboardNode!=null && clipboardNode.getValue() instanceof SingleRead
73 && !((Sequence) value).getSingleReads().contains(clipboardNode.getValue())){
74 return true;
75 }
76 return false;
77 }
78
79 private boolean isSequence(Object object) {
80 return (object instanceof Sequence);
81 }
82
83 private boolean isSingleReadAlignment(Object object) {
84 return (object instanceof SingleRead);
85 }
86 }