[8838] | 1 | package fr.mael.jiwigo.ui.search; |
---|
| 2 | |
---|
| 3 | import java.awt.GridBagConstraints; |
---|
| 4 | import java.awt.GridBagLayout; |
---|
| 5 | import java.awt.event.ActionEvent; |
---|
| 6 | import java.awt.event.ActionListener; |
---|
| 7 | import java.io.IOException; |
---|
| 8 | import java.util.ArrayList; |
---|
| 9 | import java.util.List; |
---|
| 10 | |
---|
| 11 | import javax.swing.JButton; |
---|
| 12 | import javax.swing.JComboBox; |
---|
| 13 | import javax.swing.JDialog; |
---|
| 14 | import javax.swing.JLabel; |
---|
[9894] | 15 | import javax.swing.JOptionPane; |
---|
[8838] | 16 | |
---|
| 17 | import fr.mael.jiwigo.om.Category; |
---|
[9385] | 18 | import fr.mael.jiwigo.transverse.ImagesManagement; |
---|
| 19 | import fr.mael.jiwigo.transverse.enumeration.PreferencesEnum; |
---|
[9894] | 20 | import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException; |
---|
[9431] | 21 | import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; |
---|
[9894] | 22 | import fr.mael.jiwigo.transverse.exception.WrongChunkSizeException; |
---|
| 23 | import fr.mael.jiwigo.transverse.util.Messages; |
---|
[9431] | 24 | import fr.mael.jiwigo.transverse.util.Tools; |
---|
[9385] | 25 | import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement; |
---|
[9921] | 26 | import fr.mael.jiwigo.transverse.util.spring.SpringUtils; |
---|
[8838] | 27 | import fr.mael.jiwigo.ui.mainframe.MainFrame; |
---|
| 28 | |
---|
| 29 | /** |
---|
[9385] | 30 | * |
---|
[8838] | 31 | Copyright (c) 2010, Mael |
---|
| 32 | All rights reserved. |
---|
| 33 | |
---|
| 34 | Redistribution and use in source and binary forms, with or without |
---|
| 35 | modification, are permitted provided that the following conditions are met: |
---|
| 36 | * Redistributions of source code must retain the above copyright |
---|
| 37 | notice, this list of conditions and the following disclaimer. |
---|
| 38 | * Redistributions in binary form must reproduce the above copyright |
---|
| 39 | notice, this list of conditions and the following disclaimer in the |
---|
| 40 | documentation and/or other materials provided with the distribution. |
---|
| 41 | * Neither the name of jiwigo nor the |
---|
| 42 | names of its contributors may be used to endorse or promote products |
---|
| 43 | derived from this software without specific prior written permission. |
---|
| 44 | |
---|
| 45 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
| 46 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
| 47 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
| 48 | DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY |
---|
| 49 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
| 50 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
| 51 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
| 52 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
| 53 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
| 54 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
[9385] | 55 | |
---|
[8838] | 56 | * @author mael |
---|
[9385] | 57 | * Renderer used to display the category list with a hierarchy |
---|
[8838] | 58 | * |
---|
| 59 | */ |
---|
| 60 | public class DialogChooseCategory extends JDialog implements ActionListener { |
---|
| 61 | /** |
---|
| 62 | * ArrayList containing all files to send |
---|
| 63 | */ |
---|
| 64 | private ArrayList<File> filesToSend; |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | * Combobox containing all categories, with a hierarchical display |
---|
| 68 | */ |
---|
| 69 | private JComboBox comboCategories; |
---|
| 70 | |
---|
| 71 | private JButton okButton; |
---|
| 72 | |
---|
| 73 | private JButton cancelButton; |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | * categories list |
---|
| 77 | */ |
---|
| 78 | private List<Category> categories; |
---|
| 79 | |
---|
| 80 | private JLabel labelCategory; |
---|
| 81 | |
---|
[9431] | 82 | /** |
---|
| 83 | * Logger |
---|
| 84 | */ |
---|
| 85 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory |
---|
| 86 | .getLog(DialogChooseCategory.class); |
---|
| 87 | |
---|
[8838] | 88 | public DialogChooseCategory(ArrayList<File> filesToSend) { |
---|
| 89 | super(MainFrame.getInstance()); |
---|
| 90 | this.filesToSend = filesToSend; |
---|
| 91 | okButton = new JButton("Ok"); |
---|
| 92 | cancelButton = new JButton("Annuler"); |
---|
| 93 | okButton.addActionListener(this); |
---|
| 94 | cancelButton.addActionListener(this); |
---|
| 95 | labelCategory = new JLabel("Categorie : "); |
---|
| 96 | comboCategories = new JComboBox(); |
---|
| 97 | comboCategories.setRenderer(new ComboCategoryRenderer()); |
---|
| 98 | try { |
---|
[9921] | 99 | categories = SpringUtils.getCategoryService().makeTree(); |
---|
[8838] | 100 | createCategoriesTree(); |
---|
| 101 | } catch (IOException e) { |
---|
| 102 | e.printStackTrace(); |
---|
[9431] | 103 | } catch (ProxyAuthenticationException e) { |
---|
| 104 | LOG.error(Tools.getStackTrace(e)); |
---|
[8838] | 105 | } |
---|
| 106 | |
---|
| 107 | this.setLayout(new GridBagLayout()); |
---|
| 108 | GridBagConstraints constraints = new GridBagConstraints(); |
---|
| 109 | constraints.gridx = 0; |
---|
| 110 | constraints.gridy = 0; |
---|
| 111 | this.add(labelCategory, constraints); |
---|
| 112 | constraints.gridx++; |
---|
| 113 | this.add(comboCategories, constraints); |
---|
| 114 | |
---|
| 115 | constraints.gridx = 0; |
---|
| 116 | constraints.gridy++; |
---|
| 117 | this.add(okButton, constraints); |
---|
| 118 | constraints.gridx++; |
---|
| 119 | this.add(cancelButton, constraints); |
---|
| 120 | |
---|
| 121 | this.pack(); |
---|
| 122 | this.setLocationRelativeTo(null); |
---|
| 123 | this.setVisible(true); |
---|
| 124 | |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | * Method that create the first level of the combobox list |
---|
| 129 | * it calls the method that creates the hierarchy |
---|
| 130 | */ |
---|
| 131 | private void createCategoriesTree() { |
---|
| 132 | for (Category category : categories) { |
---|
[9393] | 133 | if (category.getParentCategories().size() == 0) { |
---|
[8838] | 134 | category.setLevel(0); |
---|
| 135 | comboCategories.addItem(category); |
---|
| 136 | createRecursiveCategoriesTree(1, category); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | /** |
---|
| 144 | * Method that creates the hierarchical view of the combobox list |
---|
| 145 | * @param level |
---|
| 146 | * @param category |
---|
| 147 | */ |
---|
| 148 | private void createRecursiveCategoriesTree(int level, Category category) { |
---|
| 149 | |
---|
[9393] | 150 | for (Category cat : category.getChildCategories()) { |
---|
[8838] | 151 | cat.setLevel(level); |
---|
| 152 | comboCategories.addItem(cat); |
---|
| 153 | int levelTemp = level + 1; |
---|
| 154 | createRecursiveCategoriesTree(levelTemp, cat); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | @Override |
---|
| 160 | public void actionPerformed(ActionEvent arg0) { |
---|
| 161 | if (arg0.getSource().equals(okButton)) { |
---|
| 162 | Category category = (Category) comboCategories.getSelectedItem(); |
---|
[9894] | 163 | List<File> alreadyExistingFiles = new ArrayList<File>(); |
---|
| 164 | List<File> unExceptedFilesError = new ArrayList<File>(); |
---|
[9896] | 165 | //loop on all files to send. |
---|
| 166 | //if there is a WrongChunkSizeException, the loop stops |
---|
| 167 | //if there is a FileAlreadyExistsException or an Exception |
---|
| 168 | //the file is just added to a list to display the error messages |
---|
| 169 | //at the end of the loop |
---|
[8838] | 170 | for (File file : filesToSend) { |
---|
| 171 | try { |
---|
[9385] | 172 | Integer widthOriginal = Integer.valueOf(PreferencesManagement |
---|
| 173 | .getValue(PreferencesEnum.WIDTH_ORIGINALE.getLabel())); |
---|
| 174 | Integer heightOriginal = Integer.valueOf(PreferencesManagement |
---|
| 175 | .getValue(PreferencesEnum.HEIGHT_ORIGINAL.getLabel())); |
---|
| 176 | Double chunkSize = Double.valueOf(PreferencesManagement.getValue(PreferencesEnum.CHUNK_SIZE |
---|
| 177 | .getLabel())); |
---|
[9921] | 178 | SpringUtils.getImageService().create(file.getAbsolutePath(), category.getIdentifier(), |
---|
| 179 | widthOriginal, heightOriginal, chunkSize, ImagesManagement.getInstance().getPrivacyLevel()); |
---|
[9894] | 180 | } catch (WrongChunkSizeException ex) { |
---|
| 181 | JOptionPane.showMessageDialog(null, Messages.getMessage("wrongChunkSizeError"), Messages |
---|
| 182 | .getMessage("error"), JOptionPane.ERROR_MESSAGE); |
---|
| 183 | LOG.error(Tools.getStackTrace(ex)); |
---|
| 184 | break; |
---|
| 185 | } catch (FileAlreadyExistsException e) { |
---|
| 186 | alreadyExistingFiles.add(file); |
---|
[8838] | 187 | } catch (Exception e) { |
---|
[9894] | 188 | unExceptedFilesError.add(file); |
---|
| 189 | LOG.error(Tools.getStackTrace(e)); |
---|
[8838] | 190 | } |
---|
| 191 | } |
---|
[9896] | 192 | //searches for FileAlreadyExistsException and displays a message dialog |
---|
| 193 | //with the name of the file |
---|
[9894] | 194 | if (alreadyExistingFiles.size() != 0) { |
---|
| 195 | StringBuffer list = new StringBuffer(); |
---|
| 196 | for (File file : alreadyExistingFiles) { |
---|
| 197 | list.append("<li>" + file.getName() + "</li>"); |
---|
| 198 | } |
---|
| 199 | String message = String.format(Messages.getMessage("fileAlreadyExistsError"), list); |
---|
| 200 | JOptionPane.showMessageDialog(null, message, Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); |
---|
| 201 | |
---|
| 202 | } |
---|
| 203 | |
---|
[9896] | 204 | //searches for Exception and displays a message dialog |
---|
| 205 | //with the name of the file |
---|
[9894] | 206 | if (unExceptedFilesError.size() != 0) { |
---|
| 207 | StringBuffer list = new StringBuffer(); |
---|
| 208 | for (File file : unExceptedFilesError) { |
---|
| 209 | list.append("<li>" + file.getName() + "</li>"); |
---|
| 210 | } |
---|
| 211 | String message = String.format(Messages.getMessage("unexpectedSendingError"), list); |
---|
| 212 | JOptionPane.showMessageDialog(null, message, Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); |
---|
| 213 | |
---|
| 214 | } |
---|
[8838] | 215 | this.dispose(); |
---|
| 216 | } else if (arg0.getSource().equals(cancelButton)) { |
---|
| 217 | this.dispose(); |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | } |
---|