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

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

bug correction
is now impossible to add files if the application is already sending photos.
feature:0001887

File size: 4.8 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         * boolean that defines if the application is sending files
83         *
84         */
85        public static boolean sendingFiles = false;
86
87
88        /**
89         * gets the current image
90         * @return the image
91         */
92        public static Image getCurrentImage() {
93                //return LIST_IMAGE.get(CURRENT_IMAGE_INDEX);
94                return CURRENT_IMAGE;
95        }
96
97
98        /**
99         * next image
100         */
101        public static void next() {
102                if (CURRENT_IMAGE_INDEX != LIST_IMAGE.size() - 1) {
103                        CURRENT_IMAGE_INDEX++;
104                } else {
105                        CURRENT_IMAGE_INDEX = 0;
106                }
107                CURRENT_IMAGE = LIST_IMAGE.get(CURRENT_IMAGE_INDEX);
108        }
109
110
111        /**
112         * previous image
113         */
114        public static void previous() {
115                if (CURRENT_IMAGE_INDEX != 0) {
116                        CURRENT_IMAGE_INDEX--;
117                } else {
118                        CURRENT_IMAGE_INDEX = LIST_IMAGE.size() - 1;
119                }
120                CURRENT_IMAGE = LIST_IMAGE.get(CURRENT_IMAGE_INDEX);
121        }
122
123
124        /**
125         *
126         * @param image the current image
127         */
128        public static void setCurrentImage(Image image) {
129                CURRENT_IMAGE = image;
130                int compteur = 0;
131                for (Image im : LIST_IMAGE) {
132                        if (im.equals(image)) {
133                                CURRENT_IMAGE_INDEX = compteur;
134                        }
135                        compteur++;
136                }
137        }
138
139
140        /**
141         * Function that allows to load images once
142         * to decrease response delays
143         * @return the image
144         */
145        public static BufferedImage getCurrentBufferedImage() {
146                if (IMAGES_CACHE.get(CURRENT_IMAGE.getIdentifiant()) == null) {
147                        try {
148                                BufferedImage img = ImageIO.read(new URL(CURRENT_IMAGE.getUrl()));
149                                IMAGES_CACHE.put(CURRENT_IMAGE.getIdentifiant(), img);
150                        } catch (Exception e) {
151                                LOG.error(Outil.getStackTrace(e));
152                        }
153                }
154                return IMAGES_CACHE.get(CURRENT_IMAGE.getIdentifiant());
155        }
156
157
158        /**
159         * Function that allows to load thimbnails once
160         * to decrease response delays
161         * @return the image
162         */
163        public static BufferedImage getMiniatureBufferedImage(Image image) {
164                if (MINIATURE_CACHE.get(image.getIdentifiant()) == null) {
165                        try {
166                                BufferedImage img = ImageIO.read(new URL(image.getMiniature()));
167                                MINIATURE_CACHE.put(image.getIdentifiant(), img);
168                        } catch (Exception e) {
169                                LOG.error(Outil.getStackTrace(e));
170                        }
171                }
172
173                return MINIATURE_CACHE.get(image.getIdentifiant());
174        }
175}
Note: See TracBrowser for help on using the repository browser.