(no commit message)
[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 System.out.println(selectedElement);
59 System.out.println(property);
60 if(ACCEPTED.equals(property)){
61 return isAccepted(selectedElement);
62 }
63 else if(SYNONYM.equals(property)){
64 System.out.println(isSynonym(selectedElement));
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 }
82 }
83
84 return false;
85
86 }
87
88 private boolean isAcceptedAndHasNoHomotypicSynonyms(Object selectedElement) {
89 if (isAccepted(selectedElement)){
90 Taxon taxon = (Taxon) selectedElement;
91 return taxon.getSynonymsInGroup(taxon.getHomotypicGroup()).isEmpty();
92 }
93 return false;
94 }
95
96 /**
97 * @param receiver
98 * @return
99 */
100 private boolean hasEmptyNames(Object receiver) {
101 TaxonNameEditor editor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(Page.NAME);
102 return editor == null ? false : editor.checkForEmptyNames();
103 }
104
105 private boolean isRelatedConcept(Object selectedElement) {
106 if(selectedElement instanceof Taxon && ((Taxon) selectedElement).isRelatedConcept()){
107 return true;
108 }
109 return false;
110 }
111
112 private boolean isTaxonBase(Object selectedElement) {
113 return (selectedElement instanceof TaxonBase) ? true : false;
114 }
115
116 private boolean isMisapplication(Object selectedElement) {
117 if(selectedElement instanceof Taxon && ((Taxon) selectedElement).isMisapplication()){
118 return true;
119 }
120 return false;
121 }
122
123 private boolean isSynonym(Object selectedElement) {
124 System.out.println("isSynonym...." + selectedElement);
125 return (selectedElement instanceof Synonym) ? true : false;
126 }
127
128 private boolean isAccepted(Object selectedElement) {
129 return (selectedElement instanceof Taxon && ! ((Taxon) selectedElement).isMisapplication()) ? true : false;
130 }
131 }