270d9aa2dc3b01bb73ecd504624230f5bc087ac1
[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 private static final String HOMOTYPIC_SYNONYM_OF_ACCEPTED = "isHomotypicSynonymOfAcceptedTaxon";
39
40
41 /**
42 * <p>Constructor for NameEditorMenuPropertyTester.</p>
43 */
44 public NameEditorMenuPropertyTester() {
45 }
46
47 /* (non-Javadoc)
48 * @see org.eclipse.core.expressions.IPropertyTester#test(java.lang.Object, java.lang.String, java.lang.Object[], java.lang.Object)
49 */
50 /** {@inheritDoc} */
51 @Override
52 public boolean test(Object receiver, String property, Object[] args,
53 Object expectedValue) {
54
55 if(receiver instanceof IStructuredSelection){
56
57 IStructuredSelection selection = (IStructuredSelection) receiver;
58
59 Object selectedElement = selection.getFirstElement();
60
61 if(ACCEPTED.equals(property)){
62 return isAccepted(selectedElement);
63 }
64 else if(SYNONYM.equals(property)){
65 return isSynonym(selectedElement);
66 }
67 else if(MISAPPLICATION.equals(property)){
68 return isMisapplication(selectedElement);
69 }
70 else if(TAXONBASE.equals(property)){
71 return isTaxonBase(selectedElement);
72 }
73 else if(CONCEPT.equals(property)){
74 return isRelatedConcept(selectedElement);
75 }
76 else if(EMPTY_NAMES.equals(property)){
77 return hasEmptyNames(receiver);
78 }
79 else if(ACCEPTED_AND_NO_HOMOTYPIC_SYNONYMS.equals(property)){
80 return isAcceptedAndHasNoHomotypicSynonyms(selectedElement);
81 }else if (HOMOTYPIC_SYNONYM_OF_ACCEPTED.equals(property)){
82 return isHomotypicSynonymOfAcceptedTaxon(selectedElement);
83 }
84
85 }
86
87 return false;
88
89 }
90
91 private boolean isAcceptedAndHasNoHomotypicSynonyms(Object selectedElement) {
92 if (isAccepted(selectedElement)){
93 Taxon taxon = (Taxon) selectedElement;
94 return taxon.getSynonymsInGroup(taxon.getHomotypicGroup()).isEmpty();
95 }
96 return false;
97 }
98
99 private boolean isHomotypicSynonymOfAcceptedTaxon(Object selectedElement) {
100 if (isSynonym(selectedElement)){
101 Synonym synonym = (Synonym) selectedElement;
102 for (Taxon taxon:synonym.getAcceptedTaxa()){
103 if (taxon.getHomotypicGroup().equals(synonym.getHomotypicGroup())){
104 return false;
105 }
106 }
107 }
108 return true;
109 }
110
111
112 /**
113 * @param receiver
114 * @return
115 */
116 private boolean hasEmptyNames(Object receiver) {
117 TaxonNameEditor editor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(Page.NAME);
118 return editor == null ? false : editor.checkForEmptyNames();
119 }
120
121 private boolean isRelatedConcept(Object selectedElement) {
122 if(selectedElement instanceof Taxon && ((Taxon) selectedElement).isRelatedConcept()){
123 return true;
124 }
125 return false;
126 }
127
128 private boolean isTaxonBase(Object selectedElement) {
129 return (selectedElement instanceof TaxonBase) ? true : false;
130 }
131
132 private boolean isMisapplication(Object selectedElement) {
133 if(selectedElement instanceof Taxon && ((Taxon) selectedElement).isMisapplication()){
134 return true;
135 }
136 return false;
137 }
138
139 private boolean isSynonym(Object selectedElement) {
140 return (selectedElement instanceof Synonym) ? true : false;
141 }
142
143 private boolean isAccepted(Object selectedElement) {
144 return (selectedElement instanceof Taxon && ! ((Taxon) selectedElement).isMisapplication()) ? true : false;
145 }
146 }