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