e20a5741bdd1f246ffe0e6781eaaa4131bb19247
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / editor / description / TaxonDescriptionEditorView.java
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.editor.description;
11
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Map;
15 import java.util.Set;
16
17 import org.apache.log4j.Logger;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.swt.widgets.Composite;
20
21 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
22 import eu.etaxonomy.cdm.model.description.Feature;
23 import eu.etaxonomy.cdm.model.description.TaxonDescription;
24 import eu.etaxonomy.cdm.model.taxon.Taxon;
25 import eu.etaxonomy.taxeditor.editor.AbstractTaxonEditorView;
26 import eu.etaxonomy.taxeditor.editor.EditorGroupComposite;
27 import eu.etaxonomy.taxeditor.editor.EditorGroupedComposite;
28 import eu.etaxonomy.taxeditor.editor.IHasPropertySource;
29
30 /**
31 * Displays the <code>TaxonDescription</code>s attached to a <code>Taxon</code>.
32 * <p>
33 * For each <code>TaxonDescription</code>:
34 * <ul>
35 * <li>Displays a <code>DescriptionLabelComposite</code> with <code>TaxonDescription#getTextCache()</code></li>
36 * <li>Displays a <code>FeatureGroupComposite</code> for each <code>Feature</code> for which there are descriptive elements</li>
37 * <li>Displays a <code>DescriptionElementComposite</code> for each <code>TaxonDescription#getElements()</code></li>
38 * </ul>
39 * </p>
40 * <p>
41 * If the <Taxon> has no descriptions, creates a <code>TaxonDescription</code> that is saved when
42 * <code>firePropertyChange()</code> is called with <code>PROP_DIRTY</code>.
43 * </p>
44 * @author p.ciardelli
45 * @created 25.08.2008
46 * @version 1.0
47 */
48 public class TaxonDescriptionEditorView extends AbstractTaxonEditorView {
49 private static final Logger logger = Logger
50 .getLogger(TaxonDescriptionEditorView.class);
51
52 /**
53 * If this view is opened and the <code>Taxon</code> has no <code>Description</code>,
54 * a <code>Description</code> element is created and saved to <code>emptyDescription</code>.
55 * It should not be added to the <code>Taxon</code> until it has been changed.
56 *
57 * @see firePropertyChange(int)
58 */
59 private TaxonDescription emptyDescription;
60
61 private boolean pageInitialized = false;
62
63 @Override
64 public void doSave(IProgressMonitor monitor) {
65 // TODO Auto-generated method stub
66
67 }
68
69 @Override
70 public void doSaveAs() {
71 // TODO Auto-generated method stub
72
73 }
74
75 @Override
76 public boolean isDirty() {
77 // TODO Auto-generated method stub
78 return false;
79 }
80
81 @Override
82 public boolean isSaveAsAllowed() {
83 // TODO Auto-generated method stub
84 return false;
85 }
86
87 @Override
88 public void createPartControl(Composite composite) {
89
90 super.createPartControl(composite);
91
92 Taxon taxon = getTaxon();
93
94 Set<TaxonDescription> descriptions = taxon.getDescriptions();
95
96 EditorGroupedComposite firstEditableComposite = null;
97
98 // If the Taxon has no descriptions, give it an empty one to display
99 if (descriptions == null || descriptions.size() == 0) {
100 emptyDescription = TaxonDescription.NewInstance();
101 TaxonDescription description = emptyDescription;
102 description.setTitleCache("");
103 descriptions.add(description);
104 }
105
106 // Iterate through taxon's descriptions
107 for (TaxonDescription description : descriptions) {
108
109 // Create a composite for the description
110 EditorGroupComposite descriptionComposite = new EditorGroupComposite(
111 parent, managedForm, description);
112 descriptionComposite.setDroppable(false);
113
114 // Add a grouped composite for the description's label
115 EditorGroupedComposite descriptionLabelComposite = new DescriptionLabelComposite(
116 descriptionComposite, managedForm, description);
117
118 if (firstEditableComposite == null) {
119 firstEditableComposite = descriptionLabelComposite;
120 }
121
122 // Iterate through description's elements, group by feature
123 Map<Feature, Set<DescriptionElementBase>> features =
124 new HashMap<Feature, Set<DescriptionElementBase>>();
125 for (DescriptionElementBase element : description.getElements()) {
126 Feature feature = element.getFeature();
127 Set<DescriptionElementBase> elements = features.get(feature);
128 if (elements == null) {
129 elements = new HashSet<DescriptionElementBase>();
130 features.put(feature, elements);
131 }
132 elements.add(element);
133 }
134
135 // Create a composite for each feature, composites for its elements
136 for (Feature feature : features.keySet()) {
137 EditorGroupComposite featureComposite = new FeatureGroupComposite(
138 descriptionComposite, managedForm, feature);
139
140 for (DescriptionElementBase element : features.get(feature)) {
141 new DescriptionElementComposite(
142 featureComposite, managedForm, element);
143 }
144 }
145 }
146
147 // We've added elements to the managed form, so redraw it
148 scrolledForm.reflow(true);
149
150 // Set focus on the first label composite to give a border and send it
151 // to the property sheet
152 setSelection(firstEditableComposite);
153 firstEditableComposite.drawBorder();
154
155 pageInitialized = true;
156 }
157
158 /* (non-Javadoc)
159 * @see org.eclipse.ui.part.WorkbenchPart#firePropertyChange(int)
160 */
161 protected void firePropertyChange(final int propertyId) {
162 if (pageInitialized && propertyId == PROP_DIRTY && emptyDescription != null) {
163 getTaxon().addDescription(emptyDescription);
164 emptyDescription = null;
165 }
166 super.firePropertyChange(propertyId);
167 }
168 }