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