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

Last change on this file since 7223 was 7223, checked in by mlg, 14 years ago

bug correction
is now impossible to add files if the application is already sending photos.
feature:0001887

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