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