2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.editor
.view
.media
;
12 import java
.util
.Collections
;
13 import java
.util
.HashSet
;
14 import java
.util
.List
;
17 import org
.eclipse
.core
.runtime
.Assert
;
18 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
19 import org
.eclipse
.jface
.viewers
.Viewer
;
21 import eu
.etaxonomy
.cdm
.api
.facade
.DerivedUnitFacade
;
22 import eu
.etaxonomy
.cdm
.api
.facade
.DerivedUnitFacadeNotSupportedException
;
23 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
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
.occurrence
.FieldUnit
;
30 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationType
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
32 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditor
;
33 import eu
.etaxonomy
.taxeditor
.editor
.TaxonEditorInput
;
34 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
37 * <p>MediaContentProvider class.</p>
40 * @created Jun 15, 2010
43 public class MediaContentProvider
implements ITreeContentProvider
{
45 private static final Object
[] NO_CHILDREN
= new Object
[0];
49 public Object
[] getChildren(Object parentElement
) {
51 if (parentElement
instanceof TaxonEditorInput
|| parentElement
instanceof Taxon
) {
54 if (parentElement
instanceof TaxonEditorInput
){
55 taxon
= ((TaxonEditorInput
) parentElement
).getTaxon();
57 taxon
= HibernateProxyHelper
.deproxy(parentElement
, Taxon
.class);
61 MessagingUtils
.error(getClass(), "Taxon is null", null); //$NON-NLS-1$
64 HashSet
<DescriptionBase
> imageGalleries
= new HashSet
<DescriptionBase
>();
65 for(DescriptionBase description
: taxon
.getDescriptions()){
66 if(description
.isImageGallery()){
67 imageGalleries
.add(description
);
70 return imageGalleries
.toArray();
72 else if (parentElement
instanceof DescriptionBase
) {
73 if (((DescriptionBase
) parentElement
).isImageGallery()) {
74 List
<Media
> images
= getImages((DescriptionBase
) parentElement
);
76 return images
.toArray();
82 else if (parentElement
instanceof DerivedUnit
){
84 DerivedUnitFacade facade
= DerivedUnitFacade
.NewInstance((DerivedUnit
) parentElement
);
86 SpecimenDescription derivedUnitImageGallery
= facade
.getDerivedUnitImageGallery(false);
88 if(derivedUnitImageGallery
!= null){
89 return Collections
.singleton(derivedUnitImageGallery
).toArray();
92 } catch (DerivedUnitFacadeNotSupportedException e
) {
93 MessagingUtils
.error(this.getClass(), "DerivedUnitFacadeNotSupportedException when trying to instantiate DerivedUnitFacade", e
); //$NON-NLS-1$
96 else if (parentElement
instanceof FieldUnit
){
97 DerivedUnitFacade facade
= DerivedUnitFacade
.NewInstance(SpecimenOrObservationType
.FieldUnit
, (FieldUnit
) parentElement
);
99 SpecimenDescription fieldObjectImageGallery
= facade
.getFieldObjectImageGallery(false);
101 if(fieldObjectImageGallery
!= null){
102 return Collections
.singleton(fieldObjectImageGallery
).toArray();
110 public Object
getParent(Object element
) {
111 // TODO Auto-generated method stub
117 public boolean hasChildren(Object element
) {
118 if (getChildren(element
)== null) {
121 return (getChildren(element
).length
> 0);
126 public Object
[] getElements(Object inputElement
) {
127 return getChildren(inputElement
);
134 public void dispose() {}
138 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {}
140 private List
<Media
> getImages(DescriptionBase description
){
141 Assert
.isTrue(description
.isImageGallery(), "Description should have the imageGallery flag set."); //$NON-NLS-1$
143 Set
<DescriptionElementBase
> elements
= description
.getElements();
144 if (elements
!= null) {
145 // by definition, image galleries have only one description element
146 if(elements
.size() > 1){
147 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); //$NON-NLS-1$ //$NON-NLS-2$
149 if (!elements
.isEmpty()){
150 DescriptionElementBase element
= elements
.iterator().next();
151 return element
.getMedia();
159 private SpecimenDescription
createDerivedUnitFacadeImageGallery(DerivedUnitFacade facade
){
160 SpecimenDescription description
= SpecimenDescription
.NewInstance();
161 description
.setImageGallery(true);