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

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

Adds a wait cursor for time consuming operations.

File size: 9.3 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;
7
8import javax.swing.JOptionPane;
9import javax.swing.JPanel;
10
11import fr.mael.jiwigo.om.Category;
12import fr.mael.jiwigo.om.Image;
13import fr.mael.jiwigo.transverse.ImagesManagement;
14import fr.mael.jiwigo.transverse.enumeration.PreferencesEnum;
15import fr.mael.jiwigo.transverse.exception.JiwigoException;
16import fr.mael.jiwigo.transverse.util.Messages;
17import fr.mael.jiwigo.transverse.util.Tools;
18import fr.mael.jiwigo.transverse.util.filedrop.FileDrop;
19import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement;
20import fr.mael.jiwigo.transverse.util.spring.SpringUtils;
21import fr.mael.jiwigo.ui.layout.VerticalLayout;
22
23/**
24   Copyright (c) 2010, Mael
25   All rights reserved.
26
27   Redistribution and use in source and binary forms, with or without
28   modification, are permitted provided that the following conditions are met:
29    * Redistributions of source code must retain the above copyright
30      notice, this list of conditions and the following disclaimer.
31    * Redistributions in binary form must reproduce the above copyright
32      notice, this list of conditions and the following disclaimer in the
33      documentation and/or other materials provided with the distribution.
34    * Neither the name of jiwigo nor the
35      names of its contributors may be used to endorse or promote products
36      derived from this software without specific prior written permission.
37
38   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
39   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
40   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41   DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY
42   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
43   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
47   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48
49 * @author mael
50 * Class that display the thumbnails of a category
51 */
52public class ThumbnailCategoryPanel extends JPanel implements IThumbnailPanel {
53
54    /**
55     * Logger
56     */
57    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
58            .getLog(ThumbnailCategoryPanel.class);
59    /**
60     * id of the category
61     */
62    private Integer categoryId;
63
64    private Category category;
65
66    /**
67     * thumbnails per line
68     */
69    private int thumbnailPerLine = 7;
70
71    /**
72     * Saved dimension of the panel, used to define the number
73     * of thumbnails on a line
74     */
75    private Dimension savedDimension = new Dimension();
76
77    private ImagesManagement imagesManagement;
78
79    private ThumbnailCategoryPanel() {
80        // TODO Auto-generated constructor stub
81        imagesManagement = ImagesManagement.getInstance();
82    }
83
84    public ThumbnailCategoryPanel(Category category) {
85        this(category.getIdentifier());
86        this.category = category;
87    }
88
89    /**
90     * Constructor
91     * @param categoryId id of the category
92     */
93    public ThumbnailCategoryPanel(Integer categoryId) {
94        this();
95        this.categoryId = categoryId;
96        this.setLayout(new VerticalLayout());
97        if (categoryId != null) {
98            refresh(categoryId, false);
99        }
100        //gestion du drag'n drop
101        new FileDrop(System.out, this, new FileDrop.Listener() {
102
103            public void filesDropped(final java.io.File[] files) {
104                if (!imagesManagement.isRememberPrivacyLevel()) {
105                    new DialogPrivacyLevel();
106                }
107                if (imagesManagement.isSendingFiles()) {
108                    JOptionPane.showMessageDialog(null, Messages.getMessage("alreadySendingError"),
109                            Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
110                } else {
111                    new Thread(new ThreadEnvoiPhoto(files)).start();
112                }
113            }
114        });
115
116    }
117
118    @Override
119    public void paint(Graphics g) {
120        super.paint(g);
121        if (!MainFrame.getInstance().getSize().equals(savedDimension)) {
122            //                  LOG.debug("paint " + getSize());
123            calculateThumbnailPerLine();
124            addThumbnails();
125        }
126    }
127
128    private void calculateThumbnailPerLine() {
129        savedDimension = MainFrame.getInstance().getSize();
130        int width = savedDimension.width;
131        thumbnailPerLine = width / 150;
132    }
133
134    /**
135     * refreshes the panel
136     * @param categoryId the id of the category
137     */
138    public void refresh(Integer categoryId, boolean rafraichir) {
139        MainFrame.getInstance().showWaitCursor(true);
140        this.categoryId = categoryId;
141        try {
142            MainFrame.getInstance().setMessage(Messages.getMessage("thumbviewer_loading"));
143            imagesManagement.setListImage(SpringUtils.getImageService().listByCategory(categoryId, rafraichir));
144            calculateThumbnailPerLine();
145            addThumbnails();
146            this.repaint();
147            this.revalidate();
148            MainFrame.getInstance().setMessage(Messages.getMessage("loadingOk"));
149        } catch (Exception e) {
150            LOG.error(Tools.getStackTrace(e));
151            JOptionPane.showMessageDialog(null, Messages.getMessage("imagesListingError"),
152                    Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
153            MainFrame.getInstance().setMessage(Messages.getMessage("imagesListingError"));
154        }
155        MainFrame.getInstance().showWaitCursor(false);
156    }
157
158    public void refresh() {
159        refresh(categoryId, true);
160    }
161
162    /**
163     * Adds the thumbnails to the panel
164     */
165    private void addThumbnails() {
166        this.removeAll();
167        int nb = 1;
168        JPanel panelh = new JPanel(new FlowLayout());
169        try {
170            imagesManagement.setListImage(SpringUtils.getImageService().listByCategory(categoryId, false));
171        } catch (JiwigoException e1) {
172            LOG.error(Tools.getStackTrace(e1));
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, this);
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                if (PreferencesManagement.getValue(PreferencesEnum.SENDING_METHOD.getLabel()).equals("0")) {
248                    try {
249                        SpringUtils.getImageService().addSimple(files[i], category.getIdentifier(), files[i].getName(),
250                                imagesManagement.getPrivacyLevel());
251                    } catch (JiwigoException e) {
252                        LOG.error(Tools.getStackTrace(e));
253                        //displays a dialog if there is an error
254                        JOptionPane.showMessageDialog(null, Messages.getMessage("sendingError") + files[i].getName(),
255                                Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
256                        MainFrame.getInstance().setMessage(
257                                Messages.getMessage("sendingError") + " " + files[i].getName());
258                    }
259                } else {
260                    try {
261                        Integer widthOriginal = Integer.valueOf(PreferencesManagement
262                                .getValue(PreferencesEnum.WIDTH_ORIGINALE.getLabel()));
263                        Integer heightOriginal = Integer.valueOf(PreferencesManagement
264                                .getValue(PreferencesEnum.HEIGHT_ORIGINAL.getLabel()));
265                        Double chunkSize = Double.valueOf(PreferencesManagement.getValue(PreferencesEnum.CHUNK_SIZE
266                                .getLabel()));
267
268                        SpringUtils.getImageService().create(files[i].getCanonicalPath(), categoryId, widthOriginal,
269                                heightOriginal, chunkSize, imagesManagement.getPrivacyLevel());
270                        MainFrame.getInstance().setMessage(
271                                files[i].getName() + " " + Messages.getMessage("sendingSuccess"));
272                    } catch (Exception e) {
273                        LOG.error(Tools.getStackTrace(e));
274                        //displays a dialog if there is an error
275                        JOptionPane.showMessageDialog(null, Messages.getMessage("sendingError") + files[i].getName(),
276                                Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
277                        MainFrame.getInstance().setMessage(
278                                Messages.getMessage("sendingError") + " " + files[i].getName());
279                    } finally {
280                        MainFrame.getInstance().getProgressBar().setValue(nbProgressBar);
281                    }
282                }
283            }
284            //refresh
285            MainFrame.getInstance().setAdditionalMessage("");
286            refresh(categoryId, true);
287            MainFrame.getInstance().getProgressBar().setValue(0);
288            imagesManagement.setSendingFiles(false);
289        }
290    }
291
292}
Note: See TracBrowser for help on using the repository browser.