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