source: extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/transverse/ImagesManagement.java @ 7070

Last change on this file since 7070 was 7070, checked in by mlg, 14 years ago

New feature : management of the privacy level ;
feature:0001888
Moved ImagesManagement from ui to transverse

File size: 4.8 KB
Line 
1package fr.mael.jiwigo.transverse;
2
3import java.awt.image.BufferedImage;
4import java.net.URL;
5import java.util.HashMap;
6import java.util.List;
7
8import javax.imageio.ImageIO;
9
10import fr.mael.jiwigo.om.Image;
11import fr.mael.jiwigo.transverse.util.Outil;
12
13/**
14   Copyright (c) 2010, Mael
15   All rights reserved.
16
17   Redistribution and use in source and binary forms, with or without
18   modification, are permitted provided that the following conditions are met:
19    * Redistributions of source code must retain the above copyright
20      notice, this list of conditions and the following disclaimer.
21    * Redistributions in binary form must reproduce the above copyright
22      notice, this list of conditions and the following disclaimer in the
23      documentation and/or other materials provided with the distribution.
24    * Neither the name of jiwigo nor the
25      names of its contributors may be used to endorse or promote products
26      derived from this software without specific prior written permission.
27
28   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
29   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31   DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY
32   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38   
39 * @author mael
40 * Management of the real sized pictures
41 */
42public class ImagesManagement {
43    /**
44     * Logger
45     */
46    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
47            .getLog(ImagesManagement.class);
48    /**
49     * the index of the current image
50     */
51    public static int CURRENT_IMAGE_INDEX;
52    /**
53     * the current image
54     */
55    public static Image CURRENT_IMAGE;
56    /**
57     * images list
58     */
59    public static List<Image> LIST_IMAGE;
60
61    /**
62     * cache allows to keep the images in the ram
63     */
64    private static HashMap<Integer, BufferedImage> IMAGES_CACHE = new HashMap<Integer, BufferedImage>();
65
66    /**
67     * cache allows to keep the thumbnails in the ram
68     */
69    private static HashMap<Integer, BufferedImage> MINIATURE_CACHE = new HashMap<Integer, BufferedImage>();
70
71    /**
72     * If true : the application won't ask the privacy level
73     * to the user each time he adds pictures
74     */
75    public static boolean rememberPrivacyLevel = false;
76
77    /**
78     * the privacy level to apply to the pictures
79     */
80    public static int privacyLevel = 1;
81
82    /**
83     * gets the current image
84     * @return the image
85     */
86    public static Image getCurrentImage() {
87        //return LIST_IMAGE.get(CURRENT_IMAGE_INDEX);
88        return CURRENT_IMAGE;
89    }
90
91    /**
92     * next image
93     */
94    public static void next() {
95        if (CURRENT_IMAGE_INDEX != LIST_IMAGE.size() - 1) {
96            CURRENT_IMAGE_INDEX++;
97        } else {
98            CURRENT_IMAGE_INDEX = 0;
99        }
100        CURRENT_IMAGE = LIST_IMAGE.get(CURRENT_IMAGE_INDEX);
101    }
102
103    /**
104     * previous image
105     */
106    public static void previous() {
107        if (CURRENT_IMAGE_INDEX != 0) {
108            CURRENT_IMAGE_INDEX--;
109        } else {
110            CURRENT_IMAGE_INDEX = LIST_IMAGE.size() - 1;
111        }
112        CURRENT_IMAGE = LIST_IMAGE.get(CURRENT_IMAGE_INDEX);
113    }
114
115    /**
116     *
117     * @param image the current image
118     */
119    public static void setCurrentImage(Image image) {
120        CURRENT_IMAGE = image;
121        int compteur = 0;
122        for (Image im : LIST_IMAGE) {
123            if (im.equals(image)) {
124                CURRENT_IMAGE_INDEX = compteur;
125            }
126            compteur++;
127        }
128    }
129
130    /**
131     * Function that allows to load images once
132     * to decrease response delays
133     * @return the image
134     */
135    public static BufferedImage getCurrentBufferedImage() {
136        if (IMAGES_CACHE.get(CURRENT_IMAGE.getIdentifiant()) == null) {
137            try {
138                BufferedImage img = ImageIO.read(new URL(CURRENT_IMAGE.getUrl()));
139                IMAGES_CACHE.put(CURRENT_IMAGE.getIdentifiant(), img);
140            } catch (Exception e) {
141                LOG.error(Outil.getStackTrace(e));
142            }
143        }
144        return IMAGES_CACHE.get(CURRENT_IMAGE.getIdentifiant());
145    }
146
147    /**
148     * Function that allows to load thimbnails once
149     * to decrease response delays
150     * @return the image
151     */
152    public static BufferedImage getMiniatureBufferedImage(Image image) {
153        if (MINIATURE_CACHE.get(image.getIdentifiant()) == null) {
154            try {
155                BufferedImage img = ImageIO.read(new URL(image.getMiniature()));
156                MINIATURE_CACHE.put(image.getIdentifiant(), img);
157            } catch (Exception e) {
158                LOG.error(Outil.getStackTrace(e));
159            }
160        }
161        return MINIATURE_CACHE.get(image.getIdentifiant());
162    }
163}
Note: See TracBrowser for help on using the repository browser.