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

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

Adds spring for the dependency injection
Deletion of jdom (not useful just for a class that manipulates a simple XML file)

File size: 7.8 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.io.IOException;
8import java.util.ArrayList;
9import java.util.List;
10
11import javax.swing.JButton;
12import javax.swing.JComboBox;
13import javax.swing.JDialog;
14import javax.swing.JLabel;
15import javax.swing.JOptionPane;
16
17import fr.mael.jiwigo.om.Category;
18import fr.mael.jiwigo.transverse.ImagesManagement;
19import fr.mael.jiwigo.transverse.enumeration.PreferencesEnum;
20import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException;
21import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
22import fr.mael.jiwigo.transverse.exception.WrongChunkSizeException;
23import fr.mael.jiwigo.transverse.util.Messages;
24import fr.mael.jiwigo.transverse.util.Tools;
25import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement;
26import fr.mael.jiwigo.transverse.util.spring.SpringUtils;
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 (IOException e) {
102            e.printStackTrace();
103        } catch (ProxyAuthenticationException e) {
104            LOG.error(Tools.getStackTrace(e));
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) {
133            if (category.getParentCategories().size() == 0) {
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
150        for (Category cat : category.getChildCategories()) {
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();
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                try {
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()));
178                    SpringUtils.getImageService().create(file.getAbsolutePath(), category.getIdentifier(),
179                            widthOriginal, heightOriginal, chunkSize, ImagesManagement.getInstance().getPrivacyLevel());
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);
187                } catch (Exception e) {
188                    unExceptedFilesError.add(file);
189                    LOG.error(Tools.getStackTrace(e));
190                }
191            }
192            //searches for FileAlreadyExistsException and displays a message dialog
193            //with the name of the file
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
204            //searches for Exception and displays a message dialog
205            //with the name of the file
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            }
215            this.dispose();
216        } else if (arg0.getSource().equals(cancelButton)) {
217            this.dispose();
218        }
219    }
220}
Note: See TracBrowser for help on using the repository browser.