package fr.mael.jiwigo.transverse; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; import java.util.HashMap; import java.util.List; import javax.imageio.ImageIO; import fr.mael.jiwigo.Main; import fr.mael.jiwigo.om.Image; import fr.mael.jiwigo.service.ImageService; import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; import fr.mael.jiwigo.transverse.util.Tools; /** 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 * Management of the real sized pictures */ public class ImagesManagement { /** * Logger */ public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory .getLog(ImagesManagement.class); /** * the index of the current image */ private int currentImageIndex; /** * the current image */ private Image currentImage; /** * images list */ private List listImage; /** * cache allows to keep the images in the ram */ private HashMap imageCache = new HashMap(); /** * cache allows to keep the thumbnails in the ram */ private HashMap thumbnailsCache = new HashMap(); /** * If true : the application won't ask the privacy level * to the user each time he adds pictures */ private boolean rememberPrivacyLevel = false; /** * if true : the user is currently uploading pictures */ private boolean isUploading = false; /** * the privacy level to apply to the pictures */ private int privacyLevel = 1; /** * boolean that defines if the application is sending files * */ private boolean sendingFiles = false; /** * integer that contains the current category of the browser * it allows to empty the list of images if the category changes */ private Integer currentCategory = -1; /** * singleton */ private static ImagesManagement instance; private ImagesManagement() { } public static ImagesManagement getInstance() { if (instance == null) { instance = new ImagesManagement(); } return instance; } /** * gets the current image * @return the image */ public Image getCurrentImage() { //return LIST_IMAGE.get(CURRENT_IMAGE_INDEX); return currentImage; } /** * next image */ public void next() { if (currentImageIndex != listImage.size() - 1) { currentImageIndex++; } else { currentImageIndex = 0; } currentImage = listImage.get(currentImageIndex); } /** * previous image */ public void previous() { if (currentImageIndex != 0) { currentImageIndex--; } else { currentImageIndex = listImage.size() - 1; } currentImage = listImage.get(currentImageIndex); } /** * * @param image the current image */ public void setCurrentImage(Image image) { if (!image.getIdCategory().equals(currentCategory)) { try { listImage = ImageService.getInstance(Main.sessionManager).listByCategory(image.getIdCategory(), true); currentCategory = image.getIdCategory(); } catch (IOException e) { LOG.error(Tools.getStackTrace(e)); } catch (ProxyAuthenticationException e) { LOG.error(Tools.getStackTrace(e)); } } currentImage = image; int compteur = 0; for (Image im : listImage) { if (im.getIdentifier().equals(image.getIdentifier())) { currentImageIndex = compteur; return; } compteur++; } } /** * Function that allows to load images once * to decrease response delays * @return the image */ public BufferedImage getCurrentBufferedImage() { if (imageCache.get(currentImage.getIdentifier()) == null) { try { BufferedImage img = ImageIO.read(new URL(currentImage.getUrl())); imageCache.put(currentImage.getIdentifier(), img); } catch (Exception e) { LOG.error(Tools.getStackTrace(e)); } } return imageCache.get(currentImage.getIdentifier()); } /** * Function that allows to load thimbnails once * to decrease response delays * @return the image */ public BufferedImage getMiniatureBufferedImage(Image image) { if (thumbnailsCache.get(image.getIdentifier()) == null) { try { BufferedImage img = ImageIO.read(new URL(image.getThumbnailUrl())); thumbnailsCache.put(image.getIdentifier(), img); } catch (Exception e) { LOG.error(Tools.getStackTrace(e)); } } return thumbnailsCache.get(image.getIdentifier()); } /** * @return the listImage */ public List getListImage() { return listImage; } /** * @param listImage the listImage to set */ public void setListImage(List listImage) { this.listImage = listImage; } /** * @return the rememberPrivacyLevel */ public boolean isRememberPrivacyLevel() { return rememberPrivacyLevel; } /** * @param rememberPrivacyLevel the rememberPrivacyLevel to set */ public void setRememberPrivacyLevel(boolean rememberPrivacyLevel) { this.rememberPrivacyLevel = rememberPrivacyLevel; } /** * @return the sendingFiles */ public boolean isSendingFiles() { return sendingFiles; } /** * @param sendingFiles the sendingFiles to set */ public void setSendingFiles(boolean sendingFiles) { this.sendingFiles = sendingFiles; } /** * @return the privacyLevel */ public int getPrivacyLevel() { return privacyLevel; } /** * @param privacyLevel the privacyLevel to set */ public void setPrivacyLevel(int privacyLevel) { this.privacyLevel = privacyLevel; } }