package fr.mael.jiwigo.ui.search; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JOptionPane; import fr.mael.jiwigo.om.Category; import fr.mael.jiwigo.transverse.ImagesManagement; import fr.mael.jiwigo.transverse.enumeration.PreferencesEnum; import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException; import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; import fr.mael.jiwigo.transverse.exception.WrongChunkSizeException; import fr.mael.jiwigo.transverse.util.Messages; import fr.mael.jiwigo.transverse.util.Tools; import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement; import fr.mael.jiwigo.transverse.util.spring.SpringUtils; import fr.mael.jiwigo.ui.mainframe.MainFrame; /** * 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 * Renderer used to display the category list with a hierarchy * */ public class DialogChooseCategory extends JDialog implements ActionListener { /** * ArrayList containing all files to send */ private ArrayList filesToSend; /** * Combobox containing all categories, with a hierarchical display */ private JComboBox comboCategories; private JButton okButton; private JButton cancelButton; /** * categories list */ private List categories; private JLabel labelCategory; /** * Logger */ public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory .getLog(DialogChooseCategory.class); public DialogChooseCategory(ArrayList filesToSend) { super(MainFrame.getInstance()); this.filesToSend = filesToSend; okButton = new JButton("Ok"); cancelButton = new JButton("Annuler"); okButton.addActionListener(this); cancelButton.addActionListener(this); labelCategory = new JLabel("Categorie : "); comboCategories = new JComboBox(); comboCategories.setRenderer(new ComboCategoryRenderer()); try { categories = SpringUtils.getCategoryService().makeTree(); createCategoriesTree(); } catch (IOException e) { e.printStackTrace(); } catch (ProxyAuthenticationException e) { LOG.error(Tools.getStackTrace(e)); } this.setLayout(new GridBagLayout()); GridBagConstraints constraints = new GridBagConstraints(); constraints.gridx = 0; constraints.gridy = 0; this.add(labelCategory, constraints); constraints.gridx++; this.add(comboCategories, constraints); constraints.gridx = 0; constraints.gridy++; this.add(okButton, constraints); constraints.gridx++; this.add(cancelButton, constraints); this.pack(); this.setLocationRelativeTo(null); this.setVisible(true); } /** * Method that create the first level of the combobox list * it calls the method that creates the hierarchy */ private void createCategoriesTree() { for (Category category : categories) { if (category.getParentCategories().size() == 0) { category.setLevel(0); comboCategories.addItem(category); createRecursiveCategoriesTree(1, category); } } } /** * Method that creates the hierarchical view of the combobox list * @param level * @param category */ private void createRecursiveCategoriesTree(int level, Category category) { for (Category cat : category.getChildCategories()) { cat.setLevel(level); comboCategories.addItem(cat); int levelTemp = level + 1; createRecursiveCategoriesTree(levelTemp, cat); } } @Override public void actionPerformed(ActionEvent arg0) { if (arg0.getSource().equals(okButton)) { Category category = (Category) comboCategories.getSelectedItem(); List alreadyExistingFiles = new ArrayList(); List unExceptedFilesError = new ArrayList(); //loop on all files to send. //if there is a WrongChunkSizeException, the loop stops //if there is a FileAlreadyExistsException or an Exception //the file is just added to a list to display the error messages //at the end of the loop for (File file : filesToSend) { 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(file.getAbsolutePath(), category.getIdentifier(), widthOriginal, heightOriginal, chunkSize, ImagesManagement.getInstance().getPrivacyLevel()); } catch (WrongChunkSizeException ex) { JOptionPane.showMessageDialog(null, Messages.getMessage("wrongChunkSizeError"), Messages .getMessage("error"), JOptionPane.ERROR_MESSAGE); LOG.error(Tools.getStackTrace(ex)); break; } catch (FileAlreadyExistsException e) { alreadyExistingFiles.add(file); } catch (Exception e) { unExceptedFilesError.add(file); LOG.error(Tools.getStackTrace(e)); } } //searches for FileAlreadyExistsException and displays a message dialog //with the name of the file if (alreadyExistingFiles.size() != 0) { StringBuffer list = new StringBuffer(); for (File file : alreadyExistingFiles) { list.append("
  • " + file.getName() + "
  • "); } String message = String.format(Messages.getMessage("fileAlreadyExistsError"), list); JOptionPane.showMessageDialog(null, message, Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); } //searches for Exception and displays a message dialog //with the name of the file if (unExceptedFilesError.size() != 0) { StringBuffer list = new StringBuffer(); for (File file : unExceptedFilesError) { list.append("
  • " + file.getName() + "
  • "); } String message = String.format(Messages.getMessage("unexpectedSendingError"), list); JOptionPane.showMessageDialog(null, message, Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); } this.dispose(); } else if (arg0.getSource().equals(cancelButton)) { this.dispose(); } } }