ref #10138: after parsing send event to update the details view
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / detail / CdmSectionPart.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.view.detail;
10
11 import java.util.EventObject;
12
13 import org.eclipse.jface.util.IPropertyChangeListener;
14 import org.eclipse.jface.util.PropertyChangeEvent;
15 import org.eclipse.swt.widgets.Control;
16 import org.eclipse.ui.forms.SectionPart;
17
18 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
19 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;
20 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
21 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
22 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
23 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
24 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
25 import eu.etaxonomy.taxeditor.model.IElementHasDetails;
26 import eu.etaxonomy.taxeditor.model.MessagingUtils;
27 import eu.etaxonomy.taxeditor.model.PolytomousKeyRelationship;
28 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
29 import eu.etaxonomy.taxeditor.store.StoreUtil;
30 import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
31 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
32 import eu.etaxonomy.taxeditor.ui.section.ITaxonBaseDetailSection;
33 import eu.etaxonomy.taxeditor.ui.section.key.PolytomousKeyDetailSection;
34 import eu.etaxonomy.taxeditor.ui.section.key.ScopeRestrictionSection;
35 import eu.etaxonomy.taxeditor.ui.section.key.TaxonomicScopeSection;
36 import eu.etaxonomy.taxeditor.ui.section.name.NameRelationshipDetailSection;
37 import eu.etaxonomy.taxeditor.ui.section.name.NomenclaturalStatusSection;
38 import eu.etaxonomy.taxeditor.ui.section.name.NonViralNameDetailSection;
39 import eu.etaxonomy.taxeditor.ui.section.name.TypeDesignationSection;
40 import eu.etaxonomy.taxeditor.ui.section.occurrence.DeterminationDetailSection;
41 import eu.etaxonomy.taxeditor.ui.section.occurrence.GeoScopeDetailSection;
42 import eu.etaxonomy.taxeditor.ui.section.occurrence.IDerivedUnitFacadeDetailSection;
43 import eu.etaxonomy.taxeditor.ui.section.occurrence.dna.SampleDesignationDetailSection;
44 import eu.etaxonomy.taxeditor.ui.section.reference.NomenclaturalSourceDetailSection;
45 import eu.etaxonomy.taxeditor.ui.section.supplemental.RightsSection;
46 import eu.etaxonomy.taxeditor.ui.section.taxon.TaxonDetailSection;
47
48 /**
49 * <p>
50 * CdmSectionPart class.
51 * </p>
52 *
53 * @author n.hoffmann
54 * @created Feb 8, 2010
55 */
56 public class CdmSectionPart<T> extends SectionPart
57 implements IPropertyChangeListener {
58
59 public static String EXPANDED = "expanded";
60 public static String COLLAPSED = "collapsed";
61
62 private final AbstractFormSection<T> formSection;
63
64 /**
65 * Initial input
66 */
67 private Object rootInput;
68
69 public CdmSectionPart(AbstractFormSection<T> section) {
70 super(section);
71 formSection = section;
72 }
73
74 @Override
75 public boolean setFormInput(Object input) {
76 if (formSection.isDisposed()){
77 return false;
78 }
79
80 this.rootInput = input;
81 //FIXME (CM): Need to fix this part of the design.
82 //The design seems to be locked to the idea that only one
83 // entity (either from the navigator or the editor) drives
84 // the detail view. In the case of multiple inputs the only workaround
85 // is checking the type and extracting relevant objects.
86 if (input instanceof IElementHasDetails) {
87 input = ((IElementHasDetails) input).getData();
88 }
89
90 if ((input instanceof TaxonBase)
91 && (formSection instanceof ITaxonBaseDetailSection)) {
92 ((ITaxonBaseDetailSection) formSection)
93 .setTaxonBase((TaxonBase<?>) input);
94 return true;
95 }
96 else if (input.getClass().equals(DerivedUnit.class)
97 && (formSection instanceof IDerivedUnitFacadeDetailSection)) {
98
99 try {
100 input = DerivedUnitFacade.NewInstance((DerivedUnit) input,
101 PreferencesUtil.getDerivedUnitConfigurator());
102 } catch (DerivedUnitFacadeNotSupportedException e) {
103 MessagingUtils.error(getClass(), e);
104 }
105 }
106 else if (input instanceof PolytomousKeyRelationship) {
107 input = ((PolytomousKeyRelationship) input).getDestination();
108 if ((input instanceof PolytomousKeyNode) &&
109 (formSection instanceof PolytomousKeyDetailSection ||
110 formSection instanceof GeoScopeDetailSection ||
111 formSection instanceof ScopeRestrictionSection ||
112 formSection instanceof TaxonomicScopeSection)) {
113 input = ((PolytomousKeyNode)input).getKey();
114 }
115 }
116
117 else if ((input instanceof PolytomousKeyNode) &&
118 (formSection instanceof PolytomousKeyDetailSection ||
119 formSection instanceof GeoScopeDetailSection ||
120 formSection instanceof ScopeRestrictionSection ||
121 formSection instanceof TaxonomicScopeSection)) {
122 input = ((PolytomousKeyNode)input).getKey();
123 } else if ((input instanceof TaxonRelationship)
124 && (formSection instanceof TaxonDetailSection)) {
125 ((TaxonDetailSection) formSection)
126 .setTaxon( ((TaxonRelationship)input).getFromTaxon());
127 return true;
128 }else if ((input instanceof TaxonRelationship)
129 && (formSection instanceof NonViralNameDetailSection)) {
130 ((NonViralNameDetailSection) formSection)
131 .setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
132 return true;
133 }else if ((input instanceof TaxonRelationship)
134 && (formSection instanceof NomenclaturalSourceDetailSection)) {
135 ((NomenclaturalSourceDetailSection) formSection)
136 .setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
137 return true;
138 }else if ((input instanceof TaxonRelationship)
139 && (formSection instanceof NomenclaturalStatusSection)) {
140 ((NomenclaturalStatusSection) formSection)
141 .setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
142 return true;
143 }else if ((input instanceof TaxonRelationship)
144 && (formSection instanceof TypeDesignationSection)) {
145 ((TypeDesignationSection) formSection)
146 .setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
147 // .setEntity( ((TaxonRelationship)input).getFromTaxon().getName());
148 return true;
149 }else if ((input instanceof TaxonRelationship)
150 && (formSection instanceof NameRelationshipDetailSection)) {
151 ((NameRelationshipDetailSection) formSection)
152 .setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
153 return true;
154 }else if ((input instanceof DerivedUnitFacade)
155 && (formSection instanceof RightsSection)){
156 ((RightsSection)formSection).setEntity(((DerivedUnitFacade)input).innerDerivedUnit());
157 return true;
158 }else if ((input instanceof DerivedUnitFacade)
159 && (formSection instanceof DeterminationDetailSection)){
160 DerivedUnitFacade facade = ((DerivedUnitFacade)input);
161 SpecimenOrObservationBase<?> sob = facade.innerDerivedUnit() != null? facade.innerDerivedUnit(): facade.innerFieldUnit();
162 ((DeterminationDetailSection)formSection).setEntity(sob);
163 return true;
164 }else if ((input instanceof DerivedUnitFacade)
165 && (formSection instanceof SampleDesignationDetailSection)){
166 ((SampleDesignationDetailSection)formSection).setEntity(((DerivedUnitFacade)input).innerDerivedUnit());
167 return true;
168 }
169 //check whether this is needed
170 // }else if ((input instanceof TaxonBase)
171 // && (formSection instanceof TypeDesignationSection)){
172 // ((TypeDesignationSection)formSection).setEntity(((TaxonBase)input).getName());
173 // return true;
174 // }
175
176 formSection.setEntity((T) input);
177
178
179 return true;
180 }
181
182 public boolean setFormInputWithoutUpdate(Object input) {
183 if (formSection.isDisposed()){
184 return false;
185 }
186
187 this.rootInput = input;
188 //FIXME (CM): Need to fix this part of the design.
189 //The design seems to be locked to the idea that only one
190 // entity (either from the navigator or the editor) drives
191 // the detail view. In the case of multiple inputs the only workaround
192 // is checking the type and extracting relevant objects.
193 if (input instanceof IElementHasDetails) {
194 input = ((IElementHasDetails) input).getData();
195 }
196
197 if ((input instanceof TaxonBase)
198 && (formSection instanceof ITaxonBaseDetailSection )) {
199
200 //if (!((TaxonBase<?>) input).getTitleCache().equals(((ITaxonBaseDetailSection) formSection).getTaxonBase().getTitleCache())) {
201 ((ITaxonBaseDetailSection) formSection)
202 .setTaxonBaseWithoutUpdate((TaxonBase<?>) input);
203
204 //}
205
206 return true;
207 }
208 //actually only for Taxon Details...
209 return this.setFormInput( input);
210 }
211
212 @Override
213 public void propertyChange(PropertyChangeEvent event) {
214 if (event != null) {
215
216 Object eventSource = event.getSource();
217 if (eventSource instanceof EventObject){
218 eventSource = ((EventObject)eventSource).getSource();
219 }
220 Control[] children = formSection.getLayoutComposite().getChildren();
221 boolean containsElement = false;
222 for (Control control : children) {
223 if(eventSource.equals(control)){
224 containsElement = true;
225 break;
226 }
227 }
228 if(containsElement){
229 markDirty();
230 }
231
232 else if (formSection.equals(eventSource)){
233 markDirty();
234 }
235 else if(((eventSource instanceof ICdmFormElement)&& formSection.containsFormElement((ICdmFormElement) eventSource))) {
236 markDirty();
237 }
238 }
239 }
240
241 @Override
242 protected void expansionStateChanged(boolean expanded) {
243 super.expansionStateChanged(expanded);
244 PreferencesUtil.setStringValue(StoreUtil.getPrefKey(formSection.getClass(), rootInput.getClass().getCanonicalName()), expanded?EXPANDED:COLLAPSED);
245 }
246 }