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
.editor
.e4
.TaxonEditorInputE4
;
33 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
36 * <p>MediaContentProvider class.</p>
39 * @created Jun 15, 2010
41 public class MediaContentProvider
implements ITreeContentProvider
{
43 private static final Object
[] NO_CHILDREN
= new Object
[0];
47 public Object
[] getChildren(Object parentElement
) {
49 if (parentElement
instanceof TaxonEditorInputE4
|| parentElement
instanceof Taxon
) {
52 if (parentElement
instanceof TaxonEditorInputE4
){
53 taxon
= ((TaxonEditorInputE4
) parentElement
).getTaxon();
55 taxon
= HibernateProxyHelper
.deproxy(parentElement
, Taxon
.class);
59 MessagingUtils
.error(getClass(), "Taxon is null", null); //$NON-NLS-1$
62 HashSet
<DescriptionBase
<?
>> imageGalleries
= new HashSet
<>();
63 for(DescriptionBase
<?
> description
: taxon
.getDescriptions()){
64 if(description
.isImageGallery()){
65 imageGalleries
.add(description
);
68 return imageGalleries
.toArray();
70 else if (parentElement
instanceof DescriptionBase
) {
71 if (((DescriptionBase
<?
>) parentElement
).isImageGallery()) {
72 List
<Media
> images
= getImages((DescriptionBase
<?
>) parentElement
);
74 return images
.toArray();
80 else if (parentElement
instanceof DerivedUnit
){
82 DerivedUnitFacade facade
= DerivedUnitFacade
.NewInstance((DerivedUnit
) parentElement
);
84 SpecimenDescription derivedUnitImageGallery
= facade
.getDerivedUnitImageGallery(false);
86 if(derivedUnitImageGallery
!= null){
87 return Collections
.singleton(derivedUnitImageGallery
).toArray();
90 } catch (DerivedUnitFacadeNotSupportedException e
) {
91 MessagingUtils
.error(this.getClass(), "DerivedUnitFacadeNotSupportedException when trying to instantiate DerivedUnitFacade", e
); //$NON-NLS-1$
94 else if (parentElement
instanceof FieldUnit
){
95 DerivedUnitFacade facade
= DerivedUnitFacade
.NewInstance(SpecimenOrObservationType
.FieldUnit
, (FieldUnit
) parentElement
);
97 SpecimenDescription fieldObjectImageGallery
= facade
.getFieldObjectImageGallery(false);
99 if(fieldObjectImageGallery
!= null){
100 return Collections
.singleton(fieldObjectImageGallery
).toArray();
108 public Object
getParent(Object element
) {
109 // TODO Auto-generated method stub
115 public boolean hasChildren(Object element
) {
116 if (getChildren(element
)== null) {
119 return (getChildren(element
).length
> 0);
124 public Object
[] getElements(Object inputElement
) {
125 return getChildren(inputElement
);
132 public void dispose() {}
136 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {}
138 private List
<Media
> getImages(DescriptionBase description
){
139 Assert
.isTrue(description
.isImageGallery(), "Description should have the imageGallery flag set."); //$NON-NLS-1$
141 Set
<DescriptionElementBase
> elements
= description
.getElements();
142 if (elements
!= null) {
143 // by definition, image galleries have only one description element
144 if(elements
.size() > 1){
145 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$
147 if (!elements
.isEmpty()){
148 DescriptionElementBase element
= elements
.iterator().next();
149 return element
.getMedia();
157 private SpecimenDescription
createDerivedUnitFacadeImageGallery(DerivedUnitFacade facade
){
158 SpecimenDescription description
= SpecimenDescription
.NewInstance();
159 description
.setImageGallery(true);