3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.model
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Collections
;
15 import java
.util
.HashSet
;
16 import java
.util
.List
;
19 import org
.apache
.log4j
.Logger
;
21 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
22 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
23 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
24 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
25 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
26 import eu
.etaxonomy
.cdm
.model
.media
.ImageFile
;
27 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
28 import eu
.etaxonomy
.cdm
.model
.media
.MediaRepresentation
;
29 import eu
.etaxonomy
.cdm
.model
.media
.MediaRepresentationPart
;
30 import eu
.etaxonomy
.cdm
.model
.media
.ReferencedMedia
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
38 * This has to be moved to library at some point. Unfortunately, the whole handling of images
39 * is very half heartedly implemented in the library. We will not mark this here as depreciated
40 * until the situation in the library improves.
43 public class ImagesHelper
{
44 private static final Logger logger
= Logger
45 .getLogger(ImagesHelper
.class);
48 * Quick and dirty method to get an element's first image file.
53 public static ImageFile
getImage(DescriptionElementBase element
) {
54 List
<Media
> medias
= element
.getMedia();
56 for(Media media
: medias
){
57 Set
<MediaRepresentation
> representations
= media
.getRepresentations();
59 for(MediaRepresentation representation
: representations
){
60 List
<MediaRepresentationPart
> parts
= representation
.getParts();
62 for (MediaRepresentationPart part
: parts
){
63 if(part
instanceof ImageFile
){
64 return (ImageFile
) part
;
72 public static List
<ImageFile
> getOrderedImages(DescriptionElementBase element
){
73 List
<ImageFile
> imageList
= new ArrayList
<ImageFile
>();
74 MediaRepresentation representation
= getImageMediaRepresentation(element
);
75 if (representation
!= null) {
76 for (MediaRepresentationPart part
: representation
.getParts()){
77 if(!(part
instanceof ImageFile
)){
78 throw new RuntimeException("Your database contains media that mix Image Files with non-Image Files.");
80 imageList
.add((ImageFile
) part
);
88 * Returns the first Representation with images. If none is found, a
89 * Representation for storing images is created and returned.
94 private static MediaRepresentation
getImageMediaRepresentation(DescriptionElementBase element
) {
95 // Drill down until a representation with images is found
96 for(Media media
: element
.getMedia()){
97 Set
<MediaRepresentation
> representations
= media
.getRepresentations();
98 for(MediaRepresentation representation
: representations
){
99 List
<MediaRepresentationPart
> parts
= representation
.getParts();
100 for (MediaRepresentationPart part
: parts
){
101 if(part
instanceof ImageFile
){
102 return representation
;
107 // No representation with images found - create
108 MediaRepresentation representation
= MediaRepresentation
.NewInstance();
109 Media media
= ReferencedMedia
.NewInstance();
110 element
.addMedia(media
);
111 media
.addRepresentation(representation
);
112 return representation
;
119 public static Set
<ImageFile
> getImages(TaxonDescription description
){
120 Set
<ImageFile
> images
= new HashSet
<ImageFile
>();
122 for (DescriptionElementBase element
: description
.getElements()){
124 Feature feature
= element
.getFeature();
126 if(feature
.equals(Feature
.IMAGE())){
127 List
<Media
> medias
= element
.getMedia();
129 for(Media media
: medias
){
130 Set
<MediaRepresentation
> representations
= media
.getRepresentations();
132 for(MediaRepresentation representation
: representations
){
133 List
<MediaRepresentationPart
> parts
= representation
.getParts();
135 for (MediaRepresentationPart part
: parts
){
136 if(part
instanceof ImageFile
){
137 images
.add((ImageFile
) part
);
152 public static void addTaxonImage(Taxon taxon
, DescriptionBase
<?
> imageGallery
, ImageFile imageFile
) {
154 imageGallery
.addElement(createImageElement(imageFile
));
158 public static DescriptionElementBase
createImageElement(ImageFile imageFile
) {
160 DescriptionElementBase descriptionElement
= TextData
.NewInstance(Feature
.IMAGE());
162 Media media
= Media
.NewInstance();
163 MediaRepresentation representation
= MediaRepresentation
.NewInstance();
165 representation
.addRepresentationPart(imageFile
);
167 media
.addRepresentation(representation
);
169 descriptionElement
.addMedia(media
);
171 return descriptionElement
;
176 * Adds a new, empty image file to the end of a description element's
177 * ordered list of images.
182 public static ImageFile
addImagePart(DescriptionElementBase element
) {
183 ImageFile imageFile
= ImageFile
.NewInstance(null, null);
184 getImageMediaRepresentation(element
).addRepresentationPart(imageFile
);
192 public static void removeTaxonImage(Taxon taxon
, DescriptionBase
<?
> imageGallery
, ImageFile imageFile
) {
193 Set
<DescriptionElementBase
> descriptionElementsToRemove
= new HashSet
<DescriptionElementBase
>();
194 Set
<MediaRepresentationPart
> representationPartsToRemove
= new HashSet
<MediaRepresentationPart
>();
196 Set
<DescriptionElementBase
> images
= imageGallery
.getElements();
198 // overmodelling of media in cdmlib makes this a little bit complicated
199 for(DescriptionElementBase descriptionElement
: images
){
200 for(Media media
: descriptionElement
.getMedia()){
201 for(MediaRepresentation representation
: media
.getRepresentations()){
202 for(MediaRepresentationPart part
: representation
.getParts()){
203 if(part
.equals(imageFile
)){
204 // because of concurrent modification, we just collect the parts to remove
205 representationPartsToRemove
.add(part
);
209 // and then remove the representation parts here
210 for (MediaRepresentationPart part
: representationPartsToRemove
){
211 representation
.removeRepresentationPart(part
);
213 // clear set for next run
214 representationPartsToRemove
.clear();
216 // description elements with empty representations should be deleted as well
217 if(representation
.getParts().size() == 0){
218 descriptionElementsToRemove
.add(descriptionElement
);
224 // remove the empty description elements
225 for(DescriptionElementBase descriptionElement
: descriptionElementsToRemove
){
226 imageGallery
.removeElement(descriptionElement
);
230 public static final int UP
= 1;
231 public static final int DOWN
= -1;
235 * @param selectedImage
238 public static void moveImage(DescriptionElementBase element
, ImageFile image
,
241 MediaRepresentation representation
= getImageMediaRepresentation(element
);
242 int index
= representation
.getParts().indexOf(image
);
246 if (direction
== UP
&& index
!= 0) {
247 Collections
.swap(representation
.getParts(), index
, index
- 1);
249 if (direction
== DOWN
&& index
< representation
.getParts().size() - 1) {
250 Collections
.swap(representation
.getParts(), index
, index
+ 1);
256 * @param selectedImage
258 public static void removeImage(DescriptionElementBase element
, ImageFile image
) {
259 getImageMediaRepresentation(element
).getParts().remove(image
);
263 * Iterate through all taxon's image galleries until the descriptive element containing
264 * the ImageFile is found.
270 public static DescriptionElementBase
findImageElement(ImageFile image
, Taxon taxon
) {
274 for (TaxonDescription description
: taxon
.getDescriptions()) {
275 if (description
.isImageGallery()) {
276 for (DescriptionElementBase element
: description
.getElements()) {
277 if (getOrderedImages(element
).contains(image
)) {