Introduces a handler to delete all empty names; Cleans up the name editors context...
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / section / name / NonViralNameDetailElement.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 import java.util.List;
15
16 import org.eclipse.ui.forms.widgets.Section;
17
18 import eu.etaxonomy.cdm.model.name.NonViralName;
19 import eu.etaxonomy.taxeditor.forms.CdmFormFactory;
20 import eu.etaxonomy.taxeditor.forms.CdmFormFactory.DetailType;
21 import eu.etaxonomy.taxeditor.forms.CdmFormFactory.EnumComboType;
22 import eu.etaxonomy.taxeditor.forms.ICdmFormElement;
23 import eu.etaxonomy.taxeditor.forms.ToggleableTextElement;
24 import eu.etaxonomy.taxeditor.forms.term.NomenclaturalCodeComboElement;
25 import eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement;
26 import eu.etaxonomy.taxeditor.section.AbstractCdmDetailSection;
27
28 /**
29 * <p>NonViralNameDetailElement class.</p>
30 *
31 * @author n.hoffmann
32 * @created May 20, 2010
33 * @version 1.0
34 */
35 public class NonViralNameDetailElement extends AbstractCdmDetailElement<NonViralName> {
36
37 private NameDetailSection section_name;
38 private AuthorshipDetailSection section_author;
39 private ToggleableTextElement toggleable_cache;
40 private NomenclaturalCodeComboElement combo_nomenclaturalCode;
41
42 /**
43 * <p>Constructor for NonViralNameDetailElement.</p>
44 *
45 * @param formFactory a {@link eu.etaxonomy.taxeditor.forms.CdmFormFactory} object.
46 * @param formElement a {@link eu.etaxonomy.taxeditor.forms.ICdmFormElement} object.
47 */
48 public NonViralNameDetailElement(CdmFormFactory formFactory,
49 ICdmFormElement formElement) {
50 super(formFactory, formElement);
51 }
52
53 /* (non-Javadoc)
54 * @see eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#createControls(eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement, eu.etaxonomy.cdm.model.common.AnnotatableEntity, int)
55 */
56 /** {@inheritDoc} */
57 @Override
58 protected void createControls(ICdmFormElement formElement,
59 final NonViralName entity, int style) {
60
61 toggleable_cache = formFactory.createToggleableTextField(formElement, "Cache", entity.getTitleCache(), entity.isProtectedTitleCache() || entity.isProtectedFullTitleCache(), style);
62
63 combo_nomenclaturalCode = (NomenclaturalCodeComboElement) formFactory.createEnumComboElement(EnumComboType.NOMENCLATURALCODE, formElement, style);
64 combo_nomenclaturalCode.setSelection(entity.getNomenclaturalCode());
65
66 section_name = (NameDetailSection) formFactory.createCdmDetailSection(DetailType.SCIENTIFICNAME, getConversationHolder(), formElement, null, Section.TWISTIE | Section.EXPANDED);
67 section_name.setLayoutData(CdmFormFactory.FILL_HORIZONTALLY(2, 1));
68 addControl(section_name);
69 addElement(section_name);
70 section_author = (AuthorshipDetailSection) formFactory.createCdmDetailSection(DetailType.AUTHORSHIP, getConversationHolder(), formElement, null, Section.TWISTIE | Section.EXPANDED);
71 section_author.setLayoutData(CdmFormFactory.FILL_HORIZONTALLY(2, 1));
72 addControl(section_author);
73 addElement(section_author);
74 }
75
76
77 /** {@inheritDoc} */
78 @Override
79 protected void updateContent() {
80 super.updateContent();
81
82 // disable nomenclatural code, because changing of nom.code is not implemented on library side
83 combo_nomenclaturalCode.setEnabled(false);
84
85 setIrrelevant(toggleable_cache.getState(), Arrays.asList(new Object[]{toggleable_cache}));
86 }
87
88 /** {@inheritDoc} */
89 @Override
90 public void setEntity(NonViralName entity) {
91 super.setEntity(entity);
92 section_name.setEntity(entity);
93 section_author.setEntity(entity);
94 }
95
96 /** {@inheritDoc} */
97 @Override
98 public void removeElements() {
99 super.removeElements();
100 if(section_name != null){
101 section_name.removeElements();
102 removeControl(section_name);
103 section_name.dispose();
104 section_name = null;
105 }
106 if(section_author != null){
107 section_author.removeElements();
108 removeControl(section_author);
109 section_author.dispose();
110 section_author = null;
111 }
112 }
113
114 /* (non-Javadoc)
115 * @see eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleEvent(java.lang.Object)
116 */
117 /** {@inheritDoc} */
118 @Override
119 public void handleEvent(Object eventSource) {
120 if(eventSource == toggleable_cache){
121 getEntity().setTitleCache(toggleable_cache.getText(), toggleable_cache.getState());
122 // we never want the fullTitleCache to be protected since we only use it for
123 // initiating the free text name editor
124 getEntity().setProtectedFullTitleCache(false);
125 boolean irrelevant = toggleable_cache.getState();
126 List<Object> except = Arrays.asList(new Object[]{toggleable_cache});
127 setIrrelevant(irrelevant, except);
128 }
129 else if(eventSource == section_name || eventSource == section_author){
130 if(getParentElement() instanceof AbstractCdmDetailSection)
131 ((AbstractCdmDetailSection) getParentElement()).updateTitle();
132 if(! toggleable_cache.getState()){
133 toggleable_cache.setText(getEntity().getTitleCache());
134 }
135 }
136 if(eventSource == section_name){
137 section_name.setEntity(getEntity());
138 getLayoutComposite().layout();
139 }
140 }
141 }