b92520d34aa2ecd7c5990ad698d948be8da33193
[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
10 package eu.etaxonomy.taxeditor.ui.section.name;
11
12 import java.util.Iterator;
13
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.SelectionListener;
16
17 import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
18 import eu.etaxonomy.cdm.model.reference.Reference;
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.SelectionArbitrator;
26 import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
27 import eu.etaxonomy.taxeditor.ui.section.supplemental.AbstractSourcedEntityBaseElement;
28 import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
29
30 /**
31 * <p>Abstract AbstractTypeDesignationElement class.</p>
32 *
33 * @author n.hoffmann
34 * @created May 17, 2010
35 * @version 1.0
36 */
37 public abstract class AbstractTypeDesignationElement<T extends TypeDesignationBase> extends AbstractSourcedEntityBaseElement<T> {
38
39 protected CheckboxElement checkbox_notDesignated;
40 protected EntitySelectionElement<Reference> selection_reference;
41 protected TextWithLabelElement text_referenceDetail;
42 protected ICdmFormElement formElement;
43
44 /**
45 * <p>Constructor for AbstractTypeDesignationElement.</p>
46 *
47 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
48 * @param section a {@link eu.etaxonomy.taxeditor.ui.element.AbstractFormSection} object.
49 * @param entity a T object.
50 * @param removeListener a {@link org.eclipse.swt.events.SelectionListener} object.
51 * @param style a int.
52 * @param <T> a T object.
53 */
54 public AbstractTypeDesignationElement(CdmFormFactory formFactory,
55 AbstractFormSection section, T entity,
56 SelectionListener removeListener, int style) {
57 super(formFactory, section, entity, removeListener, style);
58 }
59
60 /** {@inheritDoc} */
61 @Override
62 public void createControls(ICdmFormElement formElement, int style) {
63 checkbox_notDesignated = formFactory.createCheckbox(formElement, "Not Designated", false, style);
64 this.formElement = formElement;
65 }
66
67 @Override
68 public void setEntity(T entity){
69 super.setEntity(entity);
70
71 if (this.entity != null && this.entity.isLectoType()){
72 if (selection_reference == null){
73 selection_reference = formFactory
74 .createSelectionElement(Reference.class, formElement, "Reference",
75 entity.getCitation(), EntitySelectionElement.ALL, SWT.NULL);
76
77 text_referenceDetail = formFactory.createTextWithLabelElement(
78 formElement, "Reference Detail", entity.getCitationMicroReference(), SWT.NULL);
79 }
80 }else{
81 if (selection_reference != null){
82 removeReferenceControls();
83 //check if there is still a reference
84 if (this.entity.getCitation() != null){
85 this.entity.setCitation(null);
86 this.entity.setCitationMicroReference(null);
87 }
88 }
89 }
90 StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
91
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
102
103 public void removeReferenceControls(){
104 for (Iterator<ICdmFormElement> iterator = getElements().iterator();iterator.hasNext();) {
105 ICdmFormElement childElement = iterator.next();
106 // recursion
107 if (childElement instanceof EntitySelectionElement && ((EntitySelectionElement)childElement).equals(selection_reference)) {
108 childElement.removeElements();
109
110 // unregister selection arbitrator
111 if(childElement instanceof ISelectableElement){
112 SelectionArbitrator selectionArbitrator = ((ISelectableElement) childElement).getSelectionArbitrator();
113 if(selectionArbitrator != null){
114 formFactory.destroySelectionArbitrator(selectionArbitrator);
115 }
116 }
117
118 // unregister from property changes
119 formFactory.removePropertyChangeListener(childElement);
120 selection_reference = null;
121 }
122 if (childElement instanceof TextWithLabelElement && ((TextWithLabelElement)childElement).equals(text_referenceDetail)) {
123
124 childElement.removeElements();
125
126 // unregister selection arbitrator
127 if(childElement instanceof ISelectableElement){
128 SelectionArbitrator selectionArbitrator = ((ISelectableElement) childElement).getSelectionArbitrator();
129 if(selectionArbitrator != null){
130 formFactory.destroySelectionArbitrator(selectionArbitrator);
131 }
132 }
133
134 // unregister from property changes
135 formFactory.removePropertyChangeListener(childElement);
136 text_referenceDetail = null;
137
138
139 }
140 }
141
142 }
143 @Override
144 public void handleEvent(Object eventSource) {
145 if (eventSource == selection_reference) {
146 getEntity().setCitation(selection_reference.getEntity());
147 setEntity(entity);
148 }
149 if (eventSource == text_referenceDetail) {
150 getEntity().setCitationMicroReference(text_referenceDetail.getText());
151 setEntity(entity);
152 }
153 }
154
155 }