751b345455d5e526ec16855d904121b747dab9e1
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / media / MediaContentProvider.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.editor.view.media;
12
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Set;
17
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.jface.viewers.ITreeContentProvider;
20 import org.eclipse.jface.viewers.Viewer;
21
22 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
23 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;
24 import eu.etaxonomy.cdm.model.description.DescriptionBase;
25 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
26 import eu.etaxonomy.cdm.model.description.SpecimenDescription;
27 import eu.etaxonomy.cdm.model.media.Media;
28 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
29 import eu.etaxonomy.cdm.model.taxon.Taxon;
30 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
31 import eu.etaxonomy.taxeditor.model.MessagingUtils;
32
33 /**
34 * <p>MediaContentProvider class.</p>
35 *
36 * @author n.hoffmann
37 * @created Jun 15, 2010
38 * @version 1.0
39 */
40 public class MediaContentProvider implements ITreeContentProvider{
41
42 private static final Object[] NO_CHILDREN = new Object[0];
43
44 /** {@inheritDoc} */
45 public Object[] getChildren(Object parentElement) {
46
47 if (parentElement instanceof TaxonEditorInput) {
48 Taxon taxon = ((TaxonEditorInput) parentElement).getTaxon();
49 if(taxon == null){
50 MessagingUtils.error(getClass(), "Taxon is null", null);
51 return NO_CHILDREN;
52 }
53 HashSet<DescriptionBase> imageGalleries = new HashSet<DescriptionBase>();
54 for(DescriptionBase description : taxon.getDescriptions()){
55 if(description.isImageGallery()){
56 imageGalleries.add(description);
57 }
58 }
59 return imageGalleries.toArray();
60 }
61 else if (parentElement instanceof DescriptionBase) {
62 if (((DescriptionBase) parentElement).isImageGallery()) {
63 return getImages((DescriptionBase) parentElement).toArray();
64 }
65 }
66 else if (parentElement instanceof DerivedUnit){
67 try {
68 DerivedUnitFacade facade = DerivedUnitFacade.NewInstance((DerivedUnit) parentElement);
69
70 // TODO at the moment we always create image galleries because the facade is like so.
71 // this should definitely change and we want to use normal getters and setters here
72 List<DescriptionBase> derivedUnitFacadeImageGalleries = new ArrayList<DescriptionBase>(2);
73
74 SpecimenDescription derivedUnitImageGallery = facade.getDerivedUnitImageGallery(false);
75 SpecimenDescription fieldObjectImageGallery = facade.getFieldObjectImageGallery(false);
76
77 if(derivedUnitImageGallery != null){
78 derivedUnitFacadeImageGalleries.add(derivedUnitImageGallery);
79 }
80
81 if(fieldObjectImageGallery != null){
82 derivedUnitFacadeImageGalleries.add(fieldObjectImageGallery);
83 }
84
85 return derivedUnitFacadeImageGalleries.toArray();
86
87 } catch (DerivedUnitFacadeNotSupportedException e) {
88 MessagingUtils.error(this.getClass(), "DerivedUnitFacadeNotSupportedException when trying to instantiate DerivedUnitFacade", e);
89 }
90 }
91
92 return NO_CHILDREN;
93 }
94
95 /** {@inheritDoc} */
96 public Object getParent(Object element) {
97 // TODO Auto-generated method stub
98 return null;
99 }
100
101 /** {@inheritDoc} */
102 public boolean hasChildren(Object element) {
103 return (getChildren(element).length > 0);
104 }
105
106 /** {@inheritDoc} */
107 public Object[] getElements(Object inputElement) {
108 return getChildren(inputElement);
109 }
110
111 /**
112 * <p>dispose</p>
113 */
114 public void dispose() {}
115
116 /** {@inheritDoc} */
117 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
118
119 private List<Media> getImages(DescriptionBase description){
120 Assert.isTrue(description.isImageGallery(), "Description should have the imageGallery flag set.");
121
122 Set<DescriptionElementBase> elements = description.getElements();
123 if (elements != null) {
124 // by definition, image galleries have only one description element
125 if(elements.size() > 1){
126 MessagingUtils.error(this.getClass(), "There should be one and only one description element to hold the images. Found: " + elements.size() + " InDescription ID: " + description.getUuid(), null);
127 }
128
129 DescriptionElementBase element = elements.iterator().next();
130
131 return element.getMedia();
132 }
133 return null;
134 }
135
136 private SpecimenDescription createDerivedUnitFacadeImageGallery(DerivedUnitFacade facade){
137 SpecimenDescription description = SpecimenDescription.NewInstance();
138 description.setImageGallery(true);
139
140
141
142 return description;
143 }
144 }