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

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

Adds spring for the dependency injection
Deletion of jdom (not useful just for a class that manipulates a simple XML file)

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