- changed "Original Label Data Field" to use data binding
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / detail / CdmSectionPart.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.view.detail;
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.taxeditor.model.IElementHasDetails;
24 import eu.etaxonomy.taxeditor.model.PolytomousKeyRelationship;
25 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
26 import eu.etaxonomy.taxeditor.store.StoreUtil;
27 import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
28 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
29 import eu.etaxonomy.taxeditor.ui.section.ITaxonBaseDetailSection;
30 import eu.etaxonomy.taxeditor.ui.section.key.GeographicalScopeDetailSection;
31 import eu.etaxonomy.taxeditor.ui.section.key.PolytomousKeyDetailSection;
32 import eu.etaxonomy.taxeditor.ui.section.key.ScopeRestrictionSection;
33 import eu.etaxonomy.taxeditor.ui.section.key.TaxonomicScopeSection;
34 import eu.etaxonomy.taxeditor.ui.section.occurrence.IDerivedUnitFacadeDetailSection;
35
36 /**
37 * <p>
38 * CdmSectionPart class.
39 * </p>
40 *
41 * @author n.hoffmann
42 * @created Feb 8, 2010
43 * @version 1.0
44 */
45 public class CdmSectionPart<T> extends SectionPart implements
46 IPropertyChangeListener {
47
48 private final AbstractFormSection<T> formSection;
49
50 /**
51 * <p>
52 * Constructor for CdmSectionPart.
53 * </p>
54 *
55 * @param section
56 * a {@link eu.etaxonomy.taxeditor.ui.element.AbstractFormSection}
57 * object.
58 * @param <T>
59 * a T object.
60 */
61 public CdmSectionPart(AbstractFormSection<T> section) {
62 super(section);
63 formSection = section;
64 }
65
66 /** {@inheritDoc} */
67 @Override
68 public boolean setFormInput(Object input) {
69 //FIXME (CM): Need to fix this part of the design.
70 //The design seems to be locked to the idea that only one
71 // entity (either from the navigator or the editor) drives
72 // the detail view. In the case of multiple inputs the only workaround
73 // is checking the type and extracting relevant objects.
74 if (input instanceof IElementHasDetails) {
75 input = ((IElementHasDetails) input).getData();
76 }
77 if ((input instanceof TaxonBase)
78 && (formSection instanceof ITaxonBaseDetailSection)) {
79 ((ITaxonBaseDetailSection) formSection)
80 .setTaxonBase((TaxonBase) input);
81 return true;
82 }
83 else if (input.getClass().equals(DerivedUnit.class)
84 && (formSection instanceof IDerivedUnitFacadeDetailSection)) {
85
86 try {
87 input = DerivedUnitFacade.NewInstance((DerivedUnit) input,
88 PreferencesUtil.getDerivedUnitConfigurator());
89 } catch (DerivedUnitFacadeNotSupportedException e) {
90 StoreUtil.error(getClass(), e);
91 }
92 }
93 else if (input instanceof PolytomousKeyRelationship) {
94 input = ((PolytomousKeyRelationship) input).getDestination();
95 }
96
97 else if ((input instanceof PolytomousKeyNode) &&
98 (formSection instanceof PolytomousKeyDetailSection ||
99 formSection instanceof GeographicalScopeDetailSection ||
100 formSection instanceof ScopeRestrictionSection ||
101 formSection instanceof TaxonomicScopeSection)) {
102 input = ((PolytomousKeyNode)input).getKey();
103 }
104
105
106 formSection.setEntity((T) input);
107
108 return true;
109 }
110
111 /*
112 * (non-Javadoc)
113 *
114 * @see
115 * org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse
116 * .jface.util.PropertyChangeEvent)
117 */
118 /** {@inheritDoc} */
119 @Override
120 public void propertyChange(PropertyChangeEvent event) {
121 if (event != null) {
122
123 Object eventSource = event.getSource();
124 Control[] children = formSection.getLayoutComposite().getChildren();
125 boolean containsElement = false;
126 for (Control control : children) {
127 if(eventSource.equals(control)){
128 containsElement = true;
129 break;
130 }
131 }
132 if(containsElement){
133 markDirty();
134 }
135
136 else if (formSection.equals(eventSource)){
137 markDirty();
138 }
139 else if(((eventSource instanceof ICdmFormElement)&& formSection.containsFormElement((ICdmFormElement) eventSource))) {
140 markDirty();
141 }
142 }
143 }
144
145 }