3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.editor
.view
.media
;
13 import java
.util
.ArrayList
;
14 import java
.util
.HashSet
;
15 import java
.util
.List
;
18 import org
.eclipse
.core
.runtime
.Assert
;
19 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
20 import org
.eclipse
.jface
.viewers
.Viewer
;
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
;
34 * <p>MediaContentProvider class.</p>
37 * @created Jun 15, 2010
40 public class MediaContentProvider
implements ITreeContentProvider
{
42 private static final Object
[] NO_CHILDREN
= new Object
[0];
45 public Object
[] getChildren(Object parentElement
) {
47 if (parentElement
instanceof TaxonEditorInput
) {
48 Taxon taxon
= ((TaxonEditorInput
) parentElement
).getTaxon();
50 MessagingUtils
.error(getClass(), "Taxon is null", null);
53 HashSet
<DescriptionBase
> imageGalleries
= new HashSet
<DescriptionBase
>();
54 for(DescriptionBase description
: taxon
.getDescriptions()){
55 if(description
.isImageGallery()){
56 imageGalleries
.add(description
);
59 return imageGalleries
.toArray();
61 else if (parentElement
instanceof DescriptionBase
) {
62 if (((DescriptionBase
) parentElement
).isImageGallery()) {
63 return getImages((DescriptionBase
) parentElement
).toArray();
66 else if (parentElement
instanceof DerivedUnit
){
68 DerivedUnitFacade facade
= DerivedUnitFacade
.NewInstance((DerivedUnit
) parentElement
);
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);
74 SpecimenDescription derivedUnitImageGallery
= facade
.getDerivedUnitImageGallery(false);
75 SpecimenDescription fieldObjectImageGallery
= facade
.getFieldObjectImageGallery(false);
77 if(derivedUnitImageGallery
!= null){
78 derivedUnitFacadeImageGalleries
.add(derivedUnitImageGallery
);
81 if(fieldObjectImageGallery
!= null){
82 derivedUnitFacadeImageGalleries
.add(fieldObjectImageGallery
);
85 return derivedUnitFacadeImageGalleries
.toArray();
87 } catch (DerivedUnitFacadeNotSupportedException e
) {
88 MessagingUtils
.error(this.getClass(), "DerivedUnitFacadeNotSupportedException when trying to instantiate DerivedUnitFacade", e
);
96 public Object
getParent(Object element
) {
97 // TODO Auto-generated method stub
102 public boolean hasChildren(Object element
) {
103 return (getChildren(element
).length
> 0);
107 public Object
[] getElements(Object inputElement
) {
108 return getChildren(inputElement
);
114 public void dispose() {}
117 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {}
119 private List
<Media
> getImages(DescriptionBase description
){
120 Assert
.isTrue(description
.isImageGallery(), "Description should have the imageGallery flag set.");
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);
129 DescriptionElementBase element
= elements
.iterator().next();
131 return element
.getMedia();
136 private SpecimenDescription
createDerivedUnitFacadeImageGallery(DerivedUnitFacade facade
){
137 SpecimenDescription description
= SpecimenDescription
.NewInstance();
138 description
.setImageGallery(true);