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

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

Integrates last jiwigo-ws-api modifications

File size: 7.6 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.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 */
59public 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                try {
169                    Integer widthOriginal = Integer.valueOf(PreferencesManagement
170                            .getValue(PreferencesEnum.WIDTH_ORIGINALE.getLabel()));
171                    Integer heightOriginal = Integer.valueOf(PreferencesManagement
172                            .getValue(PreferencesEnum.HEIGHT_ORIGINAL.getLabel()));
173                    Double chunkSize = Double.valueOf(PreferencesManagement.getValue(PreferencesEnum.CHUNK_SIZE
174                            .getLabel()));
175                    SpringUtils.getImageService().create(file.getAbsolutePath(), category.getIdentifier(),
176                            widthOriginal, heightOriginal, chunkSize, ImagesManagement.getInstance().getPrivacyLevel());
177                } catch (WrongChunkSizeException ex) {
178                    JOptionPane.showMessageDialog(null, Messages.getMessage("wrongChunkSizeError"),
179                            Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
180                    LOG.error(Tools.getStackTrace(ex));
181                    break;
182                } catch (FileAlreadyExistsException e) {
183                    alreadyExistingFiles.add(file);
184                } catch (Exception e) {
185                    unExceptedFilesError.add(file);
186                    LOG.error(Tools.getStackTrace(e));
187                }
188            }
189            //searches for FileAlreadyExistsException and displays a message dialog
190            //with the name of the file
191            if (alreadyExistingFiles.size() != 0) {
192                StringBuffer list = new StringBuffer();
193                for (File file : alreadyExistingFiles) {
194                    list.append("<li>" + file.getName() + "</li>");
195                }
196                String message = String.format(Messages.getMessage("fileAlreadyExistsError"), list);
197                JOptionPane.showMessageDialog(null, message, Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
198
199            }
200
201            //searches for Exception and displays a message dialog
202            //with the name of the file
203            if (unExceptedFilesError.size() != 0) {
204                StringBuffer list = new StringBuffer();
205                for (File file : unExceptedFilesError) {
206                    list.append("<li>" + file.getName() + "</li>");
207                }
208                String message = String.format(Messages.getMessage("unexpectedSendingError"), list);
209                JOptionPane.showMessageDialog(null, message, Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
210
211            }
212            this.dispose();
213        } else if (arg0.getSource().equals(cancelButton)) {
214            this.dispose();
215        }
216    }
217}
Note: See TracBrowser for help on using the repository browser.