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