Merge branch 'hotfix/5.44.1'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / name / AbstractTypeDesignationElement.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 java.util.Iterator;
12
13 import org.apache.logging.log4j.LogManager;
14 import org.apache.logging.log4j.Logger;
15 import org.eclipse.swt.events.SelectionListener;
16
17 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
18 import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
19 import eu.etaxonomy.taxeditor.store.StoreUtil;
20 import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
21 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
22 import eu.etaxonomy.taxeditor.ui.element.CheckboxElement;
23 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
24 import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
25 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
26 import eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator;
27 import eu.etaxonomy.taxeditor.ui.section.reference.OriginalSourceElement;
28 import eu.etaxonomy.taxeditor.ui.section.supplemental.AbstractSourcedEntityBaseElement;
29 import eu.etaxonomy.taxeditor.ui.section.supplemental.SourceSection;
30
31 /**
32 * @author n.hoffmann
33 * @created May 17, 2010
34 */
35 public abstract class AbstractTypeDesignationElement<T extends TypeDesignationBase>
36 extends AbstractSourcedEntityBaseElement<T> {
37
38 @SuppressWarnings("unused")
39 private static final Logger logger = LogManager.getLogger(AbstractTypeDesignationElement.class);
40
41 protected CheckboxElement checkbox_notDesignated;
42 // protected EntitySelectionElement<Reference> selection_reference;
43 // protected TextWithLabelElement text_referenceDetail;
44 protected OriginalSourceElement singleSourceElement;
45 protected SourceSection sourceSection;
46 protected ICdmFormElement formElement;
47 protected int style;
48
49
50 public AbstractTypeDesignationElement(CdmFormFactory formFactory,
51 AbstractFormSection section, T entity,
52 SelectionListener removeListener, int style) {
53 super(formFactory, section, entity, removeListener, style);
54 }
55
56 @Override
57 public void createControls(ICdmFormElement formElement, int style) {
58 // checkbox_notDesignated = formFactory.createCheckbox(formElement, "Not Designated", false, style);
59 this.formElement = formElement;
60 this.style = style;
61
62 }
63
64 @Override
65 public void setEntity(T entity){
66 super.setEntity(entity);
67 if (this.getElements() == null || this.getElements().isEmpty()){
68 return;
69 }
70 removeSourcesControls();
71 removeNotDesignatedControls();
72 if (this.entity != null && this.entity.hasDesignationSource()){
73 if (singleSourceElement == null){
74 singleSourceElement = formFactory.createOriginalSourceElement(formElement, entity, "Status Reference");
75 }
76 singleSourceElement.setEntity(entity.getDesignationSource(true));
77 }else{
78 if (singleSourceElement != null){
79 removeReferenceControls();
80 //check if there is still a reference
81 if (this.entity.getCitation() != null){
82 this.entity.setCitation(null);
83 this.entity.setCitationMicroReference(null);
84 }
85 }
86 }
87 checkbox_notDesignated = formFactory.createCheckbox(formElement, "Not Designated", entity.isNotDesignated(), style);
88 sourceSection = formFactory.createSourceSection(formElement, StoreUtil.getSectionStyle(SourceSection.class, IdentifiableSource.class.getCanonicalName()));
89 sourceSection.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
90 sourceSection.setEntity(entity);
91 StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
92 }
93
94 /**
95 * Updates all widgets to display the latest data
96 */
97 protected void updateContent() {
98 // if (this.getParentElement() instanceof NameDetailSection
99 }
100
101 public void removeReferenceControls(){
102 for (Iterator<ICdmFormElement> iterator = getElements().iterator();iterator.hasNext();) {
103 ICdmFormElement childElement = iterator.next();
104 // recursion
105 // if (childElement instanceof EntitySelectionElement && ((EntitySelectionElement)childElement).equals(selection_reference)) {
106 // childElement.removeElements();
107 //
108 // // unregister selection arbitrator
109 // if(childElement instanceof ISelectableElement){
110 // SelectionArbitrator selectionArbitrator = ((ISelectableElement) childElement).getSelectionArbitrator();
111 // if(selectionArbitrator != null){
112 // formFactory.destroySelectionArbitrator(selectionArbitrator);
113 // }
114 // }
115 //
116 // // unregister from property changes
117 // formFactory.removePropertyChangeListener(childElement);
118 // singleSourceElement = null;
119 // }
120 if (childElement instanceof OriginalSourceElement && ((OriginalSourceElement)childElement).equals(singleSourceElement)) {
121
122 childElement.removeElements();
123
124 // unregister selection arbitrator
125 if(childElement instanceof ISelectableElement){
126 SelectionArbitrator selectionArbitrator = ((ISelectableElement) childElement).getSelectionArbitrator();
127 if(selectionArbitrator != null){
128 formFactory.destroySelectionArbitrator(selectionArbitrator);
129 }
130 }
131
132 // unregister from property changes
133 formFactory.removePropertyChangeListener(childElement);
134 singleSourceElement = null;
135 }
136 }
137 }
138
139 public void removeNotDesignatedControls(){
140 for (Iterator<ICdmFormElement> iterator = getElements().iterator();iterator.hasNext();) {
141 ICdmFormElement childElement = iterator.next();
142 // recursion
143 if (childElement instanceof CheckboxElement && ((CheckboxElement)childElement).equals(this.checkbox_notDesignated)) {
144 childElement.removeElements();
145
146 // unregister selection arbitrator
147 if(childElement instanceof ISelectableElement){
148 SelectionArbitrator selectionArbitrator = ((ISelectableElement) childElement).getSelectionArbitrator();
149 if(selectionArbitrator != null){
150 formFactory.destroySelectionArbitrator(selectionArbitrator);
151 }
152 }
153
154 // unregister from property changes
155 formFactory.removePropertyChangeListener(childElement);
156 checkbox_notDesignated = null;
157 break;
158 }
159 }
160 }
161
162 public void removeSourcesControls(){
163 for (Iterator<ICdmFormElement> iterator = getElements().iterator();iterator.hasNext();) {
164 ICdmFormElement childElement = iterator.next();
165 // recursion
166 if (childElement instanceof SourceSection) {
167 childElement.removeElements();
168
169 getElements().remove(childElement);
170
171 // unregister selection arbitrator
172 if(childElement instanceof ISelectableElement){
173 SelectionArbitrator selectionArbitrator = ((ISelectableElement) childElement).getSelectionArbitrator();
174 if(selectionArbitrator != null){
175 formFactory.destroySelectionArbitrator(selectionArbitrator);
176 }
177 }
178
179 // unregister from property changes
180 formFactory.removePropertyChangeListener(childElement);
181 ((SourceSection)childElement).dispose();
182 sourceSection = null;
183 break;
184 }
185 }
186 }
187
188 @Override
189 public void handleEvent(Object eventSource) {
190 // if (eventSource == selection_reference) {
191 // getEntity().setCitation(selection_reference.getEntity());
192 // setEntity(entity);
193 // }
194 // if (eventSource == text_referenceDetail) {
195 // getEntity().setCitationMicroReference(text_referenceDetail.getText());
196 // setEntity(entity);
197 // }
198 if (eventSource == checkbox_notDesignated) {
199 getEntity().setNotDesignated(checkbox_notDesignated.getSelection());
200 }
201 }
202
203 }