Merge branch 'develop' into featureTreeEditor
[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
10 package eu.etaxonomy.taxeditor.view.detail;
11
12 import java.awt.Event;
13 import java.util.EventObject;
14
15 import org.eclipse.jface.util.IPropertyChangeListener;
16 import org.eclipse.jface.util.PropertyChangeEvent;
17 import org.eclipse.swt.widgets.Control;
18 import org.eclipse.ui.forms.SectionPart;
19
20 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
21 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;
22 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
23 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
24 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
25 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
26 import eu.etaxonomy.taxeditor.model.IElementHasDetails;
27 import eu.etaxonomy.taxeditor.model.MessagingUtils;
28 import eu.etaxonomy.taxeditor.model.PolytomousKeyRelationship;
29 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
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.NameDetailSection;
37 import eu.etaxonomy.taxeditor.ui.section.name.NameRelationshipDetailSection;
38 import eu.etaxonomy.taxeditor.ui.section.name.NomenclaturalStatusSection;
39 import eu.etaxonomy.taxeditor.ui.section.name.NonViralNameDetailSection;
40 import eu.etaxonomy.taxeditor.ui.section.name.ProtologueSection;
41 import eu.etaxonomy.taxeditor.ui.section.name.TypeDesignationSection;
42 import eu.etaxonomy.taxeditor.ui.section.occurrence.GeoScopeDetailSection;
43 import eu.etaxonomy.taxeditor.ui.section.occurrence.IDerivedUnitFacadeDetailSection;
44 import eu.etaxonomy.taxeditor.ui.section.reference.NomenclaturalReferenceDetailSection;
45 import eu.etaxonomy.taxeditor.ui.section.taxon.TaxonRelationshipDetailSection;
46
47 /**
48 * <p>
49 * CdmSectionPart class.
50 * </p>
51 *
52 * @author n.hoffmann
53 * @created Feb 8, 2010
54 * @version 1.0
55 */
56 public class CdmSectionPart<T> extends SectionPart implements
57 IPropertyChangeListener {
58
59 private final AbstractFormSection<T> formSection;
60
61 /**
62 * <p>
63 * Constructor for CdmSectionPart.
64 * </p>
65 *
66 * @param section
67 * a {@link eu.etaxonomy.taxeditor.ui.element.AbstractFormSection}
68 * object.
69 * @param <T>
70 * a T object.
71 */
72 public CdmSectionPart(AbstractFormSection<T> section) {
73 super(section);
74 formSection = section;
75 }
76
77 /** {@inheritDoc} */
78 @Override
79 public boolean setFormInput(Object input) {
80 //FIXME (CM): Need to fix this part of the design.
81 //The design seems to be locked to the idea that only one
82 // entity (either from the navigator or the editor) drives
83 // the detail view. In the case of multiple inputs the only workaround
84 // is checking the type and extracting relevant objects.
85 if (input instanceof IElementHasDetails) {
86 input = ((IElementHasDetails) input).getData();
87 }
88 if ((input instanceof TaxonBase)
89 && (formSection instanceof ITaxonBaseDetailSection)) {
90 ((ITaxonBaseDetailSection) formSection)
91 .setTaxonBase((TaxonBase) input);
92 return true;
93 }
94 else if (input.getClass().equals(DerivedUnit.class)
95 && (formSection instanceof IDerivedUnitFacadeDetailSection)) {
96
97 try {
98 input = DerivedUnitFacade.NewInstance((DerivedUnit) input,
99 PreferencesUtil.getDerivedUnitConfigurator());
100 } catch (DerivedUnitFacadeNotSupportedException e) {
101 MessagingUtils.error(getClass(), e);
102 }
103 }
104 else if (input instanceof PolytomousKeyRelationship) {
105 input = ((PolytomousKeyRelationship) input).getDestination();
106 }
107
108 else 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 } else if ((input instanceof TaxonRelationship)
115 && (formSection instanceof NonViralNameDetailSection)) {
116 ((NonViralNameDetailSection) formSection)
117 .setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
118 return true;
119 }else if ((input instanceof TaxonRelationship)
120 && (formSection instanceof NomenclaturalReferenceDetailSection)) {
121 ((NomenclaturalReferenceDetailSection) formSection)
122 .setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
123 return true;
124 }else if ((input instanceof TaxonRelationship)
125 && (formSection instanceof NomenclaturalStatusSection)) {
126 ((NomenclaturalStatusSection) formSection)
127 .setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
128 return true;
129 }else if ((input instanceof TaxonRelationship)
130 && (formSection instanceof ProtologueSection)) {
131 ((ProtologueSection) formSection)
132 .setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
133 return true;
134 }else if ((input instanceof TaxonRelationship)
135 && (formSection instanceof TypeDesignationSection)) {
136 ((TypeDesignationSection) formSection)
137 .setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
138 return true;
139 }else if ((input instanceof TaxonRelationship)
140 && (formSection instanceof NameRelationshipDetailSection)) {
141 ((NameRelationshipDetailSection) formSection)
142 .setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
143 return true;
144 }
145
146
147 formSection.setEntity((T) input);
148
149 return true;
150 }
151
152 /*
153 * (non-Javadoc)
154 *
155 * @see
156 * org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse
157 * .jface.util.PropertyChangeEvent)
158 */
159 /** {@inheritDoc} */
160 @Override
161 public void propertyChange(PropertyChangeEvent event) {
162 if (event != null) {
163
164 Object eventSource = event.getSource();
165 if (eventSource instanceof EventObject){
166 eventSource = ((EventObject)eventSource).getSource();
167 }
168 Control[] children = formSection.getLayoutComposite().getChildren();
169 boolean containsElement = false;
170 for (Control control : children) {
171 if(eventSource.equals(control)){
172 containsElement = true;
173 break;
174 }
175 }
176 if(containsElement){
177 markDirty();
178 }
179
180 else if (formSection.equals(eventSource)){
181 markDirty();
182 }
183 else if(((eventSource instanceof ICdmFormElement)&& formSection.containsFormElement((ICdmFormElement) eventSource))) {
184 markDirty();
185 }
186 }
187 }
188
189 }