package fr.mael.jiwigo.ui.mainframe; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; import java.io.File; import javax.swing.JOptionPane; import javax.swing.JPanel; import fr.mael.jiwigo.om.Category; import fr.mael.jiwigo.om.Image; import fr.mael.jiwigo.transverse.ImagesManagement; import fr.mael.jiwigo.transverse.enumeration.PreferencesEnum; import fr.mael.jiwigo.transverse.exception.JiwigoException; import fr.mael.jiwigo.transverse.util.Messages; import fr.mael.jiwigo.transverse.util.Tools; import fr.mael.jiwigo.transverse.util.filedrop.FileDrop; import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement; import fr.mael.jiwigo.transverse.util.spring.SpringUtils; import fr.mael.jiwigo.ui.layout.VerticalLayout; /** Copyright (c) 2010, Mael All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of jiwigo nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * @author mael * Class that display the thumbnails of a category */ public class ThumbnailCategoryPanel extends JPanel implements IThumbnailPanel { /** * Logger */ public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory .getLog(ThumbnailCategoryPanel.class); /** * id of the category */ private Integer categoryId; private Category category; /** * thumbnails per line */ private int thumbnailPerLine = 7; /** * Saved dimension of the panel, used to define the number * of thumbnails on a line */ private Dimension savedDimension = new Dimension(); private ImagesManagement imagesManagement; private ThumbnailCategoryPanel() { // TODO Auto-generated constructor stub imagesManagement = ImagesManagement.getInstance(); } public ThumbnailCategoryPanel(Category category) { this(category.getIdentifier()); this.category = category; } /** * Constructor * @param categoryId id of the category */ public ThumbnailCategoryPanel(Integer categoryId) { this(); this.categoryId = categoryId; this.setLayout(new VerticalLayout()); if (categoryId != null) { refresh(categoryId, false); } //gestion du drag'n drop new FileDrop(System.out, this, new FileDrop.Listener() { public void filesDropped(final java.io.File[] files) { if (!imagesManagement.isRememberPrivacyLevel()) { new DialogPrivacyLevel(); } if (imagesManagement.isSendingFiles()) { JOptionPane.showMessageDialog(null, Messages.getMessage("alreadySendingError"), Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); } else { new Thread(new ThreadEnvoiPhoto(files)).start(); } } }); } @Override public void paint(Graphics g) { super.paint(g); if (!MainFrame.getInstance().getSize().equals(savedDimension)) { // LOG.debug("paint " + getSize()); calculateThumbnailPerLine(); addThumbnails(); } } private void calculateThumbnailPerLine() { savedDimension = MainFrame.getInstance().getSize(); int width = savedDimension.width; thumbnailPerLine = width / 150; } /** * refreshes the panel * @param categoryId the id of the category */ public void refresh(Integer categoryId, boolean rafraichir) { this.categoryId = categoryId; try { MainFrame.getInstance().setMessage(Messages.getMessage("thumbviewer_loading")); imagesManagement.setListImage(SpringUtils.getImageService().listByCategory(categoryId, rafraichir)); calculateThumbnailPerLine(); addThumbnails(); this.repaint(); this.revalidate(); MainFrame.getInstance().setMessage(Messages.getMessage("loadingOk")); } catch (Exception e) { LOG.error(Tools.getStackTrace(e)); JOptionPane.showMessageDialog(null, Messages.getMessage("imagesListingError"), Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); MainFrame.getInstance().setMessage(Messages.getMessage("imagesListingError")); } } public void refresh() { refresh(categoryId, true); } /** * Adds the thumbnails to the panel */ private void addThumbnails() { this.removeAll(); int nb = 1; JPanel panelh = new JPanel(new FlowLayout()); try { imagesManagement.setListImage(SpringUtils.getImageService().listByCategory(categoryId, false)); } catch (JiwigoException e1) { LOG.error(Tools.getStackTrace(e1)); } for (Image image : imagesManagement.getListImage()) { try { if (nb == thumbnailPerLine) { this.add(panelh); panelh = new JPanel(new FlowLayout()); // ThumbnailPanel panel = new ThumbnailPanel(image); // panelh.add(panel); nb = 1; } else { // ThumbnailPanel panel = new ThumbnailPanel(image); // panelh.add(panel); } ThumbnailPanel panel = new ThumbnailPanel(image, this); panelh.add(panel); nb++; } catch (Exception e) { } } if (nb != thumbnailPerLine + 1) { this.add(panelh); } } /** * @return the categoryId */ public Integer getCategoryId() { return categoryId; } /** * @param categoryId the categoryId to set */ public void setCategoryId(Integer categoryId) { this.categoryId = categoryId; } /** * @return the category */ public Category getCategory() { return category; } /** * @param category the category to set */ public void setCategory(Category category) { this.category = category; } /** * @author mael * Thread that send the photos */ public class ThreadEnvoiPhoto implements Runnable { private File[] files; public ThreadEnvoiPhoto(File[] files) { this.files = files; } @Override public void run() { imagesManagement.setSendingFiles(true); for (int i = 0; i < files.length; i++) { MainFrame.getInstance().setAdditionalMessage( "" + (i + 1) + "/" + files.length + " : "); int nbProgressBar = ((i + 1) * 100) / files.length; if (PreferencesManagement.getValue(PreferencesEnum.SENDING_METHOD.getLabel()).equals("0")) { try { SpringUtils.getImageService().addSimple(files[i], category.getIdentifier(), files[i].getName(), imagesManagement.getPrivacyLevel()); } catch (JiwigoException e) { LOG.error(Tools.getStackTrace(e)); //displays a dialog if there is an error JOptionPane.showMessageDialog(null, Messages.getMessage("sendingError") + files[i].getName(), Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); MainFrame.getInstance().setMessage( Messages.getMessage("sendingError") + " " + files[i].getName()); } } else { try { Integer widthOriginal = Integer.valueOf(PreferencesManagement .getValue(PreferencesEnum.WIDTH_ORIGINALE.getLabel())); Integer heightOriginal = Integer.valueOf(PreferencesManagement .getValue(PreferencesEnum.HEIGHT_ORIGINAL.getLabel())); Double chunkSize = Double.valueOf(PreferencesManagement.getValue(PreferencesEnum.CHUNK_SIZE .getLabel())); SpringUtils.getImageService().create(files[i].getCanonicalPath(), categoryId, widthOriginal, heightOriginal, chunkSize, imagesManagement.getPrivacyLevel()); MainFrame.getInstance().setMessage( files[i].getName() + " " + Messages.getMessage("sendingSuccess")); } catch (Exception e) { LOG.error(Tools.getStackTrace(e)); //displays a dialog if there is an error JOptionPane.showMessageDialog(null, Messages.getMessage("sendingError") + files[i].getName(), Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); MainFrame.getInstance().setMessage( Messages.getMessage("sendingError") + " " + files[i].getName()); } finally { MainFrame.getInstance().getProgressBar().setValue(nbProgressBar); } } } //refresh MainFrame.getInstance().setAdditionalMessage(""); refresh(categoryId, true); MainFrame.getInstance().getProgressBar().setValue(0); imagesManagement.setSendingFiles(false); } } }