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
.Collections
;
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
.hibernate
.HibernateProxyHelper
;
25 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
26 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
27 import eu
.etaxonomy
.cdm
.model
.description
.SpecimenDescription
;
28 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
29 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
30 import eu
.etaxonomy
.cdm
.model
.occurrence
.FieldUnit
;
31 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationType
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
33 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditor
;
34 import eu
.etaxonomy
.taxeditor
.editor
.TaxonEditorInput
;
35 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
38 * <p>MediaContentProvider class.</p>
41 * @created Jun 15, 2010
44 public class MediaContentProvider
implements ITreeContentProvider
{
46 private static final Object
[] NO_CHILDREN
= new Object
[0];
50 public Object
[] getChildren(Object parentElement
) {
52 if (parentElement
instanceof TaxonEditorInput
|| parentElement
instanceof Taxon
) {
55 if (parentElement
instanceof TaxonEditorInput
){
56 taxon
= ((TaxonEditorInput
) parentElement
).getTaxon();
58 taxon
= HibernateProxyHelper
.deproxy(parentElement
, Taxon
.class);
62 MessagingUtils
.error(getClass(), "Taxon is null", null);
65 HashSet
<DescriptionBase
> imageGalleries
= new HashSet
<DescriptionBase
>();
66 for(DescriptionBase description
: taxon
.getDescriptions()){
67 if(description
.isImageGallery()){
68 imageGalleries
.add(description
);
71 return imageGalleries
.toArray();
73 else if (parentElement
instanceof DescriptionBase
) {
74 if (((DescriptionBase
) parentElement
).isImageGallery()) {
75 List
<Media
> images
= getImages((DescriptionBase
) parentElement
);
77 return images
.toArray();
83 else if (parentElement
instanceof DerivedUnit
){
85 DerivedUnitFacade facade
= DerivedUnitFacade
.NewInstance((DerivedUnit
) parentElement
);
87 SpecimenDescription derivedUnitImageGallery
= facade
.getDerivedUnitImageGallery(false);
89 if(derivedUnitImageGallery
!= null){
90 return Collections
.singleton(derivedUnitImageGallery
).toArray();
93 } catch (DerivedUnitFacadeNotSupportedException e
) {
94 MessagingUtils
.error(this.getClass(), "DerivedUnitFacadeNotSupportedException when trying to instantiate DerivedUnitFacade", e
);
97 else if (parentElement
instanceof FieldUnit
){
98 DerivedUnitFacade facade
= DerivedUnitFacade
.NewInstance(SpecimenOrObservationType
.FieldUnit
, (FieldUnit
) parentElement
);
100 SpecimenDescription fieldObjectImageGallery
= facade
.getFieldObjectImageGallery(false);
102 if(fieldObjectImageGallery
!= null){
103 return Collections
.singleton(fieldObjectImageGallery
).toArray();
111 public Object
getParent(Object element
) {
112 // TODO Auto-generated method stub
118 public boolean hasChildren(Object element
) {
119 if (getChildren(element
)== null) {
122 return (getChildren(element
).length
> 0);
127 public Object
[] getElements(Object inputElement
) {
128 return getChildren(inputElement
);
135 public void dispose() {}
139 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {}
141 private List
<Media
> getImages(DescriptionBase description
){
142 Assert
.isTrue(description
.isImageGallery(), "Description should have the imageGallery flag set.");
144 Set
<DescriptionElementBase
> elements
= description
.getElements();
145 if (elements
!= null) {
146 // by definition, image galleries have only one description element
147 if(elements
.size() > 1){
148 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);
150 if (!elements
.isEmpty()){
151 DescriptionElementBase element
= elements
.iterator().next();
152 return element
.getMedia();
160 private SpecimenDescription
createDerivedUnitFacadeImageGallery(DerivedUnitFacade facade
){
161 SpecimenDescription description
= SpecimenDescription
.NewInstance();
162 description
.setImageGallery(true);