Project

General

Profile

Download (7.7 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
    public static String EXPANDED = "expanded";
59
    public static String COLLAPSED = "collapsed";
60

    
61
	private final AbstractFormSection<T> formSection;
62

    
63
	/**
64
	 * Initial input
65
	 */
66
	private Object rootInput;
67

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

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

    
105
			try {
106
				input = DerivedUnitFacade.NewInstance((DerivedUnit) input,
107
						PreferencesUtil.getDerivedUnitConfigurator());
108
			} catch (DerivedUnitFacadeNotSupportedException e) {
109
				MessagingUtils.error(getClass(), e);
110
			}
111
		}
112
		else if (input instanceof PolytomousKeyRelationship) {
113
			input = ((PolytomousKeyRelationship) input).getDestination();
114
			if ((input instanceof PolytomousKeyNode) &&
115
	                (formSection instanceof PolytomousKeyDetailSection ||
116
	                        formSection instanceof GeoScopeDetailSection ||
117
	                        formSection instanceof ScopeRestrictionSection ||
118
	                        formSection instanceof TaxonomicScopeSection)) {
119
	            input = ((PolytomousKeyNode)input).getKey();
120
			}
121
		}
122

    
123
		else if ((input instanceof PolytomousKeyNode) &&
124
	            (formSection instanceof PolytomousKeyDetailSection ||
125
	                    formSection instanceof GeoScopeDetailSection ||
126
	                    formSection instanceof ScopeRestrictionSection ||
127
	                    formSection instanceof TaxonomicScopeSection)) {
128
	        input = ((PolytomousKeyNode)input).getKey();
129
	    } else if ((input instanceof TaxonRelationship)
130
				&& (formSection instanceof TaxonDetailSection)) {
131
			((TaxonDetailSection) formSection)
132
					.setTaxon( ((TaxonRelationship)input).getFromTaxon());
133
			return true;
134
		}else if ((input instanceof TaxonRelationship)
135
				&& (formSection instanceof NonViralNameDetailSection)) {
136
			((NonViralNameDetailSection) formSection)
137
					.setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
138
			return true;
139
		}else if ((input instanceof TaxonRelationship)
140
				&& (formSection instanceof NomenclaturalReferenceDetailSection)) {
141
			((NomenclaturalReferenceDetailSection) formSection)
142
					.setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
143
			return true;
144
		}else if ((input instanceof TaxonRelationship)
145
				&& (formSection instanceof NomenclaturalStatusSection)) {
146
			((NomenclaturalStatusSection) formSection)
147
					.setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
148
			return true;
149
		}else if ((input instanceof TaxonRelationship)
150
				&& (formSection instanceof ProtologueSection)) {
151
			((ProtologueSection) formSection)
152
					.setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
153
			return true;
154
		}else if ((input instanceof TaxonRelationship)
155
				&& (formSection instanceof TypeDesignationSection)) {
156
			((TypeDesignationSection) formSection)
157
					.setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
158
			return true;
159
		}else if ((input instanceof TaxonRelationship)
160
				&& (formSection instanceof NameRelationshipDetailSection)) {
161
			((NameRelationshipDetailSection) formSection)
162
					.setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
163
			return true;
164
		}
165

    
166

    
167
		formSection.setEntity((T) input);
168

    
169
		return true;
170
	}
171

    
172
	/*
173
	 * (non-Javadoc)
174
	 *
175
	 * @see
176
	 * org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse
177
	 * .jface.util.PropertyChangeEvent)
178
	 */
179
	/** {@inheritDoc} */
180
	@Override
181
	public void propertyChange(PropertyChangeEvent event) {
182
		if (event != null) {
183

    
184
			Object eventSource = event.getSource();
185
			if (eventSource instanceof EventObject){
186
				eventSource = ((EventObject)eventSource).getSource();
187
			}
188
			Control[] children = formSection.getLayoutComposite().getChildren();
189
			boolean containsElement = false;
190
			for (Control control : children) {
191
			    if(eventSource.equals(control)){
192
			        containsElement = true;
193
			        break;
194
			    }
195
            }
196
			if(containsElement){
197
			    markDirty();
198
			}
199

    
200
			else if (formSection.equals(eventSource)){
201
				markDirty();
202
			}
203
			else if(((eventSource instanceof ICdmFormElement)&& formSection.containsFormElement((ICdmFormElement) eventSource))) {
204
			    markDirty();
205
			}
206
		}
207
	}
208

    
209
	@Override
210
	protected void expansionStateChanged(boolean expanded) {
211
	    super.expansionStateChanged(expanded);
212
	    PreferencesUtil.setStringValue(StoreUtil.getPrefKey(formSection.getClass(), rootInput.getClass().getCanonicalName()), expanded?EXPANDED:COLLAPSED);
213
	}
214

    
215
}
    (1-1/1)