#4018 let the right side of the translation wizard empty, fix the save problem for...
[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 java.awt.Event;
14 import java.util.EventObject;
15
16 import org.eclipse.jface.util.IPropertyChangeListener;
17 import org.eclipse.jface.util.PropertyChangeEvent;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.ui.forms.SectionPart;
20
21 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
22 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;
23 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
24 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
25 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
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.occurrence.GeoScopeDetailSection;
37 import eu.etaxonomy.taxeditor.ui.section.occurrence.IDerivedUnitFacadeDetailSection;
38
39 /**
40 * <p>
41 * CdmSectionPart class.
42 * </p>
43 *
44 * @author n.hoffmann
45 * @created Feb 8, 2010
46 * @version 1.0
47 */
48 public class CdmSectionPart<T> extends SectionPart implements
49 IPropertyChangeListener {
50
51 private final AbstractFormSection<T> formSection;
52
53 /**
54 * <p>
55 * Constructor for CdmSectionPart.
56 * </p>
57 *
58 * @param section
59 * a {@link eu.etaxonomy.taxeditor.ui.element.AbstractFormSection}
60 * object.
61 * @param <T>
62 * a T object.
63 */
64 public CdmSectionPart(AbstractFormSection<T> section) {
65 super(section);
66 formSection = section;
67 }
68
69 /** {@inheritDoc} */
70 @Override
71 public boolean setFormInput(Object input) {
72 //FIXME (CM): Need to fix this part of the design.
73 //The design seems to be locked to the idea that only one
74 // entity (either from the navigator or the editor) drives
75 // the detail view. In the case of multiple inputs the only workaround
76 // is checking the type and extracting relevant objects.
77 if (input instanceof IElementHasDetails) {
78 input = ((IElementHasDetails) input).getData();
79 }
80 if ((input instanceof TaxonBase)
81 && (formSection instanceof ITaxonBaseDetailSection)) {
82 ((ITaxonBaseDetailSection) formSection)
83 .setTaxonBase((TaxonBase) input);
84 return true;
85 }
86 else if (input.getClass().equals(DerivedUnit.class)
87 && (formSection instanceof IDerivedUnitFacadeDetailSection)) {
88
89 try {
90 input = DerivedUnitFacade.NewInstance((DerivedUnit) input,
91 PreferencesUtil.getDerivedUnitConfigurator());
92 } catch (DerivedUnitFacadeNotSupportedException e) {
93 MessagingUtils.error(getClass(), e);
94 }
95 }
96 else if (input instanceof PolytomousKeyRelationship) {
97 input = ((PolytomousKeyRelationship) input).getDestination();
98 }
99
100 else if ((input instanceof PolytomousKeyNode) &&
101 (formSection instanceof PolytomousKeyDetailSection ||
102 formSection instanceof GeoScopeDetailSection ||
103 formSection instanceof ScopeRestrictionSection ||
104 formSection instanceof TaxonomicScopeSection)) {
105 input = ((PolytomousKeyNode)input).getKey();
106 }
107
108
109 formSection.setEntity((T) input);
110
111 return true;
112 }
113
114 /*
115 * (non-Javadoc)
116 *
117 * @see
118 * org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse
119 * .jface.util.PropertyChangeEvent)
120 */
121 /** {@inheritDoc} */
122 @Override
123 public void propertyChange(PropertyChangeEvent event) {
124 if (event != null) {
125
126 Object eventSource = event.getSource();
127 if (eventSource instanceof EventObject){
128 eventSource = ((EventObject)eventSource).getSource();
129 }
130 Control[] children = formSection.getLayoutComposite().getChildren();
131 boolean containsElement = false;
132 for (Control control : children) {
133 if(eventSource.equals(control)){
134 containsElement = true;
135 break;
136 }
137 }
138 if(containsElement){
139 markDirty();
140 }
141
142 else if (formSection.equals(eventSource)){
143 markDirty();
144 }
145 else if(((eventSource instanceof ICdmFormElement)&& formSection.containsFormElement((ICdmFormElement) eventSource))) {
146 markDirty();
147 }
148 }
149 }
150
151 }