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

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

Integrates last jiwigo-ws-api modifications

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