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