source: extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/ui/mainframe/ThumbnailCategoryPanel.java @ 6965

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

removal of useless comments and System.out.println

File size: 6.4 KB
Line 
1package fr.mael.jiwigo.ui.mainframe;
2
3import java.awt.FlowLayout;
4import java.io.File;
5
6import javax.swing.JOptionPane;
7import javax.swing.JPanel;
8
9import fr.mael.jiwigo.om.Category;
10import fr.mael.jiwigo.om.Image;
11import fr.mael.jiwigo.service.ImageService;
12import fr.mael.jiwigo.transverse.util.Messages;
13import fr.mael.jiwigo.transverse.util.Outil;
14import fr.mael.jiwigo.transverse.util.filedrop.FileDrop;
15import fr.mael.jiwigo.ui.ImagesManagement;
16import fr.mael.jiwigo.ui.layout.VerticalLayout;
17
18/**
19   Copyright (c) 2010, Mael
20   All rights reserved.
21
22   Redistribution and use in source and binary forms, with or without
23   modification, are permitted provided that the following conditions are met:
24    * Redistributions of source code must retain the above copyright
25      notice, this list of conditions and the following disclaimer.
26    * Redistributions in binary form must reproduce the above copyright
27      notice, this list of conditions and the following disclaimer in the
28      documentation and/or other materials provided with the distribution.
29    * Neither the name of jiwigo nor the
30      names of its contributors may be used to endorse or promote products
31      derived from this software without specific prior written permission.
32
33   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
34   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36   DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY
37   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   
44 * @author mael
45 * Classe qui affiche toutes les miniatures d'une catégorie
46 */
47public class ThumbnailCategoryPanel extends JPanel {
48    /**
49     * Logger
50     */
51    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
52            .getLog(ThumbnailCategoryPanel.class);
53    /**
54     * L'id de la catégorie pour laquelle les miniatures doivent être affichées
55     */
56    private Integer categoryId;
57
58    private Category category;
59
60    public ThumbnailCategoryPanel(Category category) {
61        this(category.getIdentifiant());
62        this.category = category;
63    }
64
65    /**
66     * Constructeur
67     * @param categoryId l'id de la catégorie
68     */
69    public ThumbnailCategoryPanel(Integer categoryId) {
70        this.categoryId = categoryId;
71        this.setLayout(new VerticalLayout());
72        if (categoryId != null) {
73            rafraichir(categoryId, false);
74        }
75        //gestion du drag'n drop
76        new FileDrop(System.out, this, new FileDrop.Listener() {
77            public void filesDropped(final java.io.File[] files) {
78                new Thread(new ThreadEnvoiPhoto(files)).start();
79
80            }
81        });
82
83    }
84
85    /**
86     * rafraichissement, afin d'afficher les nouvelles images
87     * @param categoryId
88     */
89    public void rafraichir(Integer categoryId, boolean rafraichir) {
90        this.categoryId = categoryId;
91        new Thread(new ThreadLoadThumb(this, rafraichir)).start();
92    }
93
94    /**
95     * @return the categoryId
96     */
97    public Integer getCategoryId() {
98        return categoryId;
99    }
100
101    /**
102     * @param categoryId the categoryId to set
103     */
104    public void setCategoryId(Integer categoryId) {
105        this.categoryId = categoryId;
106    }
107
108    /**
109     * @return the category
110     */
111    public Category getCategory() {
112        return category;
113    }
114
115    /**
116     * @param category the category to set
117     */
118    public void setCategory(Category category) {
119        this.category = category;
120    }
121
122    /**
123     * @author mael
124     * Thread that send the photos
125     */
126    public class ThreadEnvoiPhoto implements Runnable {
127        private File[] files;
128
129        public ThreadEnvoiPhoto(File[] files) {
130            this.files = files;
131        }
132
133        @Override
134        public void run() {
135
136            for (int i = 0; i < files.length; i++) {
137                int nbProgressBar = ((i + 1) * 100) / files.length;
138                try {
139
140                    ImageService.getInstance().creer(files[i].getCanonicalPath(), categoryId);
141                    MainFrame.getInstance().setReussiteMessage(
142                            files[i].getName() + " " + Messages.getMessage("sendingSuccess"));
143                } catch (Exception e) {
144                    LOG.error(Outil.getStackTrace(e));
145                    //s'il y a une erreur lors de l'envoi
146                    JOptionPane.showMessageDialog(null, Messages.getMessage("sendingError") + files[i].getName(),
147                            Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
148                    MainFrame.getInstance().setErrorMessage(
149                            Messages.getMessage("sendingError") + " " + files[i].getName());
150                } finally {
151                    MainFrame.getInstance().getProgressBar().setValue(nbProgressBar);
152                }
153            }
154            //on rafraichit
155            rafraichir(categoryId, true);
156            MainFrame.getInstance().getProgressBar().setValue(0);
157        }
158    }
159
160    /**
161     *
162     * @author mael
163     * Thread that loads the thumbs and fills the thumb panel
164     */
165    public class ThreadLoadThumb implements Runnable {
166        ThumbnailCategoryPanel panel;
167        boolean rafraichir;
168
169        public ThreadLoadThumb(ThumbnailCategoryPanel panel, boolean rafraichir) {
170            this.panel = panel;
171            this.rafraichir = rafraichir;
172        }
173
174        @Override
175        public void run() {
176
177            panel.removeAll();
178            int nb = 1;
179            JPanel panelh = new JPanel(new FlowLayout());
180            try {
181                MainFrame.getInstance().setReussiteMessage(Messages.getMessage("thumbviewer_loading"));
182                ImagesManagement.LIST_IMAGE = ImageService.getInstance().listerParCategory(categoryId, rafraichir);
183                for (Image image : ImagesManagement.LIST_IMAGE) {
184                    try {
185
186                        if (nb == 7) {
187                            panel.add(panelh);
188                            panelh = new JPanel(new FlowLayout());
189                            nb = 0;
190                        } else {
191                            ThumbnailPanel panel = new ThumbnailPanel(image);
192                            panelh.add(panel);
193                        }
194                        nb++;
195
196                    } catch (Exception e) {
197
198                    }
199                }
200                if (nb != 8) {
201                    panel.add(panelh);
202                }
203                panel.repaint();
204                panel.revalidate();
205                MainFrame.getInstance().setReussiteMessage(Messages.getMessage("loadingOk"));
206            } catch (Exception e) {
207                LOG.error(Outil.getStackTrace(e));
208                JOptionPane.showMessageDialog(null, Messages.getMessage("imagesListingError"), Messages
209                        .getMessage("error"), JOptionPane.ERROR_MESSAGE);
210                MainFrame.getInstance().setErrorMessage(Messages.getMessage("imagesListingError"));
211            }
212
213        }
214    }
215
216}
Note: See TracBrowser for help on using the repository browser.