source: extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/ui/mainframe/CategoriesTree.java @ 6962

Last change on this file since 6962 was 6962, checked in by mlg, 14 years ago

"Feature" :
Until now, it was not possible to create a "root" category. It is now possible.

File size: 7.9 KB
Line 
1package fr.mael.jiwigo.ui.mainframe;
2
3import java.awt.BorderLayout;
4import java.awt.Dimension;
5import java.awt.event.ActionEvent;
6import java.awt.event.ActionListener;
7import java.awt.event.MouseEvent;
8import java.awt.event.MouseListener;
9import java.util.List;
10
11import javax.swing.JMenuItem;
12import javax.swing.JOptionPane;
13import javax.swing.JPanel;
14import javax.swing.JPopupMenu;
15import javax.swing.JScrollPane;
16import javax.swing.JTree;
17import javax.swing.event.TreeSelectionEvent;
18import javax.swing.event.TreeSelectionListener;
19import javax.swing.tree.DefaultMutableTreeNode;
20import javax.swing.tree.DefaultTreeModel;
21import javax.swing.tree.TreePath;
22import javax.swing.tree.TreeSelectionModel;
23
24import fr.mael.jiwigo.om.Category;
25import fr.mael.jiwigo.service.CategoryService;
26import fr.mael.jiwigo.transverse.util.Messages;
27import fr.mael.jiwigo.transverse.util.Outil;
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 * Arbre des catégories
57 */
58public class CategoriesTree extends JPanel implements TreeSelectionListener, MouseListener, ActionListener {
59
60    /**
61     * Logger
62     */
63    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
64            .getLog(CategoriesTree.class);
65    /**
66     *  L'arbre
67     */
68    private JTree tree;
69    /**
70     * le menu permettant d'ajouter une catégorie
71     */
72    private JMenuItem menuAjouter;
73    /**
74     * Le menu permettant d'actualiser les catégories
75     */
76    private JMenuItem menuActualiser;
77    /**
78     * la racine du tree, qu'on n'affiche pas
79     */
80    private DefaultMutableTreeNode root = new DefaultMutableTreeNode("");
81    /**
82     * La catégorie selectionnée
83     */
84    private Category selectedCategory;
85
86    /**
87     * Constructeur
88     */
89    public CategoriesTree() {
90        super(new BorderLayout());
91        //creation de l'arbre
92        createNodes(root);
93        tree = new JTree(root);
94        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
95        tree.addTreeSelectionListener(this);
96        tree.setRootVisible(false);
97        tree.addMouseListener(this);
98        JScrollPane treeView = new JScrollPane(tree);
99        Dimension minimumSize = new Dimension(100, 50);
100        treeView.setMinimumSize(minimumSize);
101        add(treeView, BorderLayout.CENTER);
102    }
103
104    /**
105     * Methode qui permet de mettre à jour l'arbre
106     */
107    public void setUpUi() {
108        //begin bug:0001830
109        root.removeAllChildren();
110        createNodes(root);
111        ((DefaultTreeModel) tree.getModel()).reload();
112        //end
113
114    }
115
116    /* (non-Javadoc)
117     * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent)
118     */
119    public void valueChanged(TreeSelectionEvent e) {
120        DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
121        //
122        if (node == null)
123            return;
124        Object catSelectionnee = node.getUserObject();
125        Category category = (Category) catSelectionnee;
126        //MainFrame.imagesPanel.rafraichir(category.getIdentifiant(), false);
127        MainFrame.getInstance().addTabb(new ThumbnailCategoryPanel(category));
128    }
129
130    /**
131     * Creation des noeuds
132     * @param root noeud racine
133     */
134    private void createNodes(DefaultMutableTreeNode root) {
135        DefaultMutableTreeNode category = null;
136        try {
137            List<Category> list = CategoryService.getInstance().construireArbre();
138            for (Category cat : list) {
139                if (cat.getCategoriesMeres().size() == 0) {
140                    category = new DefaultMutableTreeNode(cat);
141                    root.add(category);
142                    recursiveNodeCreation(category, cat);
143                }
144
145            }
146        } catch (Exception e) {
147            LOG.error(Outil.getStackTrace(e));
148            JOptionPane.showMessageDialog(null, Messages.getMessage("treeCreationError"), Messages.getMessage("error"),
149                    JOptionPane.ERROR_MESSAGE);
150        }
151
152    }
153
154    /**
155     * Méthode récursive de creation des noeuds de l'arbre
156     * @param categoryNode
157     * @param category
158     */
159    private void recursiveNodeCreation(DefaultMutableTreeNode categoryNode, Category category) {
160        for (Category cat : category.getCategoriesFilles()) {
161            DefaultMutableTreeNode node = new DefaultMutableTreeNode(cat);
162            categoryNode.add(node);
163            recursiveNodeCreation(node, cat);
164        }
165    }
166
167    @Override
168    public void mouseClicked(MouseEvent arg0) {
169
170    }
171
172    @Override
173    public void mouseEntered(MouseEvent arg0) {
174        // TODO Auto-generated method stub
175
176    }
177
178    @Override
179    public void mouseExited(MouseEvent arg0) {
180        // TODO Auto-generated method stub
181
182    }
183
184    @Override
185    public void mousePressed(MouseEvent e) {
186        int selRow = tree.getRowForLocation(e.getX(), e.getY());
187        TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
188        //begin bug:0001832
189        tree.setSelectionRow(selRow);
190        //end
191
192        if (e.getButton() == 3) {
193            JPopupMenu popup = new JPopupMenu();
194            menuAjouter = new JMenuItem(Messages.getMessage("categories_add"));
195            menuAjouter.addActionListener(this);
196            popup.add(menuAjouter);
197            menuActualiser = new JMenuItem(Messages.getMessage("categories_update"));
198            menuActualiser.addActionListener(this);
199            popup.add(menuActualiser);
200            popup.show(tree, e.getX(), e.getY());
201            if (selRow != -1) {
202                DefaultMutableTreeNode node = (DefaultMutableTreeNode) (selPath.getLastPathComponent());
203                this.selectedCategory = (Category) node.getUserObject();
204            } else {
205                this.selectedCategory = null;
206            }
207        }
208
209    }
210
211    @Override
212    public void mouseReleased(MouseEvent arg0) {
213        // TODO Auto-generated method stub
214
215    }
216
217    @Override
218    public void actionPerformed(ActionEvent arg0) {
219        if (arg0.getSource().equals(menuAjouter)) {
220            String nomcategorie = JOptionPane.showInputDialog(null, Messages.getMessage("categories_enterName"),
221                    Messages.getMessage("categories_add"), JOptionPane.INFORMATION_MESSAGE);
222            //if the name of the category is not empty
223            if (!(nomcategorie == null) && !nomcategorie.equals("")) {
224                //if there is a parent category
225                if (selectedCategory != null) {
226                    //try to create a category
227                    if (CategoryService.getInstance().creer(nomcategorie, selectedCategory.getIdentifiant())) {
228                        setUpUi();
229                    } else {
230                        JOptionPane.showMessageDialog(null, Messages.getMessage("categoryCreationError"), Messages
231                                .getMessage("error"), JOptionPane.ERROR_MESSAGE);
232
233                    }
234                } else {
235                    if (CategoryService.getInstance().creer(nomcategorie)) {
236                        setUpUi();
237                    } else {
238                        JOptionPane.showMessageDialog(null, Messages.getMessage("categoryCreationError"), Messages
239                                .getMessage("error"), JOptionPane.ERROR_MESSAGE);
240
241                    }
242                }
243            }
244        } else if (arg0.getSource().equals(menuActualiser)) {
245            setUpUi();
246        }
247    }
248}
Note: See TracBrowser for help on using the repository browser.