Project

General

Profile

Download (8.55 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
package eu.etaxonomy.taxeditor.view.detail;
10

    
11
import java.util.EventObject;
12

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

    
18
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
19
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;
20
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
21
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
22
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
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.TypeDesignationSection;
40
import eu.etaxonomy.taxeditor.ui.section.occurrence.DeterminationDetailSection;
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.occurrence.dna.SampleDesignationDetailSection;
44
import eu.etaxonomy.taxeditor.ui.section.reference.NomenclaturalSourceDetailSection;
45
import eu.etaxonomy.taxeditor.ui.section.supplemental.RightsSection;
46
import eu.etaxonomy.taxeditor.ui.section.taxon.TaxonDetailSection;
47

    
48
/**
49
 * <p>
50
 * CdmSectionPart class.
51
 * </p>
52
 *
53
 * @author n.hoffmann
54
 * @created Feb 8, 2010
55
 */
56
public class CdmSectionPart<T> extends SectionPart
57
        implements 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
	public CdmSectionPart(AbstractFormSection<T> section) {
70
		super(section);
71
		formSection = section;
72
	}
73

    
74
	@Override
75
	public boolean setFormInput(Object input) {
76
        if (formSection.isDisposed()){
77
            return false;
78
        }
79

    
80
	    this.rootInput = input;
81
	    //FIXME (CM): Need to fix this part of the design.
82
	    //The design seems to be locked to the idea that only one
83
	    // entity (either from the navigator or the editor) drives
84
	    // the detail view. In the case of multiple inputs the only workaround
85
	    // is checking the type and extracting relevant objects.
86
		if (input instanceof IElementHasDetails) {
87
			input = ((IElementHasDetails) input).getData();
88
		}
89

    
90
		if ((input instanceof TaxonBase)
91
				&& (formSection instanceof ITaxonBaseDetailSection)) {
92
			((ITaxonBaseDetailSection) formSection)
93
					.setTaxonBase((TaxonBase<?>) input);
94
			return true;
95
		}
96
		else if (input.getClass().equals(DerivedUnit.class)
97
				&& (formSection instanceof IDerivedUnitFacadeDetailSection)) {
98

    
99
			try {
100
				input = DerivedUnitFacade.NewInstance((DerivedUnit) input,
101
						PreferencesUtil.getDerivedUnitConfigurator());
102
			} catch (DerivedUnitFacadeNotSupportedException e) {
103
				MessagingUtils.error(getClass(), e);
104
			}
105
		}
106
		else if (input instanceof PolytomousKeyRelationship) {
107
			input = ((PolytomousKeyRelationship) input).getDestination();
108
			if ((input instanceof PolytomousKeyNode) &&
109
	                (formSection instanceof PolytomousKeyDetailSection ||
110
	                        formSection instanceof GeoScopeDetailSection ||
111
	                        formSection instanceof ScopeRestrictionSection ||
112
	                        formSection instanceof TaxonomicScopeSection)) {
113
	            input = ((PolytomousKeyNode)input).getKey();
114
			}
115
		}
116

    
117
		else if ((input instanceof PolytomousKeyNode) &&
118
	            (formSection instanceof PolytomousKeyDetailSection ||
119
	                    formSection instanceof GeoScopeDetailSection ||
120
	                    formSection instanceof ScopeRestrictionSection ||
121
	                    formSection instanceof TaxonomicScopeSection)) {
122
	        input = ((PolytomousKeyNode)input).getKey();
123
	    } else if ((input instanceof TaxonRelationship)
124
				&& (formSection instanceof TaxonDetailSection)) {
125
			((TaxonDetailSection) formSection)
126
					.setTaxon( ((TaxonRelationship)input).getFromTaxon());
127
			return true;
128
		}else if ((input instanceof TaxonRelationship)
129
				&& (formSection instanceof NonViralNameDetailSection)) {
130
			((NonViralNameDetailSection) formSection)
131
					.setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
132
			return true;
133
		}else if ((input instanceof TaxonRelationship)
134
				&& (formSection instanceof NomenclaturalSourceDetailSection)) {
135
			((NomenclaturalSourceDetailSection) formSection)
136
					.setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
137
			return true;
138
		}else if ((input instanceof TaxonRelationship)
139
				&& (formSection instanceof NomenclaturalStatusSection)) {
140
			((NomenclaturalStatusSection) formSection)
141
					.setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
142
			return true;
143
		}else if ((input instanceof TaxonRelationship)
144
				&& (formSection instanceof TypeDesignationSection)) {
145
			((TypeDesignationSection) formSection)
146
					.setTaxonBase( ((TaxonRelationship)input).getFromTaxon());
147
//			.setEntity( ((TaxonRelationship)input).getFromTaxon().getName());
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
		}else if ((input instanceof DerivedUnitFacade)
155
		        && (formSection instanceof RightsSection)){
156
		    ((RightsSection)formSection).setEntity(((DerivedUnitFacade)input).innerDerivedUnit());
157
		    return true;
158
		}else if ((input instanceof DerivedUnitFacade)
159
                && (formSection instanceof DeterminationDetailSection)){
160
		    DerivedUnitFacade facade = ((DerivedUnitFacade)input);
161
		    SpecimenOrObservationBase<?> sob = facade.innerDerivedUnit() != null? facade.innerDerivedUnit(): facade.innerFieldUnit();
162
            ((DeterminationDetailSection)formSection).setEntity(sob);
163
            return true;
164
		}else if ((input instanceof DerivedUnitFacade)
165
                && (formSection instanceof SampleDesignationDetailSection)){
166
            ((SampleDesignationDetailSection)formSection).setEntity(((DerivedUnitFacade)input).innerDerivedUnit());
167
            return true;
168
		}
169
		//check whether this is needed
170
//	}else if ((input instanceof TaxonBase)
171
//            && (formSection instanceof TypeDesignationSection)){
172
//        ((TypeDesignationSection)formSection).setEntity(((TaxonBase)input).getName());
173
//        return true;
174
//    }
175

    
176
		formSection.setEntity((T) input);
177

    
178

    
179
		return true;
180
	}
181

    
182
	@Override
183
	public void propertyChange(PropertyChangeEvent event) {
184
		if (event != null) {
185

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

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

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