Project

General

Profile

Download (7.11 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.util.EventObject;
13

    
14
import org.eclipse.jface.util.IPropertyChangeListener;
15
import org.eclipse.jface.util.PropertyChangeEvent;
16
import org.eclipse.swt.widgets.Control;
17
import org.eclipse.ui.forms.SectionPart;
18

    
19
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
20
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;
21
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
22
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
23
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
24
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
25
import eu.etaxonomy.taxeditor.model.IElementHasDetails;
26
import eu.etaxonomy.taxeditor.model.MessagingUtils;
27
import eu.etaxonomy.taxeditor.model.PolytomousKeyRelationship;
28
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
29
import eu.etaxonomy.taxeditor.store.StoreUtil;
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.NameRelationshipDetailSection;
37
import eu.etaxonomy.taxeditor.ui.section.name.NomenclaturalStatusSection;
38
import eu.etaxonomy.taxeditor.ui.section.name.NonViralNameDetailSection;
39
import eu.etaxonomy.taxeditor.ui.section.name.ProtologueSection;
40
import eu.etaxonomy.taxeditor.ui.section.name.TypeDesignationSection;
41
import eu.etaxonomy.taxeditor.ui.section.occurrence.GeoScopeDetailSection;
42
import eu.etaxonomy.taxeditor.ui.section.occurrence.IDerivedUnitFacadeDetailSection;
43
import eu.etaxonomy.taxeditor.ui.section.reference.NomenclaturalReferenceDetailSection;
44
import eu.etaxonomy.taxeditor.ui.section.taxon.TaxonDetailSection;
45

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

    
58
	private final AbstractFormSection<T> formSection;
59

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

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

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

    
107
		else if ((input instanceof PolytomousKeyNode) &&
108
	            (formSection instanceof PolytomousKeyDetailSection ||
109
	                    formSection instanceof GeoScopeDetailSection ||
110
	                    formSection instanceof ScopeRestrictionSection ||
111
	                    formSection instanceof TaxonomicScopeSection)) {
112
	        input = ((PolytomousKeyNode)input).getKey();
113
	    } else if ((input instanceof TaxonRelationship)
114
				&& (formSection instanceof TaxonDetailSection)) {
115
			((TaxonDetailSection) formSection)
116
					.setTaxon( ((TaxonRelationship)input).getFromTaxon());
117
			return true;
118
		}else if ((input instanceof TaxonRelationship)
119
				&& (formSection instanceof NonViralNameDetailSection)) {
120
			((NonViralNameDetailSection) formSection)
121
					.setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
122
			return true;
123
		}else if ((input instanceof TaxonRelationship)
124
				&& (formSection instanceof NomenclaturalReferenceDetailSection)) {
125
			((NomenclaturalReferenceDetailSection) formSection)
126
					.setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
127
			return true;
128
		}else if ((input instanceof TaxonRelationship)
129
				&& (formSection instanceof NomenclaturalStatusSection)) {
130
			((NomenclaturalStatusSection) formSection)
131
					.setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
132
			return true;
133
		}else if ((input instanceof TaxonRelationship)
134
				&& (formSection instanceof ProtologueSection)) {
135
			((ProtologueSection) formSection)
136
					.setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
137
			return true;
138
		}else if ((input instanceof TaxonRelationship)
139
				&& (formSection instanceof TypeDesignationSection)) {
140
			((TypeDesignationSection) formSection)
141
					.setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
142
			return true;
143
		}else if ((input instanceof TaxonRelationship)
144
				&& (formSection instanceof NameRelationshipDetailSection)) {
145
			((NameRelationshipDetailSection) formSection)
146
					.setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
147
			return true;
148
		}
149

    
150

    
151
		formSection.setEntity((T) input);
152

    
153
		return true;
154
	}
155

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

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

    
184
			else if (formSection.equals(eventSource)){
185
				markDirty();
186
			}
187
			else if(((eventSource instanceof ICdmFormElement)&& formSection.containsFormElement((ICdmFormElement) eventSource))) {
188
			    markDirty();
189
			}
190
		}
191
	}
192

    
193
	@Override
194
	protected void expansionStateChanged(boolean expanded) {
195
	    super.expansionStateChanged(expanded);
196
	    PreferencesUtil.getPreferenceStore().setValue(StoreUtil.getPrefKey(formSection.getClass(), formSection.getEntity()), expanded);
197
	}
198

    
199
}
    (1-1/1)