1 | package fr.mael.jiwigo.ui; |
---|
2 | |
---|
3 | import java.awt.image.BufferedImage; |
---|
4 | import java.net.URL; |
---|
5 | import java.util.HashMap; |
---|
6 | import java.util.List; |
---|
7 | |
---|
8 | import javax.imageio.ImageIO; |
---|
9 | |
---|
10 | import fr.mael.jiwigo.om.Image; |
---|
11 | import 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 | * Classe de gestion des images à taille relle. |
---|
41 | * mettre tout en static permet d'y accéder depuis les différentes classes qui en ont besoin |
---|
42 | */ |
---|
43 | public class ImagesManagement { |
---|
44 | /** |
---|
45 | * Logger |
---|
46 | */ |
---|
47 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory |
---|
48 | .getLog(ImagesManagement.class); |
---|
49 | /** |
---|
50 | * L'index de l'image courante dans la liste |
---|
51 | */ |
---|
52 | public static int CURRENT_IMAGE_INDEX; |
---|
53 | /** |
---|
54 | * l'image courante |
---|
55 | */ |
---|
56 | public static Image CURRENT_IMAGE; |
---|
57 | /** |
---|
58 | * la liste des images |
---|
59 | */ |
---|
60 | public static List<Image> LIST_IMAGE; |
---|
61 | |
---|
62 | /** |
---|
63 | * cache permettant de ne pas recharger les images à chaque fois |
---|
64 | */ |
---|
65 | private static HashMap<Integer, BufferedImage> IMAGES_CACHE = new HashMap<Integer, BufferedImage>(); |
---|
66 | |
---|
67 | /** |
---|
68 | * cache permettant de ne pas recharger les images à chaque fois |
---|
69 | */ |
---|
70 | private static HashMap<Integer, BufferedImage> MINIATURE_CACHE = new HashMap<Integer, BufferedImage>(); |
---|
71 | |
---|
72 | /** |
---|
73 | * Récupération de l'image courante |
---|
74 | * @return l'image |
---|
75 | */ |
---|
76 | public static Image getCurrentImage() { |
---|
77 | return LIST_IMAGE.get(CURRENT_IMAGE_INDEX); |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * passage à l'image suivante |
---|
82 | */ |
---|
83 | public static void next() { |
---|
84 | if (CURRENT_IMAGE_INDEX != LIST_IMAGE.size() - 1) { |
---|
85 | CURRENT_IMAGE_INDEX++; |
---|
86 | } else { |
---|
87 | CURRENT_IMAGE_INDEX = 0; |
---|
88 | } |
---|
89 | CURRENT_IMAGE = LIST_IMAGE.get(CURRENT_IMAGE_INDEX); |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * passage à l'image précédente |
---|
94 | */ |
---|
95 | public static void previous() { |
---|
96 | if (CURRENT_IMAGE_INDEX != 0) { |
---|
97 | CURRENT_IMAGE_INDEX--; |
---|
98 | } else { |
---|
99 | CURRENT_IMAGE_INDEX = LIST_IMAGE.size() - 1; |
---|
100 | } |
---|
101 | CURRENT_IMAGE = LIST_IMAGE.get(CURRENT_IMAGE_INDEX); |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * |
---|
106 | * @param image l'image courante |
---|
107 | */ |
---|
108 | public static void setCurrentImage(Image image) { |
---|
109 | CURRENT_IMAGE = image; |
---|
110 | int compteur = 0; |
---|
111 | for (Image im : LIST_IMAGE) { |
---|
112 | if (im.equals(image)) { |
---|
113 | CURRENT_IMAGE_INDEX = compteur; |
---|
114 | } |
---|
115 | compteur++; |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | * Fonction qui permet de ne charger les images qu'une seule fois |
---|
121 | * afin d'améliorer les temps de réponses en ne consommant pas de bande passante. |
---|
122 | * @return l'image |
---|
123 | */ |
---|
124 | public static BufferedImage getCurrentBufferedImage() { |
---|
125 | if (IMAGES_CACHE.get(CURRENT_IMAGE.getIdentifiant()) == null) { |
---|
126 | try { |
---|
127 | BufferedImage img = ImageIO.read(new URL(CURRENT_IMAGE.getUrl())); |
---|
128 | IMAGES_CACHE.put(CURRENT_IMAGE.getIdentifiant(), img); |
---|
129 | } catch (Exception e) { |
---|
130 | LOG.error(Outil.getStackTrace(e)); |
---|
131 | } |
---|
132 | } |
---|
133 | return IMAGES_CACHE.get(CURRENT_IMAGE.getIdentifiant()); |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * Fonction qui permet de ne charger les images qu'une seule fois |
---|
138 | * afin d'améliorer les temps de réponses en ne consommant pas de bande passante. |
---|
139 | * @return l'image |
---|
140 | */ |
---|
141 | public static BufferedImage getMiniatureBufferedImage(Image image) { |
---|
142 | if (MINIATURE_CACHE.get(image.getIdentifiant()) == null) { |
---|
143 | try { |
---|
144 | BufferedImage img = ImageIO.read(new URL(image.getMiniature())); |
---|
145 | MINIATURE_CACHE.put(image.getIdentifiant(), img); |
---|
146 | } catch (Exception e) { |
---|
147 | LOG.error(Outil.getStackTrace(e)); |
---|
148 | } |
---|
149 | } |
---|
150 | return MINIATURE_CACHE.get(image.getIdentifiant()); |
---|
151 | } |
---|
152 | } |
---|