-// $Id$\r
-/**\r
-* Copyright (C) 2007 EDIT\r
-* European Distributed Institute of Taxonomy \r
-* http://www.e-taxonomy.eu\r
-* \r
-* The contents of this file are subject to the Mozilla Public License Version 1.1\r
-* See LICENSE.TXT at the top of this package for the full license terms.\r
-*/\r
-\r
-package eu.etaxonomy.taxeditor.model;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collections;\r
-import java.util.HashSet;\r
-import java.util.List;\r
-import java.util.Set;\r
-\r
-import org.apache.log4j.Logger;\r
-\r
-import eu.etaxonomy.cdm.model.description.DescriptionBase;\r
-import eu.etaxonomy.cdm.model.description.DescriptionElementBase;\r
-import eu.etaxonomy.cdm.model.description.Feature;\r
-import eu.etaxonomy.cdm.model.description.TaxonDescription;\r
-import eu.etaxonomy.cdm.model.description.TextData;\r
-import eu.etaxonomy.cdm.model.media.ImageFile;\r
-import eu.etaxonomy.cdm.model.media.Media;\r
-import eu.etaxonomy.cdm.model.media.MediaRepresentation;\r
-import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;\r
-import eu.etaxonomy.cdm.model.media.ReferencedMedia;\r
-import eu.etaxonomy.cdm.model.taxon.Taxon;\r
-\r
-/**\r
- * @author p.ciardelli\r
- * @created 31.03.2009\r
- * @version 1.0\r
- * \r
- * This has to be moved to library at some point. Unfortunately, the whole handling of images\r
- * is very half heartedly implemented in the library. We will not mark this here as depreciated \r
- * until the situation in the library improves. \r
- * \r
- */\r
-public class ImagesHelper {\r
- private static final Logger logger = Logger\r
- .getLogger(ImagesHelper.class);\r
- \r
- /**\r
- * Quick and dirty method to get an element's first image file.\r
- * \r
- * @param element\r
- * @return\r
- */\r
- public static ImageFile getImage(DescriptionElementBase element) {\r
- List<Media> medias = element.getMedia();\r
- \r
- for(Media media : medias){\r
- Set<MediaRepresentation> representations = media.getRepresentations();\r
- \r
- for(MediaRepresentation representation : representations){\r
- List<MediaRepresentationPart> parts = representation.getParts();\r
- \r
- for (MediaRepresentationPart part : parts){\r
- if(part instanceof ImageFile){\r
- return (ImageFile) part;\r
- }\r
- }\r
- } \r
- }\r
- return null;\r
- }\r
- \r
- public static List<ImageFile> getOrderedImages(DescriptionElementBase element){\r
- List<ImageFile> imageList = new ArrayList<ImageFile>();\r
- MediaRepresentation representation = getImageMediaRepresentation(element);\r
- if (representation != null) {\r
- for (MediaRepresentationPart part : representation.getParts()){\r
- if(!(part instanceof ImageFile)){\r
- throw new RuntimeException("Your database contains media that mix Image Files with non-Image Files.");\r
- } else {\r
- imageList.add((ImageFile) part);\r
- }\r
- }\r
- }\r
- return imageList;\r
- }\r
- \r
- /**\r
- * Returns the first Representation with images. If none is found, a\r
- * Representation for storing images is created and returned.\r
- * \r
- * @param element\r
- * @return\r
- */\r
- private static MediaRepresentation getImageMediaRepresentation(DescriptionElementBase element) {\r
- // Drill down until a representation with images is found\r
- for(Media media : element.getMedia()){\r
- Set<MediaRepresentation> representations = media.getRepresentations();\r
- for(MediaRepresentation representation : representations){\r
- List<MediaRepresentationPart> parts = representation.getParts();\r
- for (MediaRepresentationPart part : parts){\r
- if(part instanceof ImageFile){\r
- return representation;\r
- }\r
- }\r
- } \r
- } \r
- // No representation with images found - create\r
- MediaRepresentation representation = MediaRepresentation.NewInstance();\r
- Media media = ReferencedMedia.NewInstance();\r
- element.addMedia(media);\r
- media.addRepresentation(representation);\r
- return representation;\r
- }\r
- \r
- /**\r
- * @param description\r
- * @return\r
- */\r
- public static Set<ImageFile> getImages(TaxonDescription description){\r
- Set<ImageFile> images = new HashSet<ImageFile>();\r
- \r
- for (DescriptionElementBase element : description.getElements()){\r
- \r
- Feature feature = element.getFeature();\r
- \r
- if(feature.equals(Feature.IMAGE())){\r
- List<Media> medias = element.getMedia();\r
- \r
- for(Media media : medias){\r
- Set<MediaRepresentation> representations = media.getRepresentations();\r
- \r
- for(MediaRepresentation representation : representations){\r
- List<MediaRepresentationPart> parts = representation.getParts();\r
- \r
- for (MediaRepresentationPart part : parts){\r
- if(part instanceof ImageFile){\r
- images.add((ImageFile) part);\r
- }\r
- }\r
- } \r
- } \r
- }\r
- }\r
- \r
- return images;\r
- }\r
-\r
- /**\r
- * @param taxon\r
- * @param imageFile\r
- */\r
- public static void addTaxonImage(Taxon taxon, DescriptionBase<?> imageGallery, ImageFile imageFile) {\r
- \r
- imageGallery.addElement(createImageElement(imageFile));\r
- \r
- }\r
- \r
- public static DescriptionElementBase createImageElement(ImageFile imageFile) {\r
- \r
- DescriptionElementBase descriptionElement = TextData.NewInstance(Feature.IMAGE());\r
- \r
- Media media = Media.NewInstance();\r
- MediaRepresentation representation = MediaRepresentation.NewInstance();\r
- \r
- representation.addRepresentationPart(imageFile);\r
- \r
- media.addRepresentation(representation);\r
- \r
- descriptionElement.addMedia(media);\r
- \r
- return descriptionElement;\r
- \r
- }\r
- \r
- /**\r
- * Adds a new, empty image file to the end of a description element's\r
- * ordered list of images.\r
- * \r
- * @param element\r
- * @return \r
- */\r
- public static ImageFile addImagePart(DescriptionElementBase element) {\r
- ImageFile imageFile = ImageFile.NewInstance(null, null);\r
- getImageMediaRepresentation(element).addRepresentationPart(imageFile);\r
- return imageFile;\r
- }\r
- \r
- /**\r
- * @param taxon\r
- * @param imageFile\r
- */\r
- public static void removeTaxonImage(Taxon taxon, DescriptionBase<?> imageGallery, ImageFile imageFile) {\r
- Set<DescriptionElementBase> descriptionElementsToRemove = new HashSet<DescriptionElementBase>();\r
- Set<MediaRepresentationPart> representationPartsToRemove = new HashSet<MediaRepresentationPart>();\r
- \r
- Set<DescriptionElementBase> images = imageGallery.getElements();\r
- \r
- // overmodelling of media in cdmlib makes this a little bit complicated\r
- for(DescriptionElementBase descriptionElement : images){\r
- for(Media media : descriptionElement.getMedia()){\r
- for(MediaRepresentation representation : media.getRepresentations()){\r
- for(MediaRepresentationPart part : representation.getParts()){\r
- if(part.equals(imageFile)){\r
- // because of concurrent modification, we just collect the parts to remove \r
- representationPartsToRemove.add(part);\r
- }\r
- }\r
-\r
- // and then remove the representation parts here\r
- for (MediaRepresentationPart part : representationPartsToRemove){\r
- representation.removeRepresentationPart(part);\r
- }\r
- // clear set for next run\r
- representationPartsToRemove.clear();\r
- \r
- // description elements with empty representations should be deleted as well\r
- if(representation.getParts().size() == 0){\r
- descriptionElementsToRemove.add(descriptionElement);\r
- }\r
- }\r
- }\r
- } \r
- \r
- // remove the empty description elements\r
- for(DescriptionElementBase descriptionElement : descriptionElementsToRemove){\r
- imageGallery.removeElement(descriptionElement);\r
- } \r
- }\r
-\r
- public static final int UP = 1;\r
- public static final int DOWN = -1;\r
- \r
- /**\r
- * @param input\r
- * @param selectedImage\r
- * @param direction\r
- */\r
- public static void moveImage(DescriptionElementBase element, ImageFile image,\r
- int direction) {\r
- \r
- MediaRepresentation representation = getImageMediaRepresentation(element);\r
- int index = representation.getParts().indexOf(image);\r
- if (index == -1) {\r
- return;\r
- } \r
- if (direction == UP && index != 0) {\r
- Collections.swap(representation.getParts(), index, index - 1);\r
- }\r
- if (direction == DOWN && index < representation.getParts().size() - 1) {\r
- Collections.swap(representation.getParts(), index, index + 1);\r
- }\r
- }\r
-\r
- /**\r
- * @param input\r
- * @param selectedImage\r
- */\r
- public static void removeImage(DescriptionElementBase element, ImageFile image) {\r
- getImageMediaRepresentation(element).getParts().remove(image);\r
- }\r
-\r
- /**\r
- * Iterate through all taxon's image galleries until the descriptive element containing\r
- * the ImageFile is found.\r
- * \r
- * @param image\r
- * @param taxon \r
- * @return \r
- */\r
- public static DescriptionElementBase findImageElement(ImageFile image, Taxon taxon) {\r
- if (taxon == null) {\r
- return null;\r
- }\r
- for (TaxonDescription description : taxon.getDescriptions()) {\r
- if (description.isImageGallery()) {\r
- for (DescriptionElementBase element : description.getElements()) {\r
- if (getOrderedImages(element).contains(image)) {\r
- return element;\r
- }\r
- }\r
- }\r
- }\r
- return null; \r
- }\r
-}
\ No newline at end of file