show media information for taxa in bulk editor
[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.Collections;
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.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;
36
37 /**
38 * <p>MediaContentProvider class.</p>
39 *
40 * @author n.hoffmann
41 * @created Jun 15, 2010
42 * @version 1.0
43 */
44 public class MediaContentProvider implements ITreeContentProvider{
45
46 private static final Object[] NO_CHILDREN = new Object[0];
47
48 /** {@inheritDoc} */
49 @Override
50 public Object[] getChildren(Object parentElement) {
51
52 if (parentElement instanceof TaxonEditorInput || parentElement instanceof Taxon) {
53
54 Taxon taxon = null;
55 if (parentElement instanceof TaxonEditorInput){
56 taxon = ((TaxonEditorInput) parentElement).getTaxon();
57 } else{
58 taxon = HibernateProxyHelper.deproxy(parentElement, Taxon.class);
59 }
60
61 if(taxon == null){
62 MessagingUtils.error(getClass(), "Taxon is null", null);
63 return NO_CHILDREN;
64 }
65 HashSet<DescriptionBase> imageGalleries = new HashSet<DescriptionBase>();
66 for(DescriptionBase description : taxon.getDescriptions()){
67 if(description.isImageGallery()){
68 imageGalleries.add(description);
69 }
70 }
71 return imageGalleries.toArray();
72 }
73 else if (parentElement instanceof DescriptionBase) {
74 if (((DescriptionBase) parentElement).isImageGallery()) {
75 return getImages((DescriptionBase) parentElement).toArray();
76 }
77 }
78 else if (parentElement instanceof DerivedUnit){
79 try {
80 DerivedUnitFacade facade = DerivedUnitFacade.NewInstance((DerivedUnit) parentElement);
81
82 SpecimenDescription derivedUnitImageGallery = facade.getDerivedUnitImageGallery(false);
83
84 if(derivedUnitImageGallery != null){
85 return Collections.singleton(derivedUnitImageGallery).toArray();
86 }
87
88 } catch (DerivedUnitFacadeNotSupportedException e) {
89 MessagingUtils.error(this.getClass(), "DerivedUnitFacadeNotSupportedException when trying to instantiate DerivedUnitFacade", e);
90 }
91 }
92 else if (parentElement instanceof FieldUnit){
93 DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.FieldUnit, (FieldUnit) parentElement);
94
95 SpecimenDescription fieldObjectImageGallery = facade.getFieldObjectImageGallery(false);
96
97 if(fieldObjectImageGallery != null){
98 return Collections.singleton(fieldObjectImageGallery).toArray();
99 }
100 }
101 return NO_CHILDREN;
102 }
103
104 /** {@inheritDoc} */
105 @Override
106 public Object getParent(Object element) {
107 // TODO Auto-generated method stub
108 return null;
109 }
110
111 /** {@inheritDoc} */
112 @Override
113 public boolean hasChildren(Object element) {
114 return (getChildren(element).length > 0);
115 }
116
117 /** {@inheritDoc} */
118 @Override
119 public Object[] getElements(Object inputElement) {
120 return getChildren(inputElement);
121 }
122
123 /**
124 * <p>dispose</p>
125 */
126 @Override
127 public void dispose() {}
128
129 /** {@inheritDoc} */
130 @Override
131 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
132
133 private List<Media> getImages(DescriptionBase description){
134 Assert.isTrue(description.isImageGallery(), "Description should have the imageGallery flag set.");
135
136 Set<DescriptionElementBase> elements = description.getElements();
137 if (elements != null) {
138 // by definition, image galleries have only one description element
139 if(elements.size() > 1){
140 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);
141 }
142
143 DescriptionElementBase element = elements.iterator().next();
144
145 return element.getMedia();
146 }
147 return null;
148 }
149
150 private SpecimenDescription createDerivedUnitFacadeImageGallery(DerivedUnitFacade facade){
151 SpecimenDescription description = SpecimenDescription.NewInstance();
152 description.setImageGallery(true);
153
154
155
156 return description;
157 }
158 }