package fr.mael.jiwigo.ui; import java.awt.image.BufferedImage; import java.net.URL; import java.util.HashMap; import java.util.List; import javax.imageio.ImageIO; import fr.mael.jiwigo.om.Image; import fr.mael.jiwigo.transverse.util.Outil; /** Copyright (c) 2010, Mael All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of jiwigo nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * @author mael * Classe de gestion des images à taille relle. * mettre tout en static permet d'y accéder depuis les différentes classes qui en ont besoin */ public class ImagesManagement { /** * Logger */ public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory .getLog(ImagesManagement.class); /** * L'index de l'image courante dans la liste */ public static int CURRENT_IMAGE_INDEX; /** * l'image courante */ public static Image CURRENT_IMAGE; /** * la liste des images */ public static List LIST_IMAGE; /** * cache permettant de ne pas recharger les images à chaque fois */ private static HashMap IMAGES_CACHE = new HashMap(); /** * cache permettant de ne pas recharger les images à chaque fois */ private static HashMap MINIATURE_CACHE = new HashMap(); /** * Récupération de l'image courante * @return l'image */ public static Image getCurrentImage() { return LIST_IMAGE.get(CURRENT_IMAGE_INDEX); } /** * passage à l'image suivante */ public static void next() { if (CURRENT_IMAGE_INDEX != LIST_IMAGE.size() - 1) { CURRENT_IMAGE_INDEX++; } else { CURRENT_IMAGE_INDEX = 0; } CURRENT_IMAGE = LIST_IMAGE.get(CURRENT_IMAGE_INDEX); } /** * passage à l'image précédente */ public static void previous() { if (CURRENT_IMAGE_INDEX != 0) { CURRENT_IMAGE_INDEX--; } else { CURRENT_IMAGE_INDEX = LIST_IMAGE.size() - 1; } CURRENT_IMAGE = LIST_IMAGE.get(CURRENT_IMAGE_INDEX); } /** * * @param image l'image courante */ public static void setCurrentImage(Image image) { CURRENT_IMAGE = image; int compteur = 0; for (Image im : LIST_IMAGE) { if (im.equals(image)) { CURRENT_IMAGE_INDEX = compteur; } compteur++; } } /** * Fonction qui permet de ne charger les images qu'une seule fois * afin d'améliorer les temps de réponses en ne consommant pas de bande passante. * @return l'image */ public static BufferedImage getCurrentBufferedImage() { if (IMAGES_CACHE.get(CURRENT_IMAGE.getIdentifiant()) == null) { try { BufferedImage img = ImageIO.read(new URL(CURRENT_IMAGE.getUrl())); IMAGES_CACHE.put(CURRENT_IMAGE.getIdentifiant(), img); } catch (Exception e) { LOG.error(Outil.getStackTrace(e)); } } return IMAGES_CACHE.get(CURRENT_IMAGE.getIdentifiant()); } /** * Fonction qui permet de ne charger les images qu'une seule fois * afin d'améliorer les temps de réponses en ne consommant pas de bande passante. * @return l'image */ public static BufferedImage getMiniatureBufferedImage(Image image) { if (MINIATURE_CACHE.get(image.getIdentifiant()) == null) { try { BufferedImage img = ImageIO.read(new URL(image.getMiniature())); MINIATURE_CACHE.put(image.getIdentifiant(), img); } catch (Exception e) { LOG.error(Outil.getStackTrace(e)); } } return MINIATURE_CACHE.get(image.getIdentifiant()); } }