1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
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.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.taxeditor.editor.view.media;
|
11
|
|
12
|
import java.util.Collections;
|
13
|
import java.util.HashSet;
|
14
|
import java.util.List;
|
15
|
import java.util.Set;
|
16
|
|
17
|
import org.eclipse.core.runtime.Assert;
|
18
|
import org.eclipse.jface.viewers.ITreeContentProvider;
|
19
|
import org.eclipse.jface.viewers.Viewer;
|
20
|
|
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;
|
34
|
|
35
|
/**
|
36
|
* <p>MediaContentProvider class.</p>
|
37
|
*
|
38
|
* @author n.hoffmann
|
39
|
* @created Jun 15, 2010
|
40
|
*/
|
41
|
public class MediaContentProvider implements ITreeContentProvider{
|
42
|
|
43
|
private static final Object[] NO_CHILDREN = new Object[0];
|
44
|
|
45
|
/** {@inheritDoc} */
|
46
|
@Override
|
47
|
public Object[] getChildren(Object parentElement) {
|
48
|
|
49
|
if (parentElement instanceof TaxonEditorInputE4 || parentElement instanceof Taxon) {
|
50
|
|
51
|
Taxon taxon = null;
|
52
|
if (parentElement instanceof TaxonEditorInputE4){
|
53
|
taxon = ((TaxonEditorInputE4) parentElement).getTaxon();
|
54
|
} else{
|
55
|
taxon = HibernateProxyHelper.deproxy(parentElement, Taxon.class);
|
56
|
}
|
57
|
|
58
|
if(taxon == null){
|
59
|
MessagingUtils.error(getClass(), "Taxon is null", null); //$NON-NLS-1$
|
60
|
return NO_CHILDREN;
|
61
|
}
|
62
|
HashSet<DescriptionBase<?>> imageGalleries = new HashSet<>();
|
63
|
for(DescriptionBase<?> description : taxon.getDescriptions()){
|
64
|
if(description.isImageGallery()){
|
65
|
imageGalleries.add(description);
|
66
|
}
|
67
|
}
|
68
|
return imageGalleries.toArray();
|
69
|
}
|
70
|
else if (parentElement instanceof DescriptionBase) {
|
71
|
if (((DescriptionBase<?>) parentElement).isImageGallery()) {
|
72
|
List<Media> images = getImages((DescriptionBase<?>) parentElement);
|
73
|
if (images != null){
|
74
|
return images.toArray();
|
75
|
}
|
76
|
return null;
|
77
|
|
78
|
}
|
79
|
}
|
80
|
else if (parentElement instanceof DerivedUnit){
|
81
|
try {
|
82
|
DerivedUnitFacade facade = DerivedUnitFacade.NewInstance((DerivedUnit) parentElement);
|
83
|
|
84
|
SpecimenDescription derivedUnitImageGallery = facade.getDerivedUnitImageGallery(false);
|
85
|
|
86
|
if(derivedUnitImageGallery != null){
|
87
|
return Collections.singleton(derivedUnitImageGallery).toArray();
|
88
|
}
|
89
|
|
90
|
} catch (DerivedUnitFacadeNotSupportedException e) {
|
91
|
MessagingUtils.error(this.getClass(), "DerivedUnitFacadeNotSupportedException when trying to instantiate DerivedUnitFacade", e); //$NON-NLS-1$
|
92
|
}
|
93
|
}
|
94
|
else if (parentElement instanceof FieldUnit){
|
95
|
DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.FieldUnit, (FieldUnit) parentElement);
|
96
|
|
97
|
SpecimenDescription fieldObjectImageGallery = facade.getFieldObjectImageGallery(false);
|
98
|
|
99
|
if(fieldObjectImageGallery != null){
|
100
|
return Collections.singleton(fieldObjectImageGallery).toArray();
|
101
|
}
|
102
|
}
|
103
|
return NO_CHILDREN;
|
104
|
}
|
105
|
|
106
|
/** {@inheritDoc} */
|
107
|
@Override
|
108
|
public Object getParent(Object element) {
|
109
|
// TODO Auto-generated method stub
|
110
|
return null;
|
111
|
}
|
112
|
|
113
|
/** {@inheritDoc} */
|
114
|
@Override
|
115
|
public boolean hasChildren(Object element) {
|
116
|
if (getChildren(element)== null) {
|
117
|
return false;
|
118
|
}
|
119
|
return (getChildren(element).length > 0);
|
120
|
}
|
121
|
|
122
|
/** {@inheritDoc} */
|
123
|
@Override
|
124
|
public Object[] getElements(Object inputElement) {
|
125
|
return getChildren(inputElement);
|
126
|
}
|
127
|
|
128
|
/**
|
129
|
* <p>dispose</p>
|
130
|
*/
|
131
|
@Override
|
132
|
public void dispose() {}
|
133
|
|
134
|
/** {@inheritDoc} */
|
135
|
@Override
|
136
|
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
|
137
|
|
138
|
private List<Media> getImages(DescriptionBase description){
|
139
|
Assert.isTrue(description.isImageGallery(), "Description should have the imageGallery flag set."); //$NON-NLS-1$
|
140
|
|
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$
|
146
|
}
|
147
|
if (!elements.isEmpty()){
|
148
|
DescriptionElementBase element = elements.iterator().next();
|
149
|
return element.getMedia();
|
150
|
}
|
151
|
|
152
|
|
153
|
}
|
154
|
return null;
|
155
|
}
|
156
|
|
157
|
private SpecimenDescription createDerivedUnitFacadeImageGallery(DerivedUnitFacade facade){
|
158
|
SpecimenDescription description = SpecimenDescription.NewInstance();
|
159
|
description.setImageGallery(true);
|
160
|
|
161
|
|
162
|
|
163
|
return description;
|
164
|
}
|
165
|
}
|