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
|
List<Media> images = getImages((DescriptionBase) parentElement);
|
76
|
if (images != null){
|
77
|
return images.toArray();
|
78
|
}
|
79
|
return null;
|
80
|
|
81
|
}
|
82
|
}
|
83
|
else if (parentElement instanceof DerivedUnit){
|
84
|
try {
|
85
|
DerivedUnitFacade facade = DerivedUnitFacade.NewInstance((DerivedUnit) parentElement);
|
86
|
|
87
|
SpecimenDescription derivedUnitImageGallery = facade.getDerivedUnitImageGallery(false);
|
88
|
|
89
|
if(derivedUnitImageGallery != null){
|
90
|
return Collections.singleton(derivedUnitImageGallery).toArray();
|
91
|
}
|
92
|
|
93
|
} catch (DerivedUnitFacadeNotSupportedException e) {
|
94
|
MessagingUtils.error(this.getClass(), "DerivedUnitFacadeNotSupportedException when trying to instantiate DerivedUnitFacade", e);
|
95
|
}
|
96
|
}
|
97
|
else if (parentElement instanceof FieldUnit){
|
98
|
DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.FieldUnit, (FieldUnit) parentElement);
|
99
|
|
100
|
SpecimenDescription fieldObjectImageGallery = facade.getFieldObjectImageGallery(false);
|
101
|
|
102
|
if(fieldObjectImageGallery != null){
|
103
|
return Collections.singleton(fieldObjectImageGallery).toArray();
|
104
|
}
|
105
|
}
|
106
|
return NO_CHILDREN;
|
107
|
}
|
108
|
|
109
|
/** {@inheritDoc} */
|
110
|
@Override
|
111
|
public Object getParent(Object element) {
|
112
|
// TODO Auto-generated method stub
|
113
|
return null;
|
114
|
}
|
115
|
|
116
|
/** {@inheritDoc} */
|
117
|
@Override
|
118
|
public boolean hasChildren(Object element) {
|
119
|
if (getChildren(element)== null) {
|
120
|
return false;
|
121
|
}
|
122
|
return (getChildren(element).length > 0);
|
123
|
}
|
124
|
|
125
|
/** {@inheritDoc} */
|
126
|
@Override
|
127
|
public Object[] getElements(Object inputElement) {
|
128
|
return getChildren(inputElement);
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* <p>dispose</p>
|
133
|
*/
|
134
|
@Override
|
135
|
public void dispose() {}
|
136
|
|
137
|
/** {@inheritDoc} */
|
138
|
@Override
|
139
|
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
|
140
|
|
141
|
private List<Media> getImages(DescriptionBase description){
|
142
|
Assert.isTrue(description.isImageGallery(), "Description should have the imageGallery flag set.");
|
143
|
|
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);
|
149
|
}
|
150
|
if (!elements.isEmpty()){
|
151
|
DescriptionElementBase element = elements.iterator().next();
|
152
|
return element.getMedia();
|
153
|
}
|
154
|
|
155
|
|
156
|
}
|
157
|
return null;
|
158
|
}
|
159
|
|
160
|
private SpecimenDescription createDerivedUnitFacadeImageGallery(DerivedUnitFacade facade){
|
161
|
SpecimenDescription description = SpecimenDescription.NewInstance();
|
162
|
description.setImageGallery(true);
|
163
|
|
164
|
|
165
|
|
166
|
return description;
|
167
|
}
|
168
|
}
|