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

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

All services have been removed from this project to be integrated in a new project : jiwigo-ws-api (not yet on the svn).
The current project now uses jiwigo-ws-api as a maven dependency (explanations can be found here : http://www.jiwigo.com/api.html )

File size: 8.2 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.util.Messages;
19import fr.mael.jiwigo.transverse.util.Outil;
20import fr.mael.jiwigo.transverse.util.filedrop.FileDrop;
21import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement;
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.getIdentifiant());
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            savedDimension = MainFrame.getInstance().getSize();
125            int width = savedDimension.width;
126            thumbnailPerLine = width / 150;
127            addThumbnails();
128        }
129    }
130
131    /**
132     * refreshes the panel
133     * @param categoryId the id of the category
134     */
135    public void rafraichir(Integer categoryId, boolean rafraichir) {
136        this.categoryId = categoryId;
137        try {
138            MainFrame.getInstance().setMessage(Messages.getMessage("thumbviewer_loading"));
139            imagesManagement.setListImage(ImageService.getInstance(Main.sessionManager).listerParCategory(categoryId,
140                    rafraichir));
141            addThumbnails();
142            this.repaint();
143            this.revalidate();
144            MainFrame.getInstance().setMessage(Messages.getMessage("loadingOk"));
145        } catch (Exception e) {
146            LOG.error(Outil.getStackTrace(e));
147            JOptionPane.showMessageDialog(null, Messages.getMessage("imagesListingError"),
148                    Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
149            MainFrame.getInstance().setMessage(Messages.getMessage("imagesListingError"));
150        }
151    }
152
153    /**
154     * Adds the thumbnails to the panel
155     */
156    private void addThumbnails() {
157        this.removeAll();
158        int nb = 1;
159        JPanel panelh = new JPanel(new FlowLayout());
160        try {
161            imagesManagement.setListImage(ImageService.getInstance(Main.sessionManager).listerParCategory(categoryId,
162                    false));
163        } catch (IOException e1) {
164            e1.printStackTrace();
165        }
166        for (Image image : imagesManagement.getListImage()) {
167            try {
168
169                if (nb == thumbnailPerLine) {
170                    this.add(panelh);
171                    panelh = new JPanel(new FlowLayout());
172                    nb = 0;
173                } else {
174                    ThumbnailPanel panel = new ThumbnailPanel(image);
175                    panelh.add(panel);
176                }
177                nb++;
178
179            } catch (Exception e) {
180
181            }
182        }
183        if (nb != thumbnailPerLine + 1) {
184            this.add(panelh);
185        }
186    }
187
188    /**
189     * @return the categoryId
190     */
191    public Integer getCategoryId() {
192        return categoryId;
193    }
194
195    /**
196     * @param categoryId the categoryId to set
197     */
198    public void setCategoryId(Integer categoryId) {
199        this.categoryId = categoryId;
200    }
201
202    /**
203     * @return the category
204     */
205    public Category getCategory() {
206        return category;
207    }
208
209    /**
210     * @param category the category to set
211     */
212    public void setCategory(Category category) {
213        this.category = category;
214    }
215
216    /**
217     * @author mael
218     * Thread that send the photos
219     */
220    public class ThreadEnvoiPhoto implements Runnable {
221
222        private File[] files;
223
224        public ThreadEnvoiPhoto(File[] files) {
225            this.files = files;
226        }
227
228        @Override
229        public void run() {
230            imagesManagement.setSendingFiles(true);
231            for (int i = 0; i < files.length; i++) {
232                MainFrame.getInstance().setAdditionalMessage(
233                        "<html><i>" + (i + 1) + "/" + files.length + " : </i></html>");
234                int nbProgressBar = ((i + 1) * 100) / files.length;
235                try {
236                    Integer widthOriginal = Integer.valueOf(PreferencesManagement
237                            .getValue(PreferencesEnum.WIDTH_ORIGINALE.getLabel()));
238                    Integer heightOriginal = Integer.valueOf(PreferencesManagement
239                            .getValue(PreferencesEnum.HEIGHT_ORIGINAL.getLabel()));
240                    Double chunkSize = Double.valueOf(PreferencesManagement.getValue(PreferencesEnum.CHUNK_SIZE
241                            .getLabel()));
242
243                    ImageService.getInstance(Main.sessionManager).creer(files[i].getCanonicalPath(), categoryId,
244                            widthOriginal, heightOriginal, chunkSize, imagesManagement.getPrivacyLevel());
245                    MainFrame.getInstance()
246                            .setMessage(files[i].getName() + " " + Messages.getMessage("sendingSuccess"));
247                } catch (Exception e) {
248                    LOG.error(Outil.getStackTrace(e));
249                    //displays a dialog if there is an error
250                    JOptionPane.showMessageDialog(null, Messages.getMessage("sendingError") + files[i].getName(),
251                            Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
252                    MainFrame.getInstance().setMessage(Messages.getMessage("sendingError") + " " + files[i].getName());
253                } finally {
254                    MainFrame.getInstance().getProgressBar().setValue(nbProgressBar);
255                }
256            }
257            //refresh
258            MainFrame.getInstance().setAdditionalMessage("");
259            rafraichir(categoryId, true);
260            MainFrame.getInstance().getProgressBar().setValue(0);
261            imagesManagement.setSendingFiles(true);
262        }
263    }
264
265}
Note: See TracBrowser for help on using the repository browser.