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

Last change on this file since 7221 was 7221, checked in by mlg, 14 years ago

implements feature : the number of thumbnails in the thumbnails viewer is adapted to the size of the frame
not perfect but better
feature:0001838

File size: 4.7 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;
7import javax.imageio.ImageIO;
8import fr.mael.jiwigo.om.Image;
9import fr.mael.jiwigo.transverse.util.Outil;
10
11
12/**
13   Copyright (c) 2010, Mael
14   All rights reserved.
15
16   Redistribution and use in source and binary forms, with or without
17   modification, are permitted provided that the following conditions are met:
18    * Redistributions of source code must retain the above copyright
19      notice, this list of conditions and the following disclaimer.
20    * Redistributions in binary form must reproduce the above copyright
21      notice, this list of conditions and the following disclaimer in the
22      documentation and/or other materials provided with the distribution.
23    * Neither the name of jiwigo nor the
24      names of its contributors may be used to endorse or promote products
25      derived from this software without specific prior written permission.
26
27   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
28   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30   DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY
31   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   
38 * @author mael
39 * Management of the real sized pictures
40 */
41public class ImagesManagement {
42
43        /**
44         * Logger
45         */
46        public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog(ImagesManagement.class);
47        /**
48         * the index of the current image
49         */
50        public static int CURRENT_IMAGE_INDEX;
51        /**
52         * the current image
53         */
54        public static Image CURRENT_IMAGE;
55        /**
56         * images list
57         */
58        public static List<Image> LIST_IMAGE;
59
60        /**
61         * cache allows to keep the images in the ram
62         */
63        private static HashMap<Integer, BufferedImage> IMAGES_CACHE = new HashMap<Integer, BufferedImage>();
64
65        /**
66         * cache allows to keep the thumbnails in the ram
67         */
68        private static HashMap<Integer, BufferedImage> MINIATURE_CACHE = new HashMap<Integer, BufferedImage>();
69
70        /**
71         * If true : the application won't ask the privacy level
72         * to the user each time he adds pictures
73         */
74        public static boolean rememberPrivacyLevel = false;
75
76        /**
77         * the privacy level to apply to the pictures
78         */
79        public static int privacyLevel = 1;
80
81
82        /**
83         * gets the current image
84         * @return the image
85         */
86        public static Image getCurrentImage() {
87                //return LIST_IMAGE.get(CURRENT_IMAGE_INDEX);
88                return CURRENT_IMAGE;
89        }
90
91
92        /**
93         * next image
94         */
95        public static void next() {
96                if (CURRENT_IMAGE_INDEX != LIST_IMAGE.size() - 1) {
97                        CURRENT_IMAGE_INDEX++;
98                } else {
99                        CURRENT_IMAGE_INDEX = 0;
100                }
101                CURRENT_IMAGE = LIST_IMAGE.get(CURRENT_IMAGE_INDEX);
102        }
103
104
105        /**
106         * previous image
107         */
108        public static void previous() {
109                if (CURRENT_IMAGE_INDEX != 0) {
110                        CURRENT_IMAGE_INDEX--;
111                } else {
112                        CURRENT_IMAGE_INDEX = LIST_IMAGE.size() - 1;
113                }
114                CURRENT_IMAGE = LIST_IMAGE.get(CURRENT_IMAGE_INDEX);
115        }
116
117
118        /**
119         *
120         * @param image the current image
121         */
122        public static void setCurrentImage(Image image) {
123                CURRENT_IMAGE = image;
124                int compteur = 0;
125                for (Image im : LIST_IMAGE) {
126                        if (im.equals(image)) {
127                                CURRENT_IMAGE_INDEX = compteur;
128                        }
129                        compteur++;
130                }
131        }
132
133
134        /**
135         * Function that allows to load images once
136         * to decrease response delays
137         * @return the image
138         */
139        public static BufferedImage getCurrentBufferedImage() {
140                if (IMAGES_CACHE.get(CURRENT_IMAGE.getIdentifiant()) == null) {
141                        try {
142                                BufferedImage img = ImageIO.read(new URL(CURRENT_IMAGE.getUrl()));
143                                IMAGES_CACHE.put(CURRENT_IMAGE.getIdentifiant(), img);
144                        } catch (Exception e) {
145                                LOG.error(Outil.getStackTrace(e));
146                        }
147                }
148                return IMAGES_CACHE.get(CURRENT_IMAGE.getIdentifiant());
149        }
150
151
152        /**
153         * Function that allows to load thimbnails once
154         * to decrease response delays
155         * @return the image
156         */
157        public static BufferedImage getMiniatureBufferedImage(Image image) {
158                if (MINIATURE_CACHE.get(image.getIdentifiant()) == null) {
159                        try {
160                                BufferedImage img = ImageIO.read(new URL(image.getMiniature()));
161                                MINIATURE_CACHE.put(image.getIdentifiant(), img);
162                        } catch (Exception e) {
163                                LOG.error(Outil.getStackTrace(e));
164                        }
165                }
166
167                return MINIATURE_CACHE.get(image.getIdentifiant());
168        }
169}
Note: See TracBrowser for help on using the repository browser.