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

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

Adds spring for the dependency injection
Deletion of jdom (not useful just for a class that manipulates a simple XML file)

File size: 7.1 KB
Line 
1package fr.mael.jiwigo.transverse;
2
3import java.awt.image.BufferedImage;
4import java.io.IOException;
5import java.net.URL;
6import java.util.HashMap;
7import java.util.List;
8
9import javax.imageio.ImageIO;
10
11import fr.mael.jiwigo.om.Image;
12import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
13import fr.mael.jiwigo.transverse.util.Tools;
14import fr.mael.jiwigo.transverse.util.spring.SpringUtils;
15
16/**
17   Copyright (c) 2010, Mael
18   All rights reserved.
19
20   Redistribution and use in source and binary forms, with or without
21   modification, are permitted provided that the following conditions are met:
22    * Redistributions of source code must retain the above copyright
23      notice, this list of conditions and the following disclaimer.
24    * Redistributions in binary form must reproduce the above copyright
25      notice, this list of conditions and the following disclaimer in the
26      documentation and/or other materials provided with the distribution.
27    * Neither the name of jiwigo nor the
28      names of its contributors may be used to endorse or promote products
29      derived from this software without specific prior written permission.
30
31   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
32   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34   DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY
35   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
38   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41
42 * @author mael
43 * Management of the real sized pictures
44 */
45public class ImagesManagement {
46    /**
47     * Logger
48     */
49    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
50            .getLog(ImagesManagement.class);
51    /**
52     * the index of the current image
53     */
54    private int currentImageIndex;
55    /**
56     * the current image
57     */
58    private Image currentImage;
59    /**
60     * images list
61     */
62    private List<Image> listImage;
63
64    /**
65     * cache allows to keep the images in the ram
66     */
67    private HashMap<Integer, BufferedImage> imageCache = new HashMap<Integer, BufferedImage>();
68
69    /**
70     * cache allows to keep the thumbnails in the ram
71     */
72    private HashMap<Integer, BufferedImage> thumbnailsCache = new HashMap<Integer, BufferedImage>();
73
74    /**
75     * If true : the application won't ask the privacy level
76     * to the user each time he adds pictures
77     */
78    private boolean rememberPrivacyLevel = false;
79
80    /**
81     * if true : the user is currently uploading pictures
82     */
83    private boolean isUploading = false;
84    /**
85     * the privacy level to apply to the pictures
86     */
87    private int privacyLevel = 1;
88    /**
89     * boolean that defines if the application is sending files
90     *
91     */
92    private boolean sendingFiles = false;
93
94    /**
95     * integer that contains the current category of the browser
96     * it allows to empty the list of images if the category changes
97     */
98    private Integer currentCategory = -1;
99
100    /**
101     * singleton
102     */
103    private static ImagesManagement instance;
104
105    private ImagesManagement() {
106    }
107
108    public static ImagesManagement getInstance() {
109        if (instance == null) {
110            instance = new ImagesManagement();
111        }
112        return instance;
113    }
114
115    /**
116     * gets the current image
117     * @return the image
118     */
119    public Image getCurrentImage() {
120        //return LIST_IMAGE.get(CURRENT_IMAGE_INDEX);
121        return currentImage;
122    }
123
124    /**
125     * next image
126     */
127    public void next() {
128        if (currentImageIndex != listImage.size() - 1) {
129            currentImageIndex++;
130        } else {
131            currentImageIndex = 0;
132        }
133        currentImage = listImage.get(currentImageIndex);
134    }
135
136    /**
137     * previous image
138     */
139    public void previous() {
140        if (currentImageIndex != 0) {
141            currentImageIndex--;
142        } else {
143            currentImageIndex = listImage.size() - 1;
144        }
145        currentImage = listImage.get(currentImageIndex);
146    }
147
148    /**
149     *
150     * @param image the current image
151     */
152    public void setCurrentImage(Image image) {
153        if (!image.getIdCategory().equals(currentCategory)) {
154            try {
155                listImage = SpringUtils.getImageService().listByCategory(image.getIdCategory(), true);
156                currentCategory = image.getIdCategory();
157            } catch (IOException e) {
158                LOG.error(Tools.getStackTrace(e));
159            } catch (ProxyAuthenticationException e) {
160                LOG.error(Tools.getStackTrace(e));
161            }
162        }
163        currentImage = image;
164        int compteur = 0;
165        for (Image im : listImage) {
166            if (im.getIdentifier().equals(image.getIdentifier())) {
167                currentImageIndex = compteur;
168                return;
169            }
170            compteur++;
171        }
172    }
173
174    /**
175     * Function that allows to load images once
176     * to decrease response delays
177     * @return the image
178     */
179    public BufferedImage getCurrentBufferedImage() {
180        if (imageCache.get(currentImage.getIdentifier()) == null) {
181            try {
182                BufferedImage img = ImageIO.read(new URL(currentImage.getUrl()));
183                imageCache.put(currentImage.getIdentifier(), img);
184            } catch (Exception e) {
185                LOG.error(Tools.getStackTrace(e));
186            }
187        }
188        return imageCache.get(currentImage.getIdentifier());
189    }
190
191    /**
192     * Function that allows to load thimbnails once
193     * to decrease response delays
194     * @return the image
195     */
196    public BufferedImage getMiniatureBufferedImage(Image image) {
197        if (thumbnailsCache.get(image.getIdentifier()) == null) {
198            try {
199                BufferedImage img = ImageIO.read(new URL(image.getThumbnailUrl()));
200                thumbnailsCache.put(image.getIdentifier(), img);
201            } catch (Exception e) {
202                LOG.error(Tools.getStackTrace(e));
203            }
204        }
205
206        return thumbnailsCache.get(image.getIdentifier());
207    }
208
209    /**
210     * @return the listImage
211     */
212    public List<Image> getListImage() {
213        return listImage;
214    }
215
216    /**
217     * @param listImage the listImage to set
218     */
219    public void setListImage(List<Image> listImage) {
220        this.listImage = listImage;
221    }
222
223    /**
224     * @return the rememberPrivacyLevel
225     */
226    public boolean isRememberPrivacyLevel() {
227        return rememberPrivacyLevel;
228    }
229
230    /**
231     * @param rememberPrivacyLevel the rememberPrivacyLevel to set
232     */
233    public void setRememberPrivacyLevel(boolean rememberPrivacyLevel) {
234        this.rememberPrivacyLevel = rememberPrivacyLevel;
235    }
236
237    /**
238     * @return the sendingFiles
239     */
240    public boolean isSendingFiles() {
241        return sendingFiles;
242    }
243
244    /**
245     * @param sendingFiles the sendingFiles to set
246     */
247    public void setSendingFiles(boolean sendingFiles) {
248        this.sendingFiles = sendingFiles;
249    }
250
251    /**
252     * @return the privacyLevel
253     */
254    public int getPrivacyLevel() {
255        return privacyLevel;
256    }
257
258    /**
259     * @param privacyLevel the privacyLevel to set
260     */
261    public void setPrivacyLevel(int privacyLevel) {
262        this.privacyLevel = privacyLevel;
263    }
264
265}
Note: See TracBrowser for help on using the repository browser.