Project

General

Profile

Download (7.17 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
	 * Initial input
62
	 */
63
	private Object rootInput;
64

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

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

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

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

    
156

    
157
		formSection.setEntity((T) input);
158

    
159
		return true;
160
	}
161

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

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

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

    
199
	@Override
200
	protected void expansionStateChanged(boolean expanded) {
201
	    super.expansionStateChanged(expanded);
202
	    PreferencesUtil.setBooleanValue(StoreUtil.getPrefKey(formSection.getClass(), rootInput), expanded);
203
	}
204

    
205
}
    (1-1/1)