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

Last change on this file since 9892 was 9892, checked in by mlg, 13 years ago

Fixes a display bug
In the ThumbnailCategoryPanel the last Image of a line was not displayed.

File size: 8.7 KB
Line 
1package fr.mael.jiwigo.ui.mainframe;
2
3import java.awt.Dimension;
4import java.awt.FlowLayout;
5import java.awt.Graphics;
6import java.io.File;
7import java.io.IOException;
8
9import javax.swing.JOptionPane;
10import javax.swing.JPanel;
11
12import fr.mael.jiwigo.Main;
13import fr.mael.jiwigo.om.Category;
14import fr.mael.jiwigo.om.Image;
15import fr.mael.jiwigo.service.ImageService;
16import fr.mael.jiwigo.transverse.ImagesManagement;
17import fr.mael.jiwigo.transverse.enumeration.PreferencesEnum;
18import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
19import fr.mael.jiwigo.transverse.util.Messages;
20import fr.mael.jiwigo.transverse.util.Tools;
21import fr.mael.jiwigo.transverse.util.filedrop.FileDrop;
22import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement;
23import fr.mael.jiwigo.ui.layout.VerticalLayout;
24
25/**
26   Copyright (c) 2010, Mael
27   All rights reserved.
28
29   Redistribution and use in source and binary forms, with or without
30   modification, are permitted provided that the following conditions are met:
31    * Redistributions of source code must retain the above copyright
32      notice, this list of conditions and the following disclaimer.
33    * Redistributions in binary form must reproduce the above copyright
34      notice, this list of conditions and the following disclaimer in the
35      documentation and/or other materials provided with the distribution.
36    * Neither the name of jiwigo nor the
37      names of its contributors may be used to endorse or promote products
38      derived from this software without specific prior written permission.
39
40   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
41   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
42   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43   DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY
44   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
45   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
48   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
49   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50
51 * @author mael
52 * Class that display the thumbnails of a category
53 */
54public class ThumbnailCategoryPanel extends JPanel implements IThumbnailPanel {
55
56    /**
57     * Logger
58     */
59    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
60            .getLog(ThumbnailCategoryPanel.class);
61    /**
62     * id of the category
63     */
64    private Integer categoryId;
65
66    private Category category;
67
68    /**
69     * thumbnails per line
70     */
71    private int thumbnailPerLine = 7;
72
73    /**
74     * Saved dimension of the panel, used to define the number
75     * of thumbnails on a line
76     */
77    private Dimension savedDimension = new Dimension();
78
79    private ImagesManagement imagesManagement;
80
81    private ThumbnailCategoryPanel() {
82        // TODO Auto-generated constructor stub
83        imagesManagement = ImagesManagement.getInstance();
84    }
85
86    public ThumbnailCategoryPanel(Category category) {
87        this(category.getIdentifier());
88        this.category = category;
89    }
90
91    /**
92     * Constructor
93     * @param categoryId id of the category
94     */
95    public ThumbnailCategoryPanel(Integer categoryId) {
96        this();
97        this.categoryId = categoryId;
98        this.setLayout(new VerticalLayout());
99        if (categoryId != null) {
100            rafraichir(categoryId, false);
101        }
102        //gestion du drag'n drop
103        new FileDrop(System.out, this, new FileDrop.Listener() {
104
105            public void filesDropped(final java.io.File[] files) {
106                if (!imagesManagement.isRememberPrivacyLevel()) {
107                    new DialogPrivacyLevel();
108                }
109                if (imagesManagement.isSendingFiles()) {
110                    JOptionPane.showMessageDialog(null, Messages.getMessage("alreadySendingError"), Messages
111                            .getMessage("error"), JOptionPane.ERROR_MESSAGE);
112                } else {
113                    new Thread(new ThreadEnvoiPhoto(files)).start();
114                }
115            }
116        });
117
118    }
119
120    @Override
121    public void paint(Graphics g) {
122        super.paint(g);
123        if (!MainFrame.getInstance().getSize().equals(savedDimension)) {
124            //                  LOG.debug("paint " + getSize());
125            calculateThumbnailPerLine();
126            addThumbnails();
127        }
128    }
129
130    private void calculateThumbnailPerLine() {
131        savedDimension = MainFrame.getInstance().getSize();
132        int width = savedDimension.width;
133        thumbnailPerLine = width / 150;
134    }
135
136    /**
137     * refreshes the panel
138     * @param categoryId the id of the category
139     */
140    public void rafraichir(Integer categoryId, boolean rafraichir) {
141        this.categoryId = categoryId;
142        try {
143            MainFrame.getInstance().setMessage(Messages.getMessage("thumbviewer_loading"));
144            imagesManagement.setListImage(ImageService.getInstance(Main.sessionManager).listByCategory(categoryId,
145                    rafraichir));
146            calculateThumbnailPerLine();
147            addThumbnails();
148            this.repaint();
149            this.revalidate();
150            MainFrame.getInstance().setMessage(Messages.getMessage("loadingOk"));
151        } catch (Exception e) {
152            LOG.error(Tools.getStackTrace(e));
153            JOptionPane.showMessageDialog(null, Messages.getMessage("imagesListingError"),
154                    Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
155            MainFrame.getInstance().setMessage(Messages.getMessage("imagesListingError"));
156        }
157    }
158
159    /**
160     * Adds the thumbnails to the panel
161     */
162    private void addThumbnails() {
163        this.removeAll();
164        int nb = 1;
165        JPanel panelh = new JPanel(new FlowLayout());
166        try {
167            imagesManagement.setListImage(ImageService.getInstance(Main.sessionManager).listByCategory(categoryId,
168                    false));
169        } catch (IOException e1) {
170            LOG.error(Tools.getStackTrace(e1));
171        } catch (ProxyAuthenticationException e) {
172            LOG.error(Tools.getStackTrace(e));
173        }
174        for (Image image : imagesManagement.getListImage()) {
175            try {
176
177                if (nb == thumbnailPerLine) {
178                    this.add(panelh);
179                    panelh = new JPanel(new FlowLayout());
180                    //              ThumbnailPanel panel = new ThumbnailPanel(image);
181                    //              panelh.add(panel);
182                    nb = 1;
183                } else {
184                    //              ThumbnailPanel panel = new ThumbnailPanel(image);
185                    //              panelh.add(panel);
186                }
187                ThumbnailPanel panel = new ThumbnailPanel(image);
188                panelh.add(panel);
189                nb++;
190
191            } catch (Exception e) {
192
193            }
194        }
195        if (nb != thumbnailPerLine + 1) {
196            this.add(panelh);
197        }
198    }
199
200    /**
201     * @return the categoryId
202     */
203    public Integer getCategoryId() {
204        return categoryId;
205    }
206
207    /**
208     * @param categoryId the categoryId to set
209     */
210    public void setCategoryId(Integer categoryId) {
211        this.categoryId = categoryId;
212    }
213
214    /**
215     * @return the category
216     */
217    public Category getCategory() {
218        return category;
219    }
220
221    /**
222     * @param category the category to set
223     */
224    public void setCategory(Category category) {
225        this.category = category;
226    }
227
228    /**
229     * @author mael
230     * Thread that send the photos
231     */
232    public class ThreadEnvoiPhoto implements Runnable {
233
234        private File[] files;
235
236        public ThreadEnvoiPhoto(File[] files) {
237            this.files = files;
238        }
239
240        @Override
241        public void run() {
242            imagesManagement.setSendingFiles(true);
243            for (int i = 0; i < files.length; i++) {
244                MainFrame.getInstance().setAdditionalMessage(
245                        "<html><i>" + (i + 1) + "/" + files.length + " : </i></html>");
246                int nbProgressBar = ((i + 1) * 100) / files.length;
247                try {
248                    Integer widthOriginal = Integer.valueOf(PreferencesManagement
249                            .getValue(PreferencesEnum.WIDTH_ORIGINALE.getLabel()));
250                    Integer heightOriginal = Integer.valueOf(PreferencesManagement
251                            .getValue(PreferencesEnum.HEIGHT_ORIGINAL.getLabel()));
252                    Double chunkSize = Double.valueOf(PreferencesManagement.getValue(PreferencesEnum.CHUNK_SIZE
253                            .getLabel()));
254
255                    ImageService.getInstance(Main.sessionManager).create(files[i].getCanonicalPath(), categoryId,
256                            widthOriginal, heightOriginal, chunkSize, imagesManagement.getPrivacyLevel());
257                    MainFrame.getInstance()
258                            .setMessage(files[i].getName() + " " + Messages.getMessage("sendingSuccess"));
259                } catch (Exception e) {
260                    LOG.error(Tools.getStackTrace(e));
261                    //displays a dialog if there is an error
262                    JOptionPane.showMessageDialog(null, Messages.getMessage("sendingError") + files[i].getName(),
263                            Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
264                    MainFrame.getInstance().setMessage(Messages.getMessage("sendingError") + " " + files[i].getName());
265                } finally {
266                    MainFrame.getInstance().getProgressBar().setValue(nbProgressBar);
267                }
268            }
269            //refresh
270            MainFrame.getInstance().setAdditionalMessage("");
271            rafraichir(categoryId, true);
272            MainFrame.getInstance().getProgressBar().setValue(0);
273            imagesManagement.setSendingFiles(true);
274        }
275    }
276
277}
Note: See TracBrowser for help on using the repository browser.