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