Merge branch 'hotfix/5.44.1'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / name / NonViralNameDetailElement.java
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 package eu.etaxonomy.taxeditor.ui.section.name;
10
11 import eu.etaxonomy.cdm.model.name.IBacterialName;
12 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
13 import eu.etaxonomy.cdm.model.name.TaxonName;
14 import eu.etaxonomy.taxeditor.l10n.Messages;
15 import eu.etaxonomy.taxeditor.model.MessagingUtils;
16 import eu.etaxonomy.taxeditor.preference.NameDetailsConfigurator;
17 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
18 import eu.etaxonomy.taxeditor.store.StoreUtil;
19 import eu.etaxonomy.taxeditor.ui.combo.EnumComboElement;
20 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
21 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
22 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
23 import eu.etaxonomy.taxeditor.ui.element.LsidWithExceptionLabelElement;
24 import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
25 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection;
26 import eu.etaxonomy.taxeditor.ui.section.AbstractIdentifiableEntityDetailElement;
27
28 /**
29 * @author n.hoffmann
30 * @created May 20, 2010
31 */
32 public class NonViralNameDetailElement
33 extends AbstractIdentifiableEntityDetailElement<TaxonName> {
34
35 private NameDetailSection section_name;
36 private AuthorshipDetailSection section_author;
37 private EnumComboElement<NomenclaturalCode> combo_nomenclaturalCode;
38 private HybridDetailSection section_hybrid;
39 private LsidWithExceptionLabelElement textLsid;
40 private TextWithLabelElement text_nameApprobation;
41
42 public NonViralNameDetailElement(CdmFormFactory formFactory,
43 ICdmFormElement formElement) {
44 super(formFactory, formElement);
45 }
46
47 @Override
48 protected void createControls(ICdmFormElement formElement, final TaxonName entity, int style) {
49 NameDetailsConfigurator config = PreferencesUtil.getPreferredNameDetailsConfiguration();
50
51 if (config == null){
52 toggleable_cache = formFactory.createToggleableTextField(formElement, "Cache", entity.getTitleCache(), entity.isProtectedTitleCache() || entity.isProtectedFullTitleCache(), style);
53
54 combo_nomenclaturalCode = formFactory.createEnumComboElement(NomenclaturalCode.class, formElement, style, true);
55 combo_nomenclaturalCode.setSelection(entity.getNameType());
56 if (entity.getNameType().equals(NomenclaturalCode.ICNP)){
57 text_nameApprobation = formFactory.createTextWithLabelElement(formElement, "Name Approbation", entity.getNameApprobation(),style);
58 }
59 textLsid = formFactory.createLsidWithExceptionLabelElement(formElement, "Lsid", entity.getLsid(), style);
60 }else{
61 if (config.isNameCacheActivated()){
62 toggleable_cache = formFactory.createToggleableTextField(formElement, "Cache", entity.getTitleCache(), entity.isProtectedTitleCache() || entity.isProtectedFullTitleCache(), style);
63 }
64 if (config.isNomenclaturalCodeActived()){
65 combo_nomenclaturalCode = formFactory.createEnumComboElement(NomenclaturalCode.class, formElement, style);
66 combo_nomenclaturalCode.setSelection(entity.getNameType());
67 }
68 if (entity.getNameType().equals(NomenclaturalCode.ICNP)){
69 if (config.isNameApprobiationActivated()){
70 text_nameApprobation = formFactory.createTextWithLabelElement(formElement, "Name Approbation", entity.getNameApprobation(),style);
71 }
72 }
73 if (config.isLSIDActivated()){
74 textLsid = formFactory.createLsidWithExceptionLabelElement(formElement, "Lsid", entity.getLsid(), style);
75 }
76 }
77
78 section_name = formFactory.createNameDetailSection(formElement, null, StoreUtil.getSectionStyle(NameDetailSection.class, entity.getClass().getCanonicalName(), true));
79 section_name.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
80 addControl(section_name);
81 addElement(section_name);
82 // if (isAdvancedView || PreferencesUtil.getBooleanValue(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_AUTHORSHIP)){
83 section_author = formFactory.createAuthorshipDetailSection(formElement, null, StoreUtil.getSectionStyle(AuthorshipDetailSection.class, entity.getClass().getCanonicalName(), true));
84 section_author.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
85 addControl(section_author);
86 addElement(section_author);
87 // }
88 //TODO RL
89 if (config == null || config.isHybridActivated()){
90 section_hybrid = formFactory.createHybridDetailSection(formElement, null, StoreUtil.getSectionStyle(HybridDetailSection.class, entity.getClass().getCanonicalName()));
91 section_hybrid.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
92 addControl(section_hybrid);
93 addElement(section_hybrid);
94 }
95
96 //cache relevance
97 registerCacheRelevance(section_name);
98 registerCacheRelevance(section_author);
99 registerCacheRelevance(combo_nomenclaturalCode);
100 registerCacheRelevance(section_hybrid);
101 }
102
103 @Override
104 protected void updateContent() {
105 super.updateContent();
106
107 if(toggleable_cache != null){
108 toggleable_cache.setCacheEnabled(getEntity().isProtectedTitleCache() || getEntity().isProtectedFullTitleCache());
109 updateCacheRelevance();
110 }
111 }
112
113 @Override
114 public void setEntity(TaxonName entity) {
115 super.setEntity(entity);
116 if (section_name!=null) {
117 section_name.setEntity(entity);
118 }
119 if (section_author!=null) {
120 section_author.setEntity(entity);
121 }
122 if (section_hybrid!=null) {
123 section_hybrid.setEntity(entity);
124 }
125 }
126
127 @Override
128 public void removeElements() {
129 super.removeElements();
130 if (section_name != null) {
131 section_name.removeElements();
132 removeControl(section_name);
133 section_name.dispose();
134 section_name = null;
135 }
136 if (section_author != null) {
137 section_author.removeElements();
138 removeControl(section_author);
139 section_author.dispose();
140 section_author = null;
141 }
142 }
143
144 @Override
145 public void handleEvent(Object eventSource) {
146 if (eventSource == toggleable_cache) {
147 handleToggleableCacheField();
148 // we never want the fullTitleCache to be protected since we only
149 // use it for
150 // initiating the free text name editor
151 getEntity().setProtectedFullTitleCache(false);
152 } else if (eventSource == section_name || eventSource == section_author) {
153 if (getParentElement() instanceof AbstractCdmDetailSection) {
154 ((AbstractCdmDetailSection<?>) getParentElement()).updateTitle();
155 }
156 if (toggleable_cache != null){
157 if (!toggleable_cache.getState()) {
158 toggleable_cache.setText(getEntity().getTitleCache());
159 }
160 }
161 } else if (eventSource == section_name) {
162 section_name.setEntity(getEntity());
163 getLayoutComposite().layout();
164 } else if(eventSource==textLsid){
165 getEntity().setLsid(textLsid.parseText());
166 } else if (eventSource == combo_nomenclaturalCode){
167 boolean change = checkForDeletableParts(combo_nomenclaturalCode.getSelection());
168 if (change){
169 getEntity().setNameType(combo_nomenclaturalCode.getSelection());
170
171 this.updateContent();
172 //this is needed for the "new" wizards in order to redraw correctly
173 StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
174 if (section_hybrid != null){
175 section_hybrid.setEntity(getEntity());
176 }
177 if (section_author != null){
178 section_author.setEntity(getEntity());
179 }
180 if (section_name != null){
181 section_name.setEntity(getEntity());
182 }
183 }else{
184 combo_nomenclaturalCode.setSelection(getEntity().getNameType());
185 }
186 } else if(eventSource == this.text_nameApprobation){
187 ((IBacterialName)getEntity()).setNameApprobation(text_nameApprobation.getText());
188 }
189 }
190
191 private boolean checkForDeletableParts(NomenclaturalCode selection) {
192 NomenclaturalCode actualCode = getEntity().getNameType();
193 if (selection.equals(actualCode)){
194 return true;
195 }
196 if (actualCode.equals(NomenclaturalCode.ICZN) ){
197 if(getEntity().getOriginalPublicationYear() != null || getEntity().getPublicationYear() != null || getEntity().getBreed() != null){
198 if (MessagingUtils.confirmDialog(Messages.NonViralNameDetails_confirmDeleteOfZoologicalNameParts, Messages.NonViralNameDetails_descriptionDeleteZoologicalNameParts)){
199 getEntity().setOriginalPublicationYear(null);
200 getEntity().setPublicationYear(null);
201 getEntity().setBreed(null);
202 return true;
203 }
204 }else {
205 return true;
206 }
207 }
208 if (actualCode.equals(NomenclaturalCode.ICNP)){
209 if (getEntity().getNameApprobation() != null || getEntity().getSubGenusAuthorship() != null){
210 if (MessagingUtils.confirmDialog(Messages.NonViralNameDetails_confirmDeleteOfBacterialNameParts, Messages.NonViralNameDetails_desciptionDeleteOfBacterialNameParts)){
211 getEntity().setNameApprobation(null);
212 getEntity().setSubGenusAuthorship(null);
213 return true;
214 }
215 }else{
216 return true;
217 }
218 }
219 if (actualCode.equals(NomenclaturalCode.NonViral)){
220 return true;
221 }
222 if (actualCode.equals(NomenclaturalCode.Fungi)){
223 if (getEntity().isAnamorphic() ){
224 if (MessagingUtils.confirmDialog(Messages.NonViralNameDetails_confirmDeleteOfFungiNameParts, Messages.NonViralNameDetails_descriptionDeleteOfFungiNameParts)){
225 getEntity().setAnamorphic(false);
226 return true;
227 }
228 }else{
229 return true;
230 }
231 }
232 if (actualCode.equals(NomenclaturalCode.ICNCP)){
233 if (getEntity().getCultivarEpithet() != null || getEntity().getCultivarGroupEpithet() != null){
234 if (MessagingUtils.confirmDialog(Messages.NonViralNameDetails_confirmDeleteOfCultivarNameParts, Messages.NonViralNameDetails_descriptionDeleteOfCultivarNameParts)){
235 getEntity().setCultivarEpithet(null);
236 getEntity().setCultivarGroupEpithet(null);
237 return true;
238 }
239 }else{
240 return true;
241 }
242 }
243 if (actualCode.equals(NomenclaturalCode.ICNAFP)){
244 return true;
245 }
246 return false;
247 }
248
249 @Override
250 protected void handleToggleableCacheField() {
251 getEntity().setTitleCache(toggleable_cache.getText(), toggleable_cache.getState());
252 updateCacheRelevance();
253 updateToggleableCacheField();
254 }
255
256 @Override
257 public void fillFields() {
258 super.fillFields();
259
260 section_name.getDetailElement().fillFields();;
261 section_author.getDetailElement().fillFields();
262 combo_nomenclaturalCode.setSelection(getEntity().getNameType());
263 section_hybrid.getDetailElement().fillFields();
264 textLsid.setText(getEntity().getLsid()!= null? getEntity().getLsid().toString():null);
265 if (text_nameApprobation != null) {
266 text_nameApprobation.setText(getEntity().getNameApprobation());
267 }
268
269 }
270 }