Project

General

Profile

Download (5.74 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.taxeditor.editor.name.handler;
11

    
12
import org.eclipse.core.expressions.PropertyTester;
13
import org.eclipse.jface.viewers.IStructuredSelection;
14

    
15
import eu.etaxonomy.cdm.model.common.CdmBase;
16
import eu.etaxonomy.cdm.model.taxon.Synonym;
17
import eu.etaxonomy.cdm.model.taxon.Taxon;
18
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
19
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
20
import eu.etaxonomy.taxeditor.editor.EditorUtil;
21
import eu.etaxonomy.taxeditor.editor.Page;
22
import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
23
import eu.etaxonomy.taxeditor.store.CdmStore;
24

    
25
/**
26
 * <p>NameEditorMenuPropertyTester class.</p>
27
 *
28
 * @author n.hoffmann
29
 * @created Jun 22, 2010
30
 * @version 1.0
31
 */
32
public class NameEditorMenuPropertyTester extends PropertyTester {
33

    
34
	private static final String ACCEPTED = "isAcceptedTaxon"; //$NON-NLS-1$
35
	private static final String SYNONYM = "isSynonym"; //$NON-NLS-1$
36
	private static final String MISAPPLICATION = "isMisapplication"; //$NON-NLS-1$
37
	private static final String TAXONBASE = "isTaxonBase"; //$NON-NLS-1$
38
	private static final String CONCEPT = "isConceptRelation"; //$NON-NLS-1$
39
	private static final String EMPTY_NAMES = "hasEmptyNames"; //$NON-NLS-1$
40
	private static final String ACCEPTED_AND_NO_HOMOTYPIC_SYNONYMS = "isAcceptedAndHasNoHomotypicSynonyms"; //$NON-NLS-1$
41
	private static final String NOT_HOMOTYPIC_SYNONYM_OF_ACCEPTED = "isNotHomotypicSynonymOfAcceptedTaxon"; //$NON-NLS-1$
42
	private static final String SYNONYM_WITH_HOMOTYPIC_SYNONYMS = "isSynonymInHomotypicalGroupWithMoreSynonyms"; //$NON-NLS-1$
43
	private static final String IS_ORPHANED = "isOrphaned"; //$NON-NLS-1$
44

    
45
	/**
46
	 * <p>Constructor for NameEditorMenuPropertyTester.</p>
47
	 */
48
	public NameEditorMenuPropertyTester() {
49
	}
50

    
51
	/** {@inheritDoc} */
52
	@Override
53
    public boolean test(Object receiver, String property, Object[] args,
54
			Object expectedValue) {
55

    
56
		if(receiver instanceof IStructuredSelection){
57

    
58
			IStructuredSelection selection = (IStructuredSelection) receiver;
59

    
60
			Object selectedElement = selection.getFirstElement();
61
			if(selectedElement instanceof UuidAndTitleCache){
62
			    UuidAndTitleCache uuidAndTitleCache = (UuidAndTitleCache<?>)selectedElement;
63
			    CdmBase cdmBase = CdmStore.getCommonService().find(uuidAndTitleCache.getType(), uuidAndTitleCache.getUuid());
64
			    selectedElement = cdmBase;
65
			}
66

    
67
			if(ACCEPTED.equals(property)){
68
				return isAccepted(selectedElement);
69
			}
70
			else if(SYNONYM.equals(property)){
71
				return isSynonym(selectedElement);
72
			}
73
			else if(MISAPPLICATION.equals(property)){
74
				return isMisapplication(selectedElement);
75
			}
76
			else if(TAXONBASE.equals(property)){
77
				return isTaxonBase(selectedElement);
78
			}
79
			else if(CONCEPT.equals(property)){
80
				return isRelatedConcept(selectedElement);
81
			}
82
			else if(EMPTY_NAMES.equals(property)){
83
				return hasEmptyNames(receiver);
84
			}
85
			else if(ACCEPTED_AND_NO_HOMOTYPIC_SYNONYMS.equals(property)){
86
				return isAcceptedAndHasNoHomotypicSynonyms(selectedElement);
87
			}
88
			else if (NOT_HOMOTYPIC_SYNONYM_OF_ACCEPTED.equals(property)){
89
				return isNotHomotypicSynonymOfAcceptedTaxon(selectedElement);
90
			}
91
			else if (SYNONYM_WITH_HOMOTYPIC_SYNONYMS.equals(property)){
92
				return isSynonymInHomotypicalGroupWithMoreSynonyms(selectedElement);
93
			}
94
			else if (IS_ORPHANED.equals(property)){
95
			    return isOrphaned(selectedElement);
96
			}
97

    
98
		}
99

    
100
		return false;
101

    
102
	}
103

    
104
	private boolean isAcceptedAndHasNoHomotypicSynonyms(Object selectedElement) {
105
		if (isAccepted(selectedElement)){
106
			Taxon taxon = (Taxon) selectedElement;
107
			return taxon.getSynonymsInGroup(taxon.getHomotypicGroup()).isEmpty();
108
		}
109
		return false;
110
	}
111

    
112
	private boolean isSynonymInHomotypicalGroupWithMoreSynonyms(Object selectedElement) {
113
		if (isSynonym(selectedElement)){
114
			Synonym synonym = (Synonym) selectedElement;
115
			return synonym.getHomotypicGroup().getTypifiedNames().size()>1;
116
		}
117
		return false;
118
	}
119

    
120

    
121
	private boolean isNotHomotypicSynonymOfAcceptedTaxon(Object selectedElement) {
122
		if (isSynonym(selectedElement)){
123
			Synonym synonym = (Synonym) selectedElement;
124
			Taxon taxon = synonym.getAcceptedTaxon();
125
			if (taxon != null && 
126
					taxon.getHomotypicGroup().equals(synonym.getHomotypicGroup())){
127
				return false;
128
			}
129
		}
130
		return true;
131
	}
132

    
133

    
134
	/**
135
	 * @param receiver
136
	 * @return
137
	 */
138
	private boolean hasEmptyNames(Object receiver) {
139
		TaxonNameEditor editor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(Page.NAME);
140
		return editor == null ? false : editor.checkForEmptyNames();
141
	}
142

    
143
	private boolean isRelatedConcept(Object selectedElement) {
144
		if(selectedElement instanceof Taxon && ((Taxon) selectedElement).isRelatedConcept()){
145
			return true;
146
		}
147
		return false;
148
	}
149

    
150
	private boolean isTaxonBase(Object selectedElement) {
151
		return (selectedElement instanceof TaxonBase) ? true : false;
152
	}
153

    
154
	private boolean isMisapplication(Object selectedElement) {
155
		if(selectedElement instanceof Taxon && ((Taxon) selectedElement).isMisapplication()){
156
			return true;
157
		}
158
		return false;
159
	}
160

    
161
	private boolean isSynonym(Object selectedElement) {
162
		return (selectedElement instanceof Synonym) ? true : false;
163
	}
164

    
165
	private boolean isAccepted(Object selectedElement) {
166
		return (selectedElement instanceof Taxon  && ! ((Taxon) selectedElement).isMisapplication()) ? true : false;
167
	}
168

    
169
	private boolean isOrphaned(Object selectedElement) {
170
	    if(selectedElement instanceof Taxon){
171
	        return ((Taxon) selectedElement).isOrphaned();
172
	    }
173
	    else if(selectedElement instanceof Synonym){
174
	        return ((Synonym) selectedElement).isOrphaned();
175
	    }
176
	    return false;
177
	}
178
}
(12-12/15)