source: extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/ui/search/DialogChooseCategory.java @ 10811

Last change on this file since 10811 was 10811, checked in by mlg, 13 years ago

Adds a wait cursor for time consuming operations.

File size: 8.5 KB
Line 
1package fr.mael.jiwigo.ui.search;
2
3import java.awt.GridBagConstraints;
4import java.awt.GridBagLayout;
5import java.awt.event.ActionEvent;
6import java.awt.event.ActionListener;
7import java.util.ArrayList;
8import java.util.List;
9
10import javax.swing.JButton;
11import javax.swing.JComboBox;
12import javax.swing.JDialog;
13import javax.swing.JLabel;
14import javax.swing.JOptionPane;
15
16import fr.mael.jiwigo.om.Category;
17import fr.mael.jiwigo.transverse.ImagesManagement;
18import fr.mael.jiwigo.transverse.enumeration.PreferencesEnum;
19import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException;
20import fr.mael.jiwigo.transverse.exception.JiwigoException;
21import fr.mael.jiwigo.transverse.exception.WrongChunkSizeException;
22import fr.mael.jiwigo.transverse.util.Messages;
23import fr.mael.jiwigo.transverse.util.Tools;
24import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement;
25import fr.mael.jiwigo.transverse.util.spring.SpringUtils;
26import fr.mael.jiwigo.ui.mainframe.CursorSetter;
27import fr.mael.jiwigo.ui.mainframe.MainFrame;
28
29/**
30 *
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.
55
56 * @author mael
57 * Renderer used to display the category list with a hierarchy
58 *
59 */
60public 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
82    /**
83     * Logger
84     */
85    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
86            .getLog(DialogChooseCategory.class);
87
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 {
99            categories = SpringUtils.getCategoryService().makeTree();
100            createCategoriesTree();
101        } catch (JiwigoException e) {
102            e.printStackTrace();
103        }
104
105        this.setLayout(new GridBagLayout());
106        GridBagConstraints constraints = new GridBagConstraints();
107        constraints.gridx = 0;
108        constraints.gridy = 0;
109        this.add(labelCategory, constraints);
110        constraints.gridx++;
111        this.add(comboCategories, constraints);
112
113        constraints.gridx = 0;
114        constraints.gridy++;
115        this.add(okButton, constraints);
116        constraints.gridx++;
117        this.add(cancelButton, constraints);
118
119        this.pack();
120        this.setLocationRelativeTo(null);
121        this.setVisible(true);
122
123    }
124
125    /**
126     * Method that create the first level of the combobox list
127     * it calls the method that creates the hierarchy
128     */
129    private void createCategoriesTree() {
130        for (Category category : categories) {
131            if (category.getParentCategories().size() == 0) {
132                category.setLevel(0);
133                comboCategories.addItem(category);
134                createRecursiveCategoriesTree(1, category);
135            }
136
137        }
138
139    }
140
141    /**
142     * Method that creates the hierarchical view of the combobox list
143     * @param level
144     * @param category
145     */
146    private void createRecursiveCategoriesTree(int level, Category category) {
147
148        for (Category cat : category.getChildCategories()) {
149            cat.setLevel(level);
150            comboCategories.addItem(cat);
151            int levelTemp = level + 1;
152            createRecursiveCategoriesTree(levelTemp, cat);
153        }
154
155    }
156
157    @Override
158    public void actionPerformed(ActionEvent arg0) {
159        CursorSetter.setWaitCursor(this);
160        MainFrame.getInstance().showWaitCursor(true);
161        if (arg0.getSource().equals(okButton)) {
162            Category category = (Category) comboCategories.getSelectedItem();
163            List<File> alreadyExistingFiles = new ArrayList<File>();
164            List<File> unExceptedFilesError = new ArrayList<File>();
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
170            for (File file : filesToSend) {
171                if (PreferencesManagement.getValue(PreferencesEnum.SENDING_METHOD.getLabel()).equals("0")) {
172                    try {
173                        SpringUtils.getImageService().addSimple(file, category.getIdentifier(), file.getName(),
174                                ImagesManagement.getInstance().getPrivacyLevel());
175                    } catch (JiwigoException e) {
176                        LOG.error(Tools.getStackTrace(e));
177                        //displays a dialog if there is an error
178                        JOptionPane.showMessageDialog(null, Messages.getMessage("sendingError") + file.getName(),
179                                Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
180                        MainFrame.getInstance().setMessage(Messages.getMessage("sendingError") + " " + file.getName());
181                    }
182                } else {
183                    try {
184                        Integer widthOriginal = Integer.valueOf(PreferencesManagement
185                                .getValue(PreferencesEnum.WIDTH_ORIGINALE.getLabel()));
186                        Integer heightOriginal = Integer.valueOf(PreferencesManagement
187                                .getValue(PreferencesEnum.HEIGHT_ORIGINAL.getLabel()));
188                        Double chunkSize = Double.valueOf(PreferencesManagement.getValue(PreferencesEnum.CHUNK_SIZE
189                                .getLabel()));
190                        SpringUtils.getImageService().create(file.getAbsolutePath(), category.getIdentifier(),
191                                widthOriginal, heightOriginal, chunkSize,
192                                ImagesManagement.getInstance().getPrivacyLevel());
193                    } catch (WrongChunkSizeException ex) {
194                        JOptionPane.showMessageDialog(null, Messages.getMessage("wrongChunkSizeError"),
195                                Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
196                        LOG.error(Tools.getStackTrace(ex));
197                        break;
198                    } catch (FileAlreadyExistsException e) {
199                        alreadyExistingFiles.add(file);
200                    } catch (Exception e) {
201                        unExceptedFilesError.add(file);
202                        LOG.error(Tools.getStackTrace(e));
203                    }
204                }
205            }
206            //searches for FileAlreadyExistsException and displays a message dialog
207            //with the name of the file
208            if (alreadyExistingFiles.size() != 0) {
209                StringBuffer list = new StringBuffer();
210                for (File file : alreadyExistingFiles) {
211                    list.append("<li>" + file.getName() + "</li>");
212                }
213                String message = String.format(Messages.getMessage("fileAlreadyExistsError"), list);
214                JOptionPane.showMessageDialog(null, message, Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
215
216            }
217
218            //searches for Exception and displays a message dialog
219            //with the name of the file
220            if (unExceptedFilesError.size() != 0) {
221                StringBuffer list = new StringBuffer();
222                for (File file : unExceptedFilesError) {
223                    list.append("<li>" + file.getName() + "</li>");
224                }
225                String message = String.format(Messages.getMessage("unexpectedSendingError"), list);
226                JOptionPane.showMessageDialog(null, message, Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
227
228            }
229            this.dispose();
230        } else if (arg0.getSource().equals(cancelButton)) {
231            this.dispose();
232        }
233        MainFrame.getInstance().showWaitCursor(false);
234        CursorSetter.setDefaultCursor(this);
235    }
236}
Note: See TracBrowser for help on using the repository browser.