had to rename the packages to make them compliant with buckminster
[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.ArrayList;
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.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.DerivedUnitBase;
29 import eu.etaxonomy.cdm.model.taxon.Taxon;
30 import eu.etaxonomy.taxeditor.editor.EditorUtil;
31 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
32
33 /**
34 * <p>MediaContentProvider class.</p>
35 *
36 * @author n.hoffmann
37 * @created Jun 15, 2010
38 * @version 1.0
39 */
40 public class MediaContentProvider implements ITreeContentProvider{
41
42 private static final Object[] NO_CHILDREN = new Object[0];
43
44 /** {@inheritDoc} */
45 public Object[] getChildren(Object parentElement) {
46
47 if (parentElement instanceof TaxonEditorInput) {
48 Taxon taxon = ((TaxonEditorInput) parentElement).getTaxon();
49 HashSet<DescriptionBase> imageGalleries = new HashSet<DescriptionBase>();
50 for(DescriptionBase description : taxon.getDescriptions()){
51 if(description.isImageGallery()){
52 imageGalleries.add(description);
53 }
54 }
55 return imageGalleries.toArray();
56 }
57 else if (parentElement instanceof DescriptionBase) {
58 if (((DescriptionBase) parentElement).isImageGallery()) {
59 return getImages((DescriptionBase) parentElement).toArray();
60 }
61 }
62 else if (parentElement instanceof DerivedUnitBase){
63 try {
64 DerivedUnitFacade facade = DerivedUnitFacade.NewInstance((DerivedUnitBase) parentElement);
65
66 // TODO at the moment we always create image galleries because the facade is like so.
67 // this should definitely change and we want to use normal getters and setters here
68 List<DescriptionBase> derivedUnitFacadeImageGalleries = new ArrayList<DescriptionBase>(2);
69
70 SpecimenDescription derivedUnitImageGallery = facade.getDerivedUnitImageGallery(false);
71 SpecimenDescription fieldObjectImageGallery = facade.getFieldObjectImageGallery(false);
72
73 if(derivedUnitImageGallery != null){
74 derivedUnitFacadeImageGalleries.add(derivedUnitImageGallery);
75 }
76
77 if(fieldObjectImageGallery != null){
78 derivedUnitFacadeImageGalleries.add(fieldObjectImageGallery);
79 }
80
81 return derivedUnitFacadeImageGalleries.toArray();
82
83 } catch (DerivedUnitFacadeNotSupportedException e) {
84 EditorUtil.error(this.getClass(), "DerivedUnitFacadeNotSupportedException when trying to instantiate DerivedUnitFacade", e);
85 }
86 }
87
88 return NO_CHILDREN;
89 }
90
91 /** {@inheritDoc} */
92 public Object getParent(Object element) {
93 // TODO Auto-generated method stub
94 return null;
95 }
96
97 /** {@inheritDoc} */
98 public boolean hasChildren(Object element) {
99 return (getChildren(element).length > 0);
100 }
101
102 /** {@inheritDoc} */
103 public Object[] getElements(Object inputElement) {
104 return getChildren(inputElement);
105 }
106
107 /**
108 * <p>dispose</p>
109 */
110 public void dispose() {}
111
112 /** {@inheritDoc} */
113 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
114
115 private List<Media> getImages(DescriptionBase description){
116 Assert.isTrue(description.isImageGallery(), "Description should have the imageGallery flag set.");
117
118 Set<DescriptionElementBase> elements = description.getElements();
119 if (elements != null) {
120 // by definition, image galleries have only one description element
121 if(elements.size() > 1){
122 EditorUtil.error(this.getClass(), "There should be one and only one description element to hold the images. Found: " + elements.size() + " InDescription ID: " + description.getUuid(), null);
123 }
124
125 DescriptionElementBase element = elements.iterator().next();
126
127 return element.getMedia();
128 }
129 return null;
130 }
131
132 private SpecimenDescription createDerivedUnitFacadeImageGallery(DerivedUnitFacade facade){
133 SpecimenDescription description = SpecimenDescription.NewInstance();
134 description.setImageGallery(true);
135
136
137
138 return description;
139 }
140 }