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 | |
---|
8 | import javax.swing.JOptionPane; |
---|
9 | import javax.swing.JPanel; |
---|
10 | |
---|
11 | import fr.mael.jiwigo.om.Category; |
---|
12 | import fr.mael.jiwigo.om.Image; |
---|
13 | import fr.mael.jiwigo.transverse.ImagesManagement; |
---|
14 | import fr.mael.jiwigo.transverse.enumeration.PreferencesEnum; |
---|
15 | import fr.mael.jiwigo.transverse.exception.JiwigoException; |
---|
16 | import fr.mael.jiwigo.transverse.util.Messages; |
---|
17 | import fr.mael.jiwigo.transverse.util.Tools; |
---|
18 | import fr.mael.jiwigo.transverse.util.filedrop.FileDrop; |
---|
19 | import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement; |
---|
20 | import fr.mael.jiwigo.transverse.util.spring.SpringUtils; |
---|
21 | import 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 | */ |
---|
52 | public 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 | refresh(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 refresh(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 | public void refresh() { |
---|
157 | refresh(categoryId, true); |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | * Adds the thumbnails to the panel |
---|
162 | */ |
---|
163 | private void addThumbnails() { |
---|
164 | this.removeAll(); |
---|
165 | int nb = 1; |
---|
166 | JPanel panelh = new JPanel(new FlowLayout()); |
---|
167 | try { |
---|
168 | imagesManagement.setListImage(SpringUtils.getImageService().listByCategory(categoryId, false)); |
---|
169 | } catch (JiwigoException e1) { |
---|
170 | LOG.error(Tools.getStackTrace(e1)); |
---|
171 | } |
---|
172 | for (Image image : imagesManagement.getListImage()) { |
---|
173 | try { |
---|
174 | |
---|
175 | if (nb == thumbnailPerLine) { |
---|
176 | this.add(panelh); |
---|
177 | panelh = new JPanel(new FlowLayout()); |
---|
178 | // ThumbnailPanel panel = new ThumbnailPanel(image); |
---|
179 | // panelh.add(panel); |
---|
180 | nb = 1; |
---|
181 | } else { |
---|
182 | // ThumbnailPanel panel = new ThumbnailPanel(image); |
---|
183 | // panelh.add(panel); |
---|
184 | } |
---|
185 | ThumbnailPanel panel = new ThumbnailPanel(image, this); |
---|
186 | panelh.add(panel); |
---|
187 | nb++; |
---|
188 | |
---|
189 | } catch (Exception e) { |
---|
190 | |
---|
191 | } |
---|
192 | } |
---|
193 | if (nb != thumbnailPerLine + 1) { |
---|
194 | this.add(panelh); |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | /** |
---|
199 | * @return the categoryId |
---|
200 | */ |
---|
201 | public Integer getCategoryId() { |
---|
202 | return categoryId; |
---|
203 | } |
---|
204 | |
---|
205 | /** |
---|
206 | * @param categoryId the categoryId to set |
---|
207 | */ |
---|
208 | public void setCategoryId(Integer categoryId) { |
---|
209 | this.categoryId = categoryId; |
---|
210 | } |
---|
211 | |
---|
212 | /** |
---|
213 | * @return the category |
---|
214 | */ |
---|
215 | public Category getCategory() { |
---|
216 | return category; |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | * @param category the category to set |
---|
221 | */ |
---|
222 | public void setCategory(Category category) { |
---|
223 | this.category = category; |
---|
224 | } |
---|
225 | |
---|
226 | /** |
---|
227 | * @author mael |
---|
228 | * Thread that send the photos |
---|
229 | */ |
---|
230 | public class ThreadEnvoiPhoto implements Runnable { |
---|
231 | |
---|
232 | private File[] files; |
---|
233 | |
---|
234 | public ThreadEnvoiPhoto(File[] files) { |
---|
235 | this.files = files; |
---|
236 | } |
---|
237 | |
---|
238 | @Override |
---|
239 | public void run() { |
---|
240 | imagesManagement.setSendingFiles(true); |
---|
241 | for (int i = 0; i < files.length; i++) { |
---|
242 | MainFrame.getInstance().setAdditionalMessage( |
---|
243 | "<html><i>" + (i + 1) + "/" + files.length + " : </i></html>"); |
---|
244 | int nbProgressBar = ((i + 1) * 100) / files.length; |
---|
245 | if (PreferencesManagement.getValue(PreferencesEnum.SENDING_METHOD.getLabel()).equals("0")) { |
---|
246 | try { |
---|
247 | SpringUtils.getImageService().addSimple(files[i], category.getIdentifier(), files[i].getName(), |
---|
248 | imagesManagement.getPrivacyLevel()); |
---|
249 | } catch (JiwigoException e) { |
---|
250 | LOG.error(Tools.getStackTrace(e)); |
---|
251 | //displays a dialog if there is an error |
---|
252 | JOptionPane.showMessageDialog(null, Messages.getMessage("sendingError") + files[i].getName(), |
---|
253 | Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); |
---|
254 | MainFrame.getInstance().setMessage( |
---|
255 | Messages.getMessage("sendingError") + " " + files[i].getName()); |
---|
256 | } |
---|
257 | } else { |
---|
258 | try { |
---|
259 | Integer widthOriginal = Integer.valueOf(PreferencesManagement |
---|
260 | .getValue(PreferencesEnum.WIDTH_ORIGINALE.getLabel())); |
---|
261 | Integer heightOriginal = Integer.valueOf(PreferencesManagement |
---|
262 | .getValue(PreferencesEnum.HEIGHT_ORIGINAL.getLabel())); |
---|
263 | Double chunkSize = Double.valueOf(PreferencesManagement.getValue(PreferencesEnum.CHUNK_SIZE |
---|
264 | .getLabel())); |
---|
265 | |
---|
266 | SpringUtils.getImageService().create(files[i].getCanonicalPath(), categoryId, widthOriginal, |
---|
267 | heightOriginal, chunkSize, imagesManagement.getPrivacyLevel()); |
---|
268 | MainFrame.getInstance().setMessage( |
---|
269 | files[i].getName() + " " + Messages.getMessage("sendingSuccess")); |
---|
270 | } catch (Exception e) { |
---|
271 | LOG.error(Tools.getStackTrace(e)); |
---|
272 | //displays a dialog if there is an error |
---|
273 | JOptionPane.showMessageDialog(null, Messages.getMessage("sendingError") + files[i].getName(), |
---|
274 | Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); |
---|
275 | MainFrame.getInstance().setMessage( |
---|
276 | Messages.getMessage("sendingError") + " " + files[i].getName()); |
---|
277 | } finally { |
---|
278 | MainFrame.getInstance().getProgressBar().setValue(nbProgressBar); |
---|
279 | } |
---|
280 | } |
---|
281 | } |
---|
282 | //refresh |
---|
283 | MainFrame.getInstance().setAdditionalMessage(""); |
---|
284 | refresh(categoryId, true); |
---|
285 | MainFrame.getInstance().getProgressBar().setValue(0); |
---|
286 | imagesManagement.setSendingFiles(false); |
---|
287 | } |
---|
288 | } |
---|
289 | |
---|
290 | } |
---|