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