fixes #888 and #889
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / description / TaxonDescriptionEditor.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.HashSet;
13 import java.util.Set;
14
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.ui.IEditorInput;
18 import org.eclipse.ui.IEditorSite;
19 import org.eclipse.ui.PartInitException;
20 import org.eclipse.ui.Saveable;
21
22 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
23 import eu.etaxonomy.cdm.model.description.Feature;
24 import eu.etaxonomy.cdm.model.description.TaxonDescription;
25 import eu.etaxonomy.taxeditor.editor.AbstractTaxonEditor;
26 import eu.etaxonomy.taxeditor.editor.FreeTextElementFactory;
27 import eu.etaxonomy.taxeditor.editor.GroupedComposite;
28 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
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>DescriptionFeatureGroupComposite</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 * @author n.hoffmann
46 * @created 25.08.2008
47 * @version 1.0
48 */
49 public class TaxonDescriptionEditor extends AbstractTaxonEditor
50 implements IDescriptionEditorCompositeRepository{
51
52 private static final String ID = "eu.etaxonomy.taxeditor.taxonDescriptionEditor";
53
54 public TaxonDescriptionEditor(MultiPageTaxonEditor editor){
55 super(editor);
56 }
57
58 /**
59 * If this view is opened and the <code>Taxon</code> has no <code>Description</code>,
60 * a <code>Description</code> element is created and saved to <code>emptyDescription</code>.
61 * It should not be added to the <code>Taxon</code> until it has been changed.
62 *
63 * @see firePropertyChange(int)
64 */
65 private TaxonDescription emptyDescription;
66
67 private boolean pageInitialized = false;
68
69 @Override
70 public void doSave(IProgressMonitor monitor) {}
71
72 @Override
73 public void doSaveAs() {}
74
75 @Override
76 public void init(IEditorSite site, IEditorInput input)
77 throws PartInitException {
78
79 super.init(site, input);
80 }
81
82 @Override
83 public boolean isDirty() {
84 return false;
85 }
86
87 @Override
88 public boolean isSaveAsAllowed() {
89 return false;
90 }
91
92 @Override
93 protected void createManagedForm(Composite composite) {
94
95 super.createManagedForm(composite);
96
97 // FIXME image description makes this all very clumsy rethink immediately
98 Set<TaxonDescription> descriptions = taxon.getDescriptions();
99
100 Set<TaxonDescription> nonGalleryDescriptions = new HashSet<TaxonDescription>();
101
102 for (TaxonDescription description : descriptions){
103 if( ! description.isImageGallery()){
104 nonGalleryDescriptions.add(description);
105 }
106 }
107
108
109 // If the taxon has no descriptions, give it an empty one to display
110 if (nonGalleryDescriptions == null || nonGalleryDescriptions.size() == 0) {
111 emptyDescription = TaxonDescription.NewInstance();
112 TaxonDescription description = emptyDescription;
113 description.setTitleCache("");
114 descriptions.add(description);
115
116 }
117
118
119 firstGroupedComposite = null;
120
121 // FIXME image gallery stuff makes this all very clumsy
122 //
123 // Iterate through taxon's descriptions
124 for (TaxonDescription description : descriptions) {
125 if(description.isImageGallery()){
126 // image galleries will be handled in image editor
127 }else{
128 // Draw description label
129 Composite labelComposite =
130 FreeTextElementFactory.getDefault().createDescriptionLabel(this, description);
131
132 // Note whether this is the first label drawn
133 if (firstGroupedComposite == null) {
134 firstGroupedComposite = (GroupedComposite) labelComposite;
135 }
136
137 // Draw each element in description
138 for (DescriptionElementBase element : description.getElements()) {
139 FreeTextElementFactory.getDefault().createDescriptionElement(this, description, element);
140 }
141 }
142 }
143
144 // Redraw composite
145 composite.layout();
146
147 pageInitialized = true;
148 }
149
150 /* (non-Javadoc)
151 * @see org.eclipse.ui.part.WorkbenchPart#firePropertyChange(int)
152 */
153 protected void firePropertyChange(final int propertyId) {
154 if (pageInitialized && propertyId == PROP_DIRTY && emptyDescription != null) {
155 getTaxon().addDescription(emptyDescription);
156 emptyDescription = null;
157 }
158 super.firePropertyChange(propertyId);
159 }
160
161
162 public Composite getDescriptionElement(DescriptionElementBase element) {
163 for(Composite c : getGroupedComposites()){
164 if(c instanceof DescriptionElementComposite){
165 if (element.equals
166 (((DescriptionElementComposite) c).getElement())) {
167 return c;
168 }
169 }
170 }
171 return null;
172 }
173
174
175 public Composite getDescriptionFeatureGroup(TaxonDescription description,
176 Feature feature) {
177 for(Composite c : getGroupComposites()){
178 if(c instanceof DescriptionFeatureGroupComposite){
179 if (description.equals(((DescriptionFeatureGroupComposite) c).getDescription()) &&
180 feature.equals(((DescriptionFeatureGroupComposite) c).getFeature())) {
181 return c;
182 }
183 }
184 }
185 return null;
186 }
187
188
189 public Composite getDescriptionGroup(TaxonDescription description) {
190 for(Composite c : getGroupComposites()){
191 if(c instanceof DescriptionGroupComposite){
192 if (description.equals
193 (((DescriptionGroupComposite) c).getDescription())) {
194 return c;
195 }
196 }
197 }
198 return null;
199 }
200
201 public Composite getDescriptionLabel(TaxonDescription description) {
202 for(Composite c : getGroupedComposites()){
203 if(c instanceof DescriptionLabelComposite){
204 if (description.equals
205 (((DescriptionLabelComposite) c).getDescription())) {
206 return c;
207 }
208 }
209 }
210 return null;
211 }
212
213 public Saveable[] getActiveSaveables() {
214 return null;
215 }
216
217 public Saveable[] getSaveables() {
218 return null;
219 }
220
221 /* (non-Javadoc)
222 * @see eu.etaxonomy.taxeditor.editor.AbstractTaxonEditor#getID()
223 */
224 @Override
225 public String getID() {
226 return TaxonDescriptionEditor.ID;
227 }
228
229 /* (non-Javadoc)
230 * @see eu.etaxonomy.taxeditor.editor.AbstractTaxonEditor#resetGroupComposites()
231 */
232 @Override
233 protected void resetGroupComposites() {
234 // TODO Auto-generated method stub
235
236 }
237 }