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

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

Integrates new changes in jiwigo-ws-api
(Exception management)

File size: 7.4 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.Main;
18import fr.mael.jiwigo.om.Category;
19import fr.mael.jiwigo.service.CategoryService;
20import fr.mael.jiwigo.service.ImageService;
21import fr.mael.jiwigo.transverse.ImagesManagement;
22import fr.mael.jiwigo.transverse.enumeration.PreferencesEnum;
23import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException;
24import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
25import fr.mael.jiwigo.transverse.exception.WrongChunkSizeException;
26import fr.mael.jiwigo.transverse.util.Messages;
27import fr.mael.jiwigo.transverse.util.Tools;
28import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement;
29import 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 */
62public 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            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                    ImageService.getInstance(Main.sessionManager).create(file.getAbsolutePath(),
176                            category.getIdentifier(), widthOriginal, heightOriginal, chunkSize,
177                            ImagesManagement.getInstance().getPrivacyLevel());
178                } catch (WrongChunkSizeException ex) {
179                    JOptionPane.showMessageDialog(null, Messages.getMessage("wrongChunkSizeError"), Messages
180                            .getMessage("error"), JOptionPane.ERROR_MESSAGE);
181                    LOG.error(Tools.getStackTrace(ex));
182                    break;
183                } catch (FileAlreadyExistsException e) {
184                    alreadyExistingFiles.add(file);
185                } catch (Exception e) {
186                    unExceptedFilesError.add(file);
187                    LOG.error(Tools.getStackTrace(e));
188                }
189            }
190            if (alreadyExistingFiles.size() != 0) {
191                StringBuffer list = new StringBuffer();
192                for (File file : alreadyExistingFiles) {
193                    list.append("<li>" + file.getName() + "</li>");
194                }
195                String message = String.format(Messages.getMessage("fileAlreadyExistsError"), list);
196                JOptionPane.showMessageDialog(null, message, Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
197
198            }
199
200            if (unExceptedFilesError.size() != 0) {
201                StringBuffer list = new StringBuffer();
202                for (File file : unExceptedFilesError) {
203                    list.append("<li>" + file.getName() + "</li>");
204                }
205                String message = String.format(Messages.getMessage("unexpectedSendingError"), list);
206                JOptionPane.showMessageDialog(null, message, Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
207
208            }
209            this.dispose();
210        } else if (arg0.getSource().equals(cancelButton)) {
211            this.dispose();
212        }
213    }
214}
Note: See TracBrowser for help on using the repository browser.