including changes from cdmlib-print
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / section / name / NameDetailElement.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.section.name;
12
13 import java.util.Arrays;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.SWTException;
17
18 import eu.etaxonomy.cdm.model.name.BotanicalName;
19 import eu.etaxonomy.cdm.model.name.NonViralName;
20 import eu.etaxonomy.cdm.model.name.Rank;
21 import eu.etaxonomy.cdm.model.name.ZoologicalName;
22 import eu.etaxonomy.cdm.strategy.parser.ParserProblem;
23 import eu.etaxonomy.taxeditor.editor.EditorUtil;
24 import eu.etaxonomy.taxeditor.editor.Page;
25 import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
26 import eu.etaxonomy.taxeditor.forms.CdmFormFactory;
27 import eu.etaxonomy.taxeditor.forms.CdmFormFactory.TermComboType;
28 import eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent;
29 import eu.etaxonomy.taxeditor.forms.CheckboxElement;
30 import eu.etaxonomy.taxeditor.forms.ICdmFormElement;
31 import eu.etaxonomy.taxeditor.forms.IEnableableFormElement;
32 import eu.etaxonomy.taxeditor.forms.ISelectableElement;
33 import eu.etaxonomy.taxeditor.forms.NumberWithLabelElement;
34 import eu.etaxonomy.taxeditor.forms.SelectionArbitrator;
35 import eu.etaxonomy.taxeditor.forms.TextWithLabelElement;
36 import eu.etaxonomy.taxeditor.forms.ToggleableTextElement;
37 import eu.etaxonomy.taxeditor.forms.term.AbstractTermComboElement;
38 import eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement;
39 import eu.etaxonomy.taxeditor.section.AbstractCdmDetailSection;
40
41 /**
42 * <p>NameDetailElement class.</p>
43 *
44 * @author n.hoffmann
45 * @created Feb 26, 2010
46 * @version 1.0
47 */
48 public class NameDetailElement extends AbstractCdmDetailElement<NonViralName> implements ISelectableElement, IEnableableFormElement{
49
50 private ToggleableTextElement toggleable_cache;
51 private AbstractTermComboElement<Rank> combo_rank;
52 private TextWithLabelElement text_appendedPhrase;
53 private TextWithLabelElement text_uninomial;
54 private TextWithLabelElement text_infragenericEpithet;
55 private TextWithLabelElement text_specificEpithet;
56 private TextWithLabelElement text_infraspecificEpithet;
57 private SelectionArbitrator selectionArbitrator;
58 private CheckboxElement checkbox_anamorphic;
59 private TextWithLabelElement text_breed;
60 private NumberWithLabelElement text_publicationYear;
61 private NumberWithLabelElement text_originalPublicationYear;
62 private int cursorPosition;
63
64 /**
65 * <p>Constructor for NameDetailElement.</p>
66 *
67 * @param cdmFormFactory a {@link eu.etaxonomy.taxeditor.forms.CdmFormFactory} object.
68 * @param formElement a {@link eu.etaxonomy.taxeditor.forms.ICdmFormElement} object.
69 * @param style a int.
70 */
71 public NameDetailElement(CdmFormFactory cdmFormFactory, ICdmFormElement formElement,
72 int style) {
73 super(cdmFormFactory, formElement);
74 // register as selection listener
75 if(cdmFormFactory.getSelectionProvider() != null){
76 selectionArbitrator = cdmFormFactory.createSelectionArbitrator(this);
77 }
78 }
79
80 /** {@inheritDoc} */
81 @Override
82 protected void createControls(ICdmFormElement formElement, NonViralName nonViralName, int style) {
83
84 toggleable_cache = formFactory.createToggleableTextField(this, "Name Cache", nonViralName.getNameCache(), nonViralName.isProtectedNameCache(), style);
85
86 combo_rank = formFactory.createTermComboElement(TermComboType.RANK, this, "Rank", nonViralName.getRank(), style);
87
88 createGenusOrUninomialControls(this, nonViralName, style);
89 createInfragenerericEpithetControls(this, nonViralName, style);
90 createSpecificEpithetControls(this, nonViralName, style);
91 createInfraSpecificEpithetControls(this, nonViralName, style);
92
93 createSpecificNameParts(this, nonViralName, style);
94
95 text_appendedPhrase = formFactory.createTextWithLabelElement(formElement, "Appended Phrase", nonViralName.getAppendedPhrase(), style);
96
97 }
98
99 /**
100 * <p>clearCheckRankWarnings</p>
101 */
102 protected void clearCheckRankWarnings() {
103 if(getEntity().hasProblem(ParserProblem.CheckRank)){
104 getEntity().removeParsingProblem(ParserProblem.CheckRank);
105
106 TaxonNameEditor nameEditor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(Page.NAME);
107 nameEditor.getSelectedContainer().getNameViewer().clearErrors();
108 }
109 }
110
111 //
112 //
113 // public void setEnabled(boolean enabled){
114 // setEnabled(enabled, null);
115 // }
116
117 /** {@inheritDoc} */
118 @Override
119 protected void updateContent() {
120 if(getEntity() == null){
121 setEntity(NonViralName.NewInstance(null));
122 }
123
124 super.updateContent();
125
126 if(isIrrelevant()){
127 setIrrelevant(isIrrelevant());
128 }else{
129 setIrrelevant(toggleable_cache.getState(), Arrays.asList(new Object[]{toggleable_cache}));
130 }
131 }
132
133
134 private void createSpecificNameParts(ICdmFormElement formElement,
135 NonViralName nonViralName, int style) {
136 switch(nonViralName.getNomenclaturalCode()){
137 case ICBN:
138 createBotanicalNameParts(formElement, nonViralName, style);
139 break;
140 case ICZN:
141 createZoologicalNameParts(formElement, nonViralName, style);
142 break;
143 }
144 }
145
146 private void createBotanicalNameParts(ICdmFormElement formElement, NonViralName nonViralName, int style){
147 BotanicalName botanicalName = (BotanicalName) nonViralName;
148 checkbox_anamorphic = formFactory.createCheckbox(formElement, "Anamorphic", botanicalName.isAnamorphic(), style);
149 }
150
151 private void createZoologicalNameParts(ICdmFormElement formElement, NonViralName nonViralName, int style){
152 ZoologicalName zoologicalName = (ZoologicalName) nonViralName;
153 text_breed = formFactory.createTextWithLabelElement(formElement, "Breed", zoologicalName.getBreed(), style);
154 text_publicationYear = formFactory.createIntegerTextWithLabelElement(formElement, "Publication Year", zoologicalName.getPublicationYear(), style);
155 text_originalPublicationYear = formFactory.createIntegerTextWithLabelElement(formElement, "Orig. Publication Year", zoologicalName.getOriginalPublicationYear(), style);
156 }
157
158 private void createGenusOrUninomialControls(ICdmFormElement element, NonViralName nonViralName, int style){
159 String title = "Genus";
160 Rank rank = nonViralName.getRank();
161 if(rank != null && rank.isSupraGeneric()){
162 title = "Uninomial";
163 }
164 text_uninomial = formFactory.createTextWithLabelElement(element, title, nonViralName.getGenusOrUninomial(), style);
165 }
166
167 private void createInfragenerericEpithetControls(ICdmFormElement element, NonViralName nonViralName, int style){
168 if(nonViralName.getRank() != null && nonViralName.getRank().isInfraGeneric() && !nonViralName.getRank().isSpeciesAggregate()){
169 text_infragenericEpithet = formFactory.createTextWithLabelElement(element, "Infrageneric Epithet", nonViralName.getInfraGenericEpithet(), style);
170 }
171 }
172
173 private void createSpecificEpithetControls(ICdmFormElement element, NonViralName nonViralName, int style){
174 if(nonViralName.getRank() != null && (nonViralName.getRank().isSpecies() || nonViralName.getRank().isInfraSpecific() || nonViralName.getRank().isSpeciesAggregate())){
175 text_specificEpithet = formFactory.createTextWithLabelElement(element, "Specific Epithet", nonViralName.getSpecificEpithet(), SWT.NULL);
176 }
177 }
178
179 private void createInfraSpecificEpithetControls(ICdmFormElement element, NonViralName nonViralName, int style){
180 if(nonViralName.getRank() != null && nonViralName.getRank().isInfraSpecific()){
181 text_infraspecificEpithet = formFactory.createTextWithLabelElement(element, "Infraspecific Epithet", nonViralName.getInfraSpecificEpithet(), SWT.NULL);
182 }
183 }
184
185 /** {@inheritDoc} */
186 @Override
187 public void handleEvent(Object eventSource){
188 if(eventSource == combo_rank){
189 getEntity().setRank(combo_rank.getSelection());
190 clearCheckRankWarnings();
191 }
192 else if(eventSource == text_appendedPhrase){
193 getEntity().setAppendedPhrase(text_appendedPhrase.getText());
194 }
195 else if(eventSource == toggleable_cache){
196 getEntity().setNameCache(toggleable_cache.getText(), toggleable_cache.getState());
197 //setEnabled(! text_nameCache.getState(), Arrays.asList(new Object[]{text_nameCache, combo_nomenclaturalCode}));
198 if(! isIrrelevant()) setIrrelevant(toggleable_cache.getState(), Arrays.asList(new Object[]{toggleable_cache}));
199 }
200 else if(eventSource == text_infragenericEpithet){
201 getEntity().setInfraGenericEpithet(text_infragenericEpithet.getText());
202 }
203 else if(eventSource == text_infraspecificEpithet){
204 getEntity().setInfraSpecificEpithet(text_infraspecificEpithet.getText());
205 }
206 else if(eventSource == text_specificEpithet){
207 getEntity().setSpecificEpithet(text_specificEpithet.getText());
208 }
209 else if(eventSource == text_uninomial){
210 getEntity().setGenusOrUninomial(text_uninomial.getText());
211 }
212 else if(eventSource == checkbox_anamorphic){
213 ((BotanicalName)getEntity()).setAnamorphic(checkbox_anamorphic.getSelection());
214 }
215
216
217 if(eventSource != toggleable_cache && !toggleable_cache.getLayoutComposite().isDisposed()){
218 toggleable_cache.setText(getEntity().getNameCache());
219 }
220
221 // we have to notify the parent if this is embedded in the nonviral name section
222 // maybe we can handle this a little bit more elegant
223 if(getParentElement() instanceof AbstractCdmDetailSection)
224 firePropertyChangeEvent(new CdmPropertyChangeEvent(getParentElement(), null));
225 }
226
227 /*
228 * (non-Javadoc)
229 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#getSelectionArbitrator()
230 */
231 /**
232 * <p>Getter for the field <code>selectionArbitrator</code>.</p>
233 *
234 * @return a {@link eu.etaxonomy.taxeditor.forms.SelectionArbitrator} object.
235 */
236 public SelectionArbitrator getSelectionArbitrator() {
237 return selectionArbitrator;
238 }
239 }