44c865976a7d0ef6b0fd2df0001e49aad3322525
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / handler / NameEditorMenuPropertyTester.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 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
11 package eu.etaxonomy.taxeditor.editor.name.handler;
12
13 import org.eclipse.jface.viewers.IStructuredSelection;
14
15 import eu.etaxonomy.cdm.model.taxon.Synonym;
16 import eu.etaxonomy.cdm.model.taxon.Taxon;
17 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
18 import eu.etaxonomy.taxeditor.editor.EditorUtil;
19 import eu.etaxonomy.taxeditor.editor.Page;
20 import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
21
22 /**
23 * <p>NameEditorMenuPropertyTester class.</p>
24 *
25 * @author n.hoffmann
26 * @created Jun 22, 2010
27 * @version 1.0
28 */
29 public class NameEditorMenuPropertyTester extends org.eclipse.core.expressions.PropertyTester {
30
31 private static final String ACCEPTED = "isAcceptedTaxon";
32 private static final String SYNONYM = "isSynonym";
33 private static final String MISAPPLICATION = "isMisapplication";
34 private static final String TAXONBASE = "isTaxonBase";
35 private static final String CONCEPT = "isConceptRelation";
36 private static final String EMPTY_NAMES = "hasEmptyNames";
37 private static final String ACCEPTED_AND_NO_HOMOTYPIC_SYNONYMS = "isAcceptedAndHasNoHomotypicSynonyms";
38
39 /**
40 * <p>Constructor for NameEditorMenuPropertyTester.</p>
41 */
42 public NameEditorMenuPropertyTester() {
43 }
44
45 /* (non-Javadoc)
46 * @see org.eclipse.core.expressions.IPropertyTester#test(java.lang.Object, java.lang.String, java.lang.Object[], java.lang.Object)
47 */
48 /** {@inheritDoc} */
49 public boolean test(Object receiver, String property, Object[] args,
50 Object expectedValue) {
51
52 if(receiver instanceof IStructuredSelection){
53
54 IStructuredSelection selection = (IStructuredSelection) receiver;
55
56 Object selectedElement = selection.getFirstElement();
57
58 if(ACCEPTED.equals(property)){
59 return isAccepted(selectedElement);
60 }
61 else if(SYNONYM.equals(property)){
62 return isSynonym(selectedElement);
63 }
64 else if(MISAPPLICATION.equals(property)){
65 return isMisapplication(selectedElement);
66 }
67 else if(TAXONBASE.equals(property)){
68 return isTaxonBase(selectedElement);
69 }
70 else if(CONCEPT.equals(property)){
71 return isRelatedConcept(selectedElement);
72 }
73 else if(EMPTY_NAMES.equals(property)){
74 return hasEmptyNames(receiver);
75 }
76 else if(ACCEPTED_AND_NO_HOMOTYPIC_SYNONYMS.equals(property)){
77 return isAcceptedAndHasNoHomotypicSynonyms(selectedElement);
78 }
79 }
80
81 return false;
82
83 }
84
85 private boolean isAcceptedAndHasNoHomotypicSynonyms(Object selectedElement) {
86 if (isAccepted(selectedElement)){
87 Taxon taxon = (Taxon) selectedElement;
88 return taxon.getSynonymsInGroup(taxon.getHomotypicGroup()).isEmpty();
89 }
90 return false;
91 }
92
93 /**
94 * @param receiver
95 * @return
96 */
97 private boolean hasEmptyNames(Object receiver) {
98 TaxonNameEditor editor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(Page.NAME);
99 return editor == null ? false : editor.checkForEmptyNames();
100 }
101
102 private boolean isRelatedConcept(Object selectedElement) {
103 if(selectedElement instanceof Taxon && ((Taxon) selectedElement).isRelatedConcept()){
104 return true;
105 }
106 return false;
107 }
108
109 private boolean isTaxonBase(Object selectedElement) {
110 return (selectedElement instanceof TaxonBase) ? true : false;
111 }
112
113 private boolean isMisapplication(Object selectedElement) {
114 if(selectedElement instanceof Taxon && ((Taxon) selectedElement).isMisapplication()){
115 return true;
116 }
117 return false;
118 }
119
120 private boolean isSynonym(Object selectedElement) {
121 return (selectedElement instanceof Synonym) ? true : false;
122 }
123
124 private boolean isAccepted(Object selectedElement) {
125 return (selectedElement instanceof Taxon && ! ((Taxon) selectedElement).isMisapplication()) ? true : false;
126 }
127 }