Project

General

Profile

Download (8.25 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.TypeDesignationSection;
40
import eu.etaxonomy.taxeditor.ui.section.occurrence.GeoScopeDetailSection;
41
import eu.etaxonomy.taxeditor.ui.section.occurrence.IDerivedUnitFacadeDetailSection;
42
import eu.etaxonomy.taxeditor.ui.section.reference.NomenclaturalReferenceDetailSection;
43
import eu.etaxonomy.taxeditor.ui.section.supplemental.RightsSection;
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
//			.setEntity( ((TaxonRelationship)input).getFromTaxon().getName());
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
		//check whether this is needed
171
//	}else if ((input instanceof TaxonBase)
172
//            && (formSection instanceof TypeDesignationSection)){
173
//        ((TypeDesignationSection)formSection).setEntity(((TaxonBase)input).getName());
174
//        return true;
175
//    }
176

    
177

    
178
		formSection.setEntity((T) input);
179

    
180
		return true;
181
	}
182

    
183
	/*
184
	 * (non-Javadoc)
185
	 *
186
	 * @see
187
	 * org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse
188
	 * .jface.util.PropertyChangeEvent)
189
	 */
190
	/** {@inheritDoc} */
191
	@Override
192
	public void propertyChange(PropertyChangeEvent event) {
193
		if (event != null) {
194

    
195
			Object eventSource = event.getSource();
196
			if (eventSource instanceof EventObject){
197
				eventSource = ((EventObject)eventSource).getSource();
198
			}
199
			Control[] children = formSection.getLayoutComposite().getChildren();
200
			boolean containsElement = false;
201
			for (Control control : children) {
202
			    if(eventSource.equals(control)){
203
			        containsElement = true;
204
			        break;
205
			    }
206
            }
207
			if(containsElement){
208
			    markDirty();
209
			}
210

    
211
			else if (formSection.equals(eventSource)){
212
				markDirty();
213
			}
214
			else if(((eventSource instanceof ICdmFormElement)&& formSection.containsFormElement((ICdmFormElement) eventSource))) {
215
			    markDirty();
216
			}
217
		}
218
	}
219

    
220
	@Override
221
	protected void expansionStateChanged(boolean expanded) {
222
	    super.expansionStateChanged(expanded);
223
	    PreferencesUtil.setStringValue(StoreUtil.getPrefKey(formSection.getClass(), rootInput.getClass().getCanonicalName()), expanded?EXPANDED:COLLAPSED);
224
	}
225

    
226
}
    (1-1/1)