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