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

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

Modifications to use the last version of jiwigo-ws-api

File size: 5.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.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;
15
16import fr.mael.jiwigo.Main;
17import fr.mael.jiwigo.om.Category;
18import fr.mael.jiwigo.service.CategoryService;
19import fr.mael.jiwigo.service.ImageService;
20import fr.mael.jiwigo.transverse.ImagesManagement;
21import fr.mael.jiwigo.transverse.enumeration.PreferencesEnum;
22import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement;
23import fr.mael.jiwigo.ui.mainframe.MainFrame;
24
25/**
26 *
27   Copyright (c) 2010, Mael
28   All rights reserved.
29
30   Redistribution and use in source and binary forms, with or without
31   modification, are permitted provided that the following conditions are met:
32    * Redistributions of source code must retain the above copyright
33      notice, this list of conditions and the following disclaimer.
34    * Redistributions in binary form must reproduce the above copyright
35      notice, this list of conditions and the following disclaimer in the
36      documentation and/or other materials provided with the distribution.
37    * Neither the name of jiwigo nor the
38      names of its contributors may be used to endorse or promote products
39      derived from this software without specific prior written permission.
40
41   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
42   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
43   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44   DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY
45   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
46   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51
52 * @author mael
53 * Renderer used to display the category list with a hierarchy
54 *
55 */
56public class DialogChooseCategory extends JDialog implements ActionListener {
57    /**
58     * ArrayList containing all files to send
59     */
60    private ArrayList<File> filesToSend;
61
62    /**
63     * Combobox containing all categories, with a hierarchical display
64     */
65    private JComboBox comboCategories;
66
67    private JButton okButton;
68
69    private JButton cancelButton;
70
71    /**
72     * categories list
73     */
74    private List<Category> categories;
75
76    private JLabel labelCategory;
77
78    public DialogChooseCategory(ArrayList<File> filesToSend) {
79        super(MainFrame.getInstance());
80        this.filesToSend = filesToSend;
81        okButton = new JButton("Ok");
82        cancelButton = new JButton("Annuler");
83        okButton.addActionListener(this);
84        cancelButton.addActionListener(this);
85        labelCategory = new JLabel("Categorie : ");
86        comboCategories = new JComboBox();
87        comboCategories.setRenderer(new ComboCategoryRenderer());
88        try {
89            categories = CategoryService.getInstance(Main.sessionManager).makeTree();
90            createCategoriesTree();
91        } catch (IOException e) {
92            e.printStackTrace();
93        }
94
95        this.setLayout(new GridBagLayout());
96        GridBagConstraints constraints = new GridBagConstraints();
97        constraints.gridx = 0;
98        constraints.gridy = 0;
99        this.add(labelCategory, constraints);
100        constraints.gridx++;
101        this.add(comboCategories, constraints);
102
103        constraints.gridx = 0;
104        constraints.gridy++;
105        this.add(okButton, constraints);
106        constraints.gridx++;
107        this.add(cancelButton, constraints);
108
109        this.pack();
110        this.setLocationRelativeTo(null);
111        this.setVisible(true);
112
113    }
114
115    /**
116     * Method that create the first level of the combobox list
117     * it calls the method that creates the hierarchy
118     */
119    private void createCategoriesTree() {
120        for (Category category : categories) {
121            if (category.getParentCategories().size() == 0) {
122                category.setLevel(0);
123                comboCategories.addItem(category);
124                createRecursiveCategoriesTree(1, category);
125            }
126
127        }
128
129    }
130
131    /**
132     * Method that creates the hierarchical view of the combobox list
133     * @param level
134     * @param category
135     */
136    private void createRecursiveCategoriesTree(int level, Category category) {
137
138        for (Category cat : category.getChildCategories()) {
139            cat.setLevel(level);
140            comboCategories.addItem(cat);
141            int levelTemp = level + 1;
142            createRecursiveCategoriesTree(levelTemp, cat);
143        }
144
145    }
146
147    @Override
148    public void actionPerformed(ActionEvent arg0) {
149        if (arg0.getSource().equals(okButton)) {
150            Category category = (Category) comboCategories.getSelectedItem();
151            for (File file : filesToSend) {
152                try {
153                    Integer widthOriginal = Integer.valueOf(PreferencesManagement
154                            .getValue(PreferencesEnum.WIDTH_ORIGINALE.getLabel()));
155                    Integer heightOriginal = Integer.valueOf(PreferencesManagement
156                            .getValue(PreferencesEnum.HEIGHT_ORIGINAL.getLabel()));
157                    Double chunkSize = Double.valueOf(PreferencesManagement.getValue(PreferencesEnum.CHUNK_SIZE
158                            .getLabel()));
159                    ImageService.getInstance(Main.sessionManager).create(file.getAbsolutePath(),
160                            category.getIdentifier(), widthOriginal, heightOriginal, chunkSize,
161                            ImagesManagement.getInstance().getPrivacyLevel());
162                } catch (Exception e) {
163                    e.printStackTrace();
164                }
165            }
166            this.dispose();
167        } else if (arg0.getSource().equals(cancelButton)) {
168            this.dispose();
169        }
170    }
171}
Note: See TracBrowser for help on using the repository browser.