[7070] | 1 | package fr.mael.jiwigo.transverse; |
---|
| 2 | |
---|
| 3 | import java.awt.image.BufferedImage; |
---|
[9878] | 4 | import java.io.IOException; |
---|
[7070] | 5 | import java.net.URL; |
---|
| 6 | import java.util.HashMap; |
---|
| 7 | import java.util.List; |
---|
[8829] | 8 | |
---|
[7070] | 9 | import javax.imageio.ImageIO; |
---|
[8829] | 10 | |
---|
[9878] | 11 | import fr.mael.jiwigo.Main; |
---|
[7070] | 12 | import fr.mael.jiwigo.om.Image; |
---|
[9878] | 13 | import fr.mael.jiwigo.service.ImageService; |
---|
| 14 | import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; |
---|
[9393] | 15 | import fr.mael.jiwigo.transverse.util.Tools; |
---|
[7070] | 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. |
---|
[9393] | 42 | |
---|
[7070] | 43 | * @author mael |
---|
| 44 | * Management of the real sized pictures |
---|
| 45 | */ |
---|
| 46 | public class ImagesManagement { |
---|
[8829] | 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; |
---|
[7070] | 64 | |
---|
[8829] | 65 | /** |
---|
| 66 | * cache allows to keep the images in the ram |
---|
| 67 | */ |
---|
| 68 | private HashMap<Integer, BufferedImage> imageCache = new HashMap<Integer, BufferedImage>(); |
---|
[7070] | 69 | |
---|
[8829] | 70 | /** |
---|
| 71 | * cache allows to keep the thumbnails in the ram |
---|
| 72 | */ |
---|
| 73 | private HashMap<Integer, BufferedImage> thumbnailsCache = new HashMap<Integer, BufferedImage>(); |
---|
[7070] | 74 | |
---|
[8829] | 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; |
---|
[7070] | 80 | |
---|
[8829] | 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 |
---|
[9393] | 91 | * |
---|
[8829] | 92 | */ |
---|
| 93 | private boolean sendingFiles = false; |
---|
[7070] | 94 | |
---|
[8829] | 95 | /** |
---|
[9878] | 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 | /** |
---|
[8829] | 102 | * singleton |
---|
| 103 | */ |
---|
| 104 | private static ImagesManagement instance; |
---|
[7070] | 105 | |
---|
[8829] | 106 | private ImagesManagement() { |
---|
| 107 | } |
---|
[7221] | 108 | |
---|
[8829] | 109 | public static ImagesManagement getInstance() { |
---|
| 110 | if (instance == null) { |
---|
| 111 | instance = new ImagesManagement(); |
---|
[7070] | 112 | } |
---|
[8829] | 113 | return instance; |
---|
| 114 | } |
---|
[7070] | 115 | |
---|
[8829] | 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 | } |
---|
[7221] | 124 | |
---|
[8829] | 125 | /** |
---|
| 126 | * next image |
---|
| 127 | */ |
---|
| 128 | public void next() { |
---|
| 129 | if (currentImageIndex != listImage.size() - 1) { |
---|
| 130 | currentImageIndex++; |
---|
| 131 | } else { |
---|
| 132 | currentImageIndex = 0; |
---|
[7070] | 133 | } |
---|
[8829] | 134 | currentImage = listImage.get(currentImageIndex); |
---|
| 135 | } |
---|
[7070] | 136 | |
---|
[8829] | 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 | } |
---|
[7221] | 148 | |
---|
[8829] | 149 | /** |
---|
[9393] | 150 | * |
---|
[8829] | 151 | * @param image the current image |
---|
| 152 | */ |
---|
| 153 | public void setCurrentImage(Image image) { |
---|
[9878] | 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 | } |
---|
[8829] | 164 | currentImage = image; |
---|
| 165 | int compteur = 0; |
---|
| 166 | for (Image im : listImage) { |
---|
[9878] | 167 | if (im.getIdentifier().equals(image.getIdentifier())) { |
---|
[8829] | 168 | currentImageIndex = compteur; |
---|
[9878] | 169 | return; |
---|
[8829] | 170 | } |
---|
| 171 | compteur++; |
---|
[7070] | 172 | } |
---|
[8829] | 173 | } |
---|
[7070] | 174 | |
---|
[8829] | 175 | /** |
---|
| 176 | * Function that allows to load images once |
---|
| 177 | * to decrease response delays |
---|
| 178 | * @return the image |
---|
| 179 | */ |
---|
| 180 | public BufferedImage getCurrentBufferedImage() { |
---|
[9393] | 181 | if (imageCache.get(currentImage.getIdentifier()) == null) { |
---|
[8829] | 182 | try { |
---|
| 183 | BufferedImage img = ImageIO.read(new URL(currentImage.getUrl())); |
---|
[9393] | 184 | imageCache.put(currentImage.getIdentifier(), img); |
---|
[8829] | 185 | } catch (Exception e) { |
---|
[9393] | 186 | LOG.error(Tools.getStackTrace(e)); |
---|
[8829] | 187 | } |
---|
| 188 | } |
---|
[9393] | 189 | return imageCache.get(currentImage.getIdentifier()); |
---|
[8829] | 190 | } |
---|
[7221] | 191 | |
---|
[8829] | 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) { |
---|
[9393] | 198 | if (thumbnailsCache.get(image.getIdentifier()) == null) { |
---|
[8829] | 199 | try { |
---|
[9393] | 200 | BufferedImage img = ImageIO.read(new URL(image.getThumbnailUrl())); |
---|
| 201 | thumbnailsCache.put(image.getIdentifier(), img); |
---|
[8829] | 202 | } catch (Exception e) { |
---|
[9393] | 203 | LOG.error(Tools.getStackTrace(e)); |
---|
[8829] | 204 | } |
---|
[7070] | 205 | } |
---|
| 206 | |
---|
[9393] | 207 | return thumbnailsCache.get(image.getIdentifier()); |
---|
[8829] | 208 | } |
---|
[7221] | 209 | |
---|
[8829] | 210 | /** |
---|
| 211 | * @return the listImage |
---|
| 212 | */ |
---|
| 213 | public List<Image> getListImage() { |
---|
| 214 | return listImage; |
---|
| 215 | } |
---|
[7221] | 216 | |
---|
[8829] | 217 | /** |
---|
| 218 | * @param listImage the listImage to set |
---|
| 219 | */ |
---|
| 220 | public void setListImage(List<Image> listImage) { |
---|
| 221 | this.listImage = listImage; |
---|
| 222 | } |
---|
[7221] | 223 | |
---|
[8829] | 224 | /** |
---|
| 225 | * @return the rememberPrivacyLevel |
---|
| 226 | */ |
---|
| 227 | public boolean isRememberPrivacyLevel() { |
---|
| 228 | return rememberPrivacyLevel; |
---|
| 229 | } |
---|
[7221] | 230 | |
---|
[8829] | 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 | |
---|
[7070] | 266 | } |
---|