[6821] | 1 | package fr.mael.jiwigo.ui.mainframe; |
---|
| 2 | |
---|
[7221] | 3 | import java.awt.Dimension; |
---|
[6821] | 4 | import java.awt.FlowLayout; |
---|
[7221] | 5 | import java.awt.Graphics; |
---|
[6833] | 6 | import java.io.File; |
---|
[8829] | 7 | |
---|
[6821] | 8 | import javax.swing.JOptionPane; |
---|
| 9 | import javax.swing.JPanel; |
---|
[8829] | 10 | |
---|
[6958] | 11 | import fr.mael.jiwigo.om.Category; |
---|
[6821] | 12 | import fr.mael.jiwigo.om.Image; |
---|
[7070] | 13 | import fr.mael.jiwigo.transverse.ImagesManagement; |
---|
[9385] | 14 | import fr.mael.jiwigo.transverse.enumeration.PreferencesEnum; |
---|
[10697] | 15 | import fr.mael.jiwigo.transverse.exception.JiwigoException; |
---|
[6821] | 16 | import fr.mael.jiwigo.transverse.util.Messages; |
---|
[9393] | 17 | import fr.mael.jiwigo.transverse.util.Tools; |
---|
[6821] | 18 | import fr.mael.jiwigo.transverse.util.filedrop.FileDrop; |
---|
[9385] | 19 | import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement; |
---|
[9921] | 20 | import fr.mael.jiwigo.transverse.util.spring.SpringUtils; |
---|
[6839] | 21 | import fr.mael.jiwigo.ui.layout.VerticalLayout; |
---|
[6821] | 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. |
---|
[9385] | 48 | |
---|
[6821] | 49 | * @author mael |
---|
[6980] | 50 | * Class that display the thumbnails of a category |
---|
[6821] | 51 | */ |
---|
[6972] | 52 | public class ThumbnailCategoryPanel extends JPanel implements IThumbnailPanel { |
---|
[6821] | 53 | |
---|
[8829] | 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; |
---|
[6958] | 63 | |
---|
[8829] | 64 | private Category category; |
---|
[6958] | 65 | |
---|
[8829] | 66 | /** |
---|
| 67 | * thumbnails per line |
---|
| 68 | */ |
---|
| 69 | private int thumbnailPerLine = 7; |
---|
[6821] | 70 | |
---|
[8829] | 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(); |
---|
[6821] | 76 | |
---|
[8829] | 77 | private ImagesManagement imagesManagement; |
---|
[6821] | 78 | |
---|
[8829] | 79 | private ThumbnailCategoryPanel() { |
---|
| 80 | // TODO Auto-generated constructor stub |
---|
| 81 | imagesManagement = ImagesManagement.getInstance(); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | public ThumbnailCategoryPanel(Category category) { |
---|
[9393] | 85 | this(category.getIdentifier()); |
---|
[8829] | 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) { |
---|
[10717] | 98 | refresh(categoryId, false); |
---|
[7221] | 99 | } |
---|
[8829] | 100 | //gestion du drag'n drop |
---|
| 101 | new FileDrop(System.out, this, new FileDrop.Listener() { |
---|
[6821] | 102 | |
---|
[8829] | 103 | public void filesDropped(final java.io.File[] files) { |
---|
| 104 | if (!imagesManagement.isRememberPrivacyLevel()) { |
---|
| 105 | new DialogPrivacyLevel(); |
---|
[7221] | 106 | } |
---|
[8829] | 107 | if (imagesManagement.isSendingFiles()) { |
---|
[10697] | 108 | JOptionPane.showMessageDialog(null, Messages.getMessage("alreadySendingError"), |
---|
| 109 | Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); |
---|
[8829] | 110 | } else { |
---|
| 111 | new Thread(new ThreadEnvoiPhoto(files)).start(); |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | }); |
---|
[6821] | 115 | |
---|
[8829] | 116 | } |
---|
[6958] | 117 | |
---|
[8829] | 118 | @Override |
---|
| 119 | public void paint(Graphics g) { |
---|
| 120 | super.paint(g); |
---|
| 121 | if (!MainFrame.getInstance().getSize().equals(savedDimension)) { |
---|
| 122 | // LOG.debug("paint " + getSize()); |
---|
[9892] | 123 | calculateThumbnailPerLine(); |
---|
[8829] | 124 | addThumbnails(); |
---|
[6833] | 125 | } |
---|
[8829] | 126 | } |
---|
[6833] | 127 | |
---|
[9892] | 128 | private void calculateThumbnailPerLine() { |
---|
| 129 | savedDimension = MainFrame.getInstance().getSize(); |
---|
| 130 | int width = savedDimension.width; |
---|
| 131 | thumbnailPerLine = width / 150; |
---|
| 132 | } |
---|
| 133 | |
---|
[8829] | 134 | /** |
---|
| 135 | * refreshes the panel |
---|
| 136 | * @param categoryId the id of the category |
---|
| 137 | */ |
---|
[10717] | 138 | public void refresh(Integer categoryId, boolean rafraichir) { |
---|
[8829] | 139 | this.categoryId = categoryId; |
---|
| 140 | try { |
---|
| 141 | MainFrame.getInstance().setMessage(Messages.getMessage("thumbviewer_loading")); |
---|
[9921] | 142 | imagesManagement.setListImage(SpringUtils.getImageService().listByCategory(categoryId, rafraichir)); |
---|
[9892] | 143 | calculateThumbnailPerLine(); |
---|
[8829] | 144 | addThumbnails(); |
---|
| 145 | this.repaint(); |
---|
| 146 | this.revalidate(); |
---|
| 147 | MainFrame.getInstance().setMessage(Messages.getMessage("loadingOk")); |
---|
| 148 | } catch (Exception e) { |
---|
[9393] | 149 | LOG.error(Tools.getStackTrace(e)); |
---|
[8829] | 150 | JOptionPane.showMessageDialog(null, Messages.getMessage("imagesListingError"), |
---|
| 151 | Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); |
---|
| 152 | MainFrame.getInstance().setMessage(Messages.getMessage("imagesListingError")); |
---|
[7221] | 153 | } |
---|
[8829] | 154 | } |
---|
[6833] | 155 | |
---|
[10717] | 156 | public void refresh() { |
---|
| 157 | refresh(categoryId, true); |
---|
| 158 | } |
---|
| 159 | |
---|
[8829] | 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 { |
---|
[9921] | 168 | imagesManagement.setListImage(SpringUtils.getImageService().listByCategory(categoryId, false)); |
---|
[10697] | 169 | } catch (JiwigoException e1) { |
---|
[9431] | 170 | LOG.error(Tools.getStackTrace(e1)); |
---|
[6833] | 171 | } |
---|
[8829] | 172 | for (Image image : imagesManagement.getListImage()) { |
---|
| 173 | try { |
---|
[6833] | 174 | |
---|
[8829] | 175 | if (nb == thumbnailPerLine) { |
---|
| 176 | this.add(panelh); |
---|
| 177 | panelh = new JPanel(new FlowLayout()); |
---|
[9892] | 178 | // ThumbnailPanel panel = new ThumbnailPanel(image); |
---|
| 179 | // panelh.add(panel); |
---|
| 180 | nb = 1; |
---|
[8829] | 181 | } else { |
---|
[9892] | 182 | // ThumbnailPanel panel = new ThumbnailPanel(image); |
---|
| 183 | // panelh.add(panel); |
---|
[7223] | 184 | } |
---|
[10717] | 185 | ThumbnailPanel panel = new ThumbnailPanel(image, this); |
---|
[9892] | 186 | panelh.add(panel); |
---|
[8829] | 187 | nb++; |
---|
[6833] | 188 | |
---|
[8829] | 189 | } catch (Exception e) { |
---|
[6833] | 190 | |
---|
[8829] | 191 | } |
---|
[7221] | 192 | } |
---|
[8829] | 193 | if (nb != thumbnailPerLine + 1) { |
---|
| 194 | this.add(panelh); |
---|
| 195 | } |
---|
| 196 | } |
---|
[6833] | 197 | |
---|
[8829] | 198 | /** |
---|
| 199 | * @return the categoryId |
---|
| 200 | */ |
---|
| 201 | public Integer getCategoryId() { |
---|
| 202 | return categoryId; |
---|
| 203 | } |
---|
[6833] | 204 | |
---|
[8829] | 205 | /** |
---|
| 206 | * @param categoryId the categoryId to set |
---|
| 207 | */ |
---|
| 208 | public void setCategoryId(Integer categoryId) { |
---|
| 209 | this.categoryId = categoryId; |
---|
| 210 | } |
---|
[7221] | 211 | |
---|
[8829] | 212 | /** |
---|
| 213 | * @return the category |
---|
| 214 | */ |
---|
| 215 | public Category getCategory() { |
---|
| 216 | return category; |
---|
| 217 | } |
---|
[7221] | 218 | |
---|
[8829] | 219 | /** |
---|
| 220 | * @param category the category to set |
---|
| 221 | */ |
---|
| 222 | public void setCategory(Category category) { |
---|
| 223 | this.category = category; |
---|
| 224 | } |
---|
[7221] | 225 | |
---|
[8829] | 226 | /** |
---|
| 227 | * @author mael |
---|
| 228 | * Thread that send the photos |
---|
| 229 | */ |
---|
| 230 | public class ThreadEnvoiPhoto implements Runnable { |
---|
[7221] | 231 | |
---|
[8829] | 232 | private File[] files; |
---|
[7221] | 233 | |
---|
[8829] | 234 | public ThreadEnvoiPhoto(File[] files) { |
---|
| 235 | this.files = files; |
---|
[7221] | 236 | } |
---|
| 237 | |
---|
[8829] | 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; |
---|
[10748] | 245 | if (PreferencesManagement.getValue(PreferencesEnum.SENDING_METHOD.getLabel()).equals("0")) { |
---|
| 246 | try { |
---|
[10750] | 247 | SpringUtils.getImageService().addSimple(files[i], category.getIdentifier(), files[i].getName(), |
---|
| 248 | imagesManagement.getPrivacyLevel()); |
---|
[10748] | 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())); |
---|
[7221] | 265 | |
---|
[10748] | 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 | } |
---|
[6833] | 280 | } |
---|
[8829] | 281 | } |
---|
| 282 | //refresh |
---|
| 283 | MainFrame.getInstance().setAdditionalMessage(""); |
---|
[10717] | 284 | refresh(categoryId, true); |
---|
[8829] | 285 | MainFrame.getInstance().getProgressBar().setValue(0); |
---|
[10748] | 286 | imagesManagement.setSendingFiles(false); |
---|
[6833] | 287 | } |
---|
[8829] | 288 | } |
---|
[6839] | 289 | |
---|
[6821] | 290 | } |
---|