Project

General

Profile

Download (7.98 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.supplemental.RightsSection;
45
import eu.etaxonomy.taxeditor.ui.section.taxon.TaxonDetailSection;
46

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

    
59
    public static String EXPANDED = "expanded";
60
    public static String COLLAPSED = "collapsed";
61

    
62
	private final AbstractFormSection<T> formSection;
63

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

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

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

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

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

    
171

    
172
		formSection.setEntity((T) input);
173

    
174
		return true;
175
	}
176

    
177
	/*
178
	 * (non-Javadoc)
179
	 *
180
	 * @see
181
	 * org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse
182
	 * .jface.util.PropertyChangeEvent)
183
	 */
184
	/** {@inheritDoc} */
185
	@Override
186
	public void propertyChange(PropertyChangeEvent event) {
187
		if (event != null) {
188

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

    
205
			else if (formSection.equals(eventSource)){
206
				markDirty();
207
			}
208
			else if(((eventSource instanceof ICdmFormElement)&& formSection.containsFormElement((ICdmFormElement) eventSource))) {
209
			    markDirty();
210
			}
211
		}
212
	}
213

    
214
	@Override
215
	protected void expansionStateChanged(boolean expanded) {
216
	    super.expansionStateChanged(expanded);
217
	    PreferencesUtil.setStringValue(StoreUtil.getPrefKey(formSection.getClass(), rootInput.getClass().getCanonicalName()), expanded?EXPANDED:COLLAPSED);
218
	}
219

    
220
}
    (1-1/1)