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

Last change on this file since 9393 was 9393, checked in by mlg, 13 years ago

Modifications to use the last version of jiwigo-ws-api

File size: 6.4 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.Tools;
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    private int currentImageIndex;
52    /**
53     * the current image
54     */
55    private Image currentImage;
56    /**
57     * images list
58     */
59    private List<Image> listImage;
60
61    /**
62     * cache allows to keep the images in the ram
63     */
64    private HashMap<Integer, BufferedImage> imageCache = new HashMap<Integer, BufferedImage>();
65
66    /**
67     * cache allows to keep the thumbnails in the ram
68     */
69    private HashMap<Integer, BufferedImage> thumbnailsCache = 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    private boolean rememberPrivacyLevel = false;
76
77    /**
78     * if true : the user is currently uploading pictures
79     */
80    private boolean isUploading = false;
81    /**
82     * the privacy level to apply to the pictures
83     */
84    private int privacyLevel = 1;
85    /**
86     * boolean that defines if the application is sending files
87     *
88     */
89    private boolean sendingFiles = false;
90
91    /**
92     * singleton
93     */
94    private static ImagesManagement instance;
95
96    private ImagesManagement() {
97    }
98
99    public static ImagesManagement getInstance() {
100        if (instance == null) {
101            instance = new ImagesManagement();
102        }
103        return instance;
104    }
105
106    /**
107     * gets the current image
108     * @return the image
109     */
110    public Image getCurrentImage() {
111        //return LIST_IMAGE.get(CURRENT_IMAGE_INDEX);
112        return currentImage;
113    }
114
115    /**
116     * next image
117     */
118    public void next() {
119        if (currentImageIndex != listImage.size() - 1) {
120            currentImageIndex++;
121        } else {
122            currentImageIndex = 0;
123        }
124        currentImage = listImage.get(currentImageIndex);
125    }
126
127    /**
128     * previous image
129     */
130    public void previous() {
131        if (currentImageIndex != 0) {
132            currentImageIndex--;
133        } else {
134            currentImageIndex = listImage.size() - 1;
135        }
136        currentImage = listImage.get(currentImageIndex);
137    }
138
139    /**
140     *
141     * @param image the current image
142     */
143    public void setCurrentImage(Image image) {
144        currentImage = image;
145        int compteur = 0;
146        for (Image im : listImage) {
147            if (im.equals(image)) {
148                currentImageIndex = compteur;
149            }
150            compteur++;
151        }
152    }
153
154    /**
155     * Function that allows to load images once
156     * to decrease response delays
157     * @return the image
158     */
159    public BufferedImage getCurrentBufferedImage() {
160        if (imageCache.get(currentImage.getIdentifier()) == null) {
161            try {
162                BufferedImage img = ImageIO.read(new URL(currentImage.getUrl()));
163                imageCache.put(currentImage.getIdentifier(), img);
164            } catch (Exception e) {
165                LOG.error(Tools.getStackTrace(e));
166            }
167        }
168        return imageCache.get(currentImage.getIdentifier());
169    }
170
171    /**
172     * Function that allows to load thimbnails once
173     * to decrease response delays
174     * @return the image
175     */
176    public BufferedImage getMiniatureBufferedImage(Image image) {
177        if (thumbnailsCache.get(image.getIdentifier()) == null) {
178            try {
179                BufferedImage img = ImageIO.read(new URL(image.getThumbnailUrl()));
180                thumbnailsCache.put(image.getIdentifier(), img);
181            } catch (Exception e) {
182                LOG.error(Tools.getStackTrace(e));
183            }
184        }
185
186        return thumbnailsCache.get(image.getIdentifier());
187    }
188
189    /**
190     * @return the listImage
191     */
192    public List<Image> getListImage() {
193        return listImage;
194    }
195
196    /**
197     * @param listImage the listImage to set
198     */
199    public void setListImage(List<Image> listImage) {
200        this.listImage = listImage;
201    }
202
203    /**
204     * @return the rememberPrivacyLevel
205     */
206    public boolean isRememberPrivacyLevel() {
207        return rememberPrivacyLevel;
208    }
209
210    /**
211     * @param rememberPrivacyLevel the rememberPrivacyLevel to set
212     */
213    public void setRememberPrivacyLevel(boolean rememberPrivacyLevel) {
214        this.rememberPrivacyLevel = rememberPrivacyLevel;
215    }
216
217    /**
218     * @return the sendingFiles
219     */
220    public boolean isSendingFiles() {
221        return sendingFiles;
222    }
223
224    /**
225     * @param sendingFiles the sendingFiles to set
226     */
227    public void setSendingFiles(boolean sendingFiles) {
228        this.sendingFiles = sendingFiles;
229    }
230
231    /**
232     * @return the privacyLevel
233     */
234    public int getPrivacyLevel() {
235        return privacyLevel;
236    }
237
238    /**
239     * @param privacyLevel the privacyLevel to set
240     */
241    public void setPrivacyLevel(int privacyLevel) {
242        this.privacyLevel = privacyLevel;
243    }
244
245}
Note: See TracBrowser for help on using the repository browser.