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