Project

General

Profile

Download (7.12 KB) Statistics
| Branch: | Tag: | Revision:
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.TaxonBaseDetailSection;
46
import eu.etaxonomy.taxeditor.ui.section.taxon.TaxonDetailElement;
47
import eu.etaxonomy.taxeditor.ui.section.taxon.TaxonDetailSection;
48
import eu.etaxonomy.taxeditor.ui.section.taxon.TaxonRelationshipDetailSection;
49

    
50
/**
51
 * <p>
52
 * CdmSectionPart class.
53
 * </p>
54
 *
55
 * @author n.hoffmann
56
 * @created Feb 8, 2010
57
 * @version 1.0
58
 */
59
public class CdmSectionPart<T> extends SectionPart implements
60
		IPropertyChangeListener {
61

    
62
	private final AbstractFormSection<T> formSection;
63

    
64
	/**
65
	 * <p>
66
	 * Constructor for CdmSectionPart.
67
	 * </p>
68
	 *
69
	 * @param section
70
	 *            a {@link eu.etaxonomy.taxeditor.ui.element.AbstractFormSection}
71
	 *            object.
72
	 * @param <T>
73
	 *            a T object.
74
	 */
75
	public CdmSectionPart(AbstractFormSection<T> section) {
76
		super(section);
77
		formSection = section;
78
	}
79

    
80
	/** {@inheritDoc} */
81
	@Override
82
	public boolean setFormInput(Object input) {
83
	    //FIXME (CM): Need to fix this part of the design.
84
	    //The design seems to be locked to the idea that only one
85
	    // entity (either from the navigator or the editor) drives
86
	    // the detail view. In the case of multiple inputs the only workaround
87
	    // is checking the type and extracting relevant objects.
88
		if (input instanceof IElementHasDetails) {
89
			input = ((IElementHasDetails) input).getData();
90
		}
91
		if ((input instanceof TaxonBase)
92
				&& (formSection instanceof ITaxonBaseDetailSection)) {
93
			((ITaxonBaseDetailSection) formSection)
94
					.setTaxonBase((TaxonBase) input);
95
			return true;
96
		}
97
		else if (input.getClass().equals(DerivedUnit.class)
98
				&& (formSection instanceof IDerivedUnitFacadeDetailSection)) {
99

    
100
			try {
101
				input = DerivedUnitFacade.NewInstance((DerivedUnit) input,
102
						PreferencesUtil.getDerivedUnitConfigurator());
103
			} catch (DerivedUnitFacadeNotSupportedException e) {
104
				MessagingUtils.error(getClass(), e);
105
			}
106
		}
107
		else if (input instanceof PolytomousKeyRelationship) {
108
			input = ((PolytomousKeyRelationship) input).getDestination();
109
		}
110

    
111
		else 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
	    } else if ((input instanceof TaxonRelationship)
118
				&& (formSection instanceof TaxonDetailSection)) {
119
			((TaxonDetailSection) formSection)
120
					.setTaxon( ((TaxonRelationship)input).getFromTaxon());
121
			return true;
122
		}else if ((input instanceof TaxonRelationship)
123
				&& (formSection instanceof NonViralNameDetailSection)) {
124
			((NonViralNameDetailSection) formSection)
125
					.setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
126
			return true;
127
		}else if ((input instanceof TaxonRelationship)
128
				&& (formSection instanceof NomenclaturalReferenceDetailSection)) {
129
			((NomenclaturalReferenceDetailSection) formSection)
130
					.setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
131
			return true;
132
		}else if ((input instanceof TaxonRelationship)
133
				&& (formSection instanceof NomenclaturalStatusSection)) {
134
			((NomenclaturalStatusSection) formSection)
135
					.setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
136
			return true;
137
		}else if ((input instanceof TaxonRelationship)
138
				&& (formSection instanceof ProtologueSection)) {
139
			((ProtologueSection) formSection)
140
					.setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
141
			return true;
142
		}else if ((input instanceof TaxonRelationship)
143
				&& (formSection instanceof TypeDesignationSection)) {
144
			((TypeDesignationSection) formSection)
145
					.setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
146
			return true;
147
		}else if ((input instanceof TaxonRelationship)
148
				&& (formSection instanceof NameRelationshipDetailSection)) {
149
			((NameRelationshipDetailSection) formSection)
150
					.setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
151
			return true;
152
		}
153

    
154
		
155
		formSection.setEntity((T) input);
156

    
157
		return true;
158
	}
159

    
160
	/*
161
	 * (non-Javadoc)
162
	 *
163
	 * @see
164
	 * org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse
165
	 * .jface.util.PropertyChangeEvent)
166
	 */
167
	/** {@inheritDoc} */
168
	@Override
169
	public void propertyChange(PropertyChangeEvent event) {
170
		if (event != null) {
171

    
172
			Object eventSource = event.getSource();
173
			if (eventSource instanceof EventObject){
174
				eventSource = ((EventObject)eventSource).getSource();
175
			}
176
			Control[] children = formSection.getLayoutComposite().getChildren();
177
			boolean containsElement = false;
178
			for (Control control : children) {
179
			    if(eventSource.equals(control)){
180
			        containsElement = true;
181
			        break;
182
			    }
183
            }
184
			if(containsElement){
185
			    markDirty();
186
			}
187

    
188
			else if (formSection.equals(eventSource)){
189
				markDirty();
190
			}
191
			else if(((eventSource instanceof ICdmFormElement)&& formSection.containsFormElement((ICdmFormElement) eventSource))) {
192
			    markDirty();
193
			}
194
		}
195
	}
196

    
197
}
(1-1/3)