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

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

Bug fixing :
bug:0001838 I fixed it by calling the refresh of the thumbnail panel only once : at the end of the file sending.
The two thread don't try to access to the session manager at the same time...

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