source: extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/ui/ImagesManagement.java @ 6958

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

New features

  • When clicking on a category, the category is opened in a tab. When clicking on the same category again, no tab is opened but the previously opened tab is shown. The user can close the tabs (close current tab, close others, close all).
  • Ability to clip the images. There are still some bugs (unable to clip the images when zooming, and the rectangle stays when changing the image).
File size: 4.9 KB
Line 
1package fr.mael.jiwigo.ui;
2
3import java.awt.image.BufferedImage;
4import java.net.URL;
5import java.util.HashMap;
6import java.util.List;
7
8import javax.imageio.ImageIO;
9
10import fr.mael.jiwigo.om.Image;
11import 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 */
43public 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        return CURRENT_IMAGE;
79    }
80
81    /**
82     * passage à l'image suivante
83     */
84    public static void next() {
85        if (CURRENT_IMAGE_INDEX != LIST_IMAGE.size() - 1) {
86            CURRENT_IMAGE_INDEX++;
87        } else {
88            CURRENT_IMAGE_INDEX = 0;
89        }
90        CURRENT_IMAGE = LIST_IMAGE.get(CURRENT_IMAGE_INDEX);
91    }
92
93    /**
94     * passage à l'image précédente
95     */
96    public static void previous() {
97        if (CURRENT_IMAGE_INDEX != 0) {
98            CURRENT_IMAGE_INDEX--;
99        } else {
100            CURRENT_IMAGE_INDEX = LIST_IMAGE.size() - 1;
101        }
102        CURRENT_IMAGE = LIST_IMAGE.get(CURRENT_IMAGE_INDEX);
103    }
104
105    /**
106     *
107     * @param image l'image courante
108     */
109    public static void setCurrentImage(Image image) {
110        CURRENT_IMAGE = image;
111        int compteur = 0;
112        for (Image im : LIST_IMAGE) {
113            if (im.equals(image)) {
114                CURRENT_IMAGE_INDEX = compteur;
115            }
116            compteur++;
117        }
118    }
119
120    /**
121     * Fonction qui permet de ne charger les images qu'une seule fois
122     * afin d'améliorer les temps de réponses en ne consommant pas de bande passante.
123     * @return l'image
124     */
125    public static BufferedImage getCurrentBufferedImage() {
126        if (IMAGES_CACHE.get(CURRENT_IMAGE.getIdentifiant()) == null) {
127            try {
128                BufferedImage img = ImageIO.read(new URL(CURRENT_IMAGE.getUrl()));
129                IMAGES_CACHE.put(CURRENT_IMAGE.getIdentifiant(), img);
130            } catch (Exception e) {
131                LOG.error(Outil.getStackTrace(e));
132            }
133        }
134        return IMAGES_CACHE.get(CURRENT_IMAGE.getIdentifiant());
135    }
136
137    /**
138     * Fonction qui permet de ne charger les images qu'une seule fois
139     * afin d'améliorer les temps de réponses en ne consommant pas de bande passante.
140     * @return l'image
141     */
142    public static BufferedImage getMiniatureBufferedImage(Image image) {
143        if (MINIATURE_CACHE.get(image.getIdentifiant()) == null) {
144            try {
145                BufferedImage img = ImageIO.read(new URL(image.getMiniature()));
146                MINIATURE_CACHE.put(image.getIdentifiant(), img);
147            } catch (Exception e) {
148                LOG.error(Outil.getStackTrace(e));
149            }
150        }
151        return MINIATURE_CACHE.get(image.getIdentifiant());
152    }
153}
Note: See TracBrowser for help on using the repository browser.