source: extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/ui/mainframe/CategoriesTree.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: 8.0 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.Main;
25import fr.mael.jiwigo.om.Category;
26import fr.mael.jiwigo.service.CategoryService;
27import fr.mael.jiwigo.transverse.util.Messages;
28import fr.mael.jiwigo.transverse.util.Tools;
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 * Arbre des catégories
58 */
59public class CategoriesTree extends JPanel implements TreeSelectionListener, MouseListener, ActionListener {
60
61    /**
62     * Logger
63     */
64    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
65            .getLog(CategoriesTree.class);
66    /**
67     *  the tree
68     */
69    private JTree tree;
70    /**
71     * the menu to add a category
72     */
73    private JMenuItem menuAjouter;
74    /**
75     * the menu to refresh the categories
76     */
77    private JMenuItem menuActualiser;
78    /**
79     * the root of the tree which is not displayed
80     */
81    private DefaultMutableTreeNode root = new DefaultMutableTreeNode("");
82    /**
83     * the selected category
84     */
85    private Category selectedCategory;
86
87    /**
88     * Constructor
89     */
90    public CategoriesTree() {
91        super(new BorderLayout());
92        //creation of the tree
93        createNodes(root);
94        tree = new JTree(root);
95        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
96        tree.addTreeSelectionListener(this);
97        tree.setRootVisible(false);
98        tree.addMouseListener(this);
99        JScrollPane treeView = new JScrollPane(tree);
100        Dimension minimumSize = new Dimension(100, 50);
101        treeView.setMinimumSize(minimumSize);
102        add(treeView, BorderLayout.CENTER);
103    }
104
105    /**
106     * Method that allows to update the tree
107     */
108    public void setUpUi() {
109        //begin bug:0001830
110        root.removeAllChildren();
111        createNodes(root);
112        ((DefaultTreeModel) tree.getModel()).reload();
113        //end
114
115    }
116
117    /* (non-Javadoc)
118     * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent)
119     */
120    public void valueChanged(TreeSelectionEvent e) {
121        DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
122        //
123        if (node == null)
124            return;
125        Object catSelectionnee = node.getUserObject();
126        Category category = (Category) catSelectionnee;
127        //MainFrame.imagesPanel.rafraichir(category.getIdentifiant(), false);
128        if (!MainFrame.getInstance().isBrowserVisible()) {
129            MainFrame.getInstance().setBrowserVisible();
130        }
131        MainFrame.getInstance().addTabb(new ThumbnailCategoryPanel(category));
132    }
133
134    /**
135     * creation of the nodes
136     * @param root the root
137     */
138    private void createNodes(DefaultMutableTreeNode root) {
139        DefaultMutableTreeNode category = null;
140        try {
141            List<Category> list = CategoryService.getInstance(Main.sessionManager).makeTree();
142            for (Category cat : list) {
143                if (cat.getParentCategories().size() == 0) {
144                    category = new DefaultMutableTreeNode(cat);
145                    root.add(category);
146                    recursiveNodeCreation(category, cat);
147                }
148
149            }
150        } catch (Exception e) {
151            LOG.error(Tools.getStackTrace(e));
152            JOptionPane.showMessageDialog(null, Messages.getMessage("treeCreationError"), Messages.getMessage("error"),
153                    JOptionPane.ERROR_MESSAGE);
154        }
155
156    }
157
158    /**
159     * recursive method for the creation of the nodes
160     * @param categoryNode
161     * @param category
162     */
163    private void recursiveNodeCreation(DefaultMutableTreeNode categoryNode, Category category) {
164        for (Category cat : category.getChildCategories()) {
165            DefaultMutableTreeNode node = new DefaultMutableTreeNode(cat);
166            categoryNode.add(node);
167            recursiveNodeCreation(node, cat);
168        }
169    }
170
171    @Override
172    public void mouseClicked(MouseEvent arg0) {
173
174    }
175
176    @Override
177    public void mouseEntered(MouseEvent arg0) {
178        // TODO Auto-generated method stub
179
180    }
181
182    @Override
183    public void mouseExited(MouseEvent arg0) {
184        // TODO Auto-generated method stub
185
186    }
187
188    @Override
189    public void mousePressed(MouseEvent e) {
190        int selRow = tree.getRowForLocation(e.getX(), e.getY());
191        TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
192        //begin bug:0001832
193        tree.setSelectionRow(selRow);
194        //end
195
196        if (e.getButton() == 3) {
197            JPopupMenu popup = new JPopupMenu();
198            menuAjouter = new JMenuItem(Messages.getMessage("categories_add"));
199            menuAjouter.addActionListener(this);
200            popup.add(menuAjouter);
201            menuActualiser = new JMenuItem(Messages.getMessage("categories_update"));
202            menuActualiser.addActionListener(this);
203            popup.add(menuActualiser);
204            popup.show(tree, e.getX(), e.getY());
205            if (selRow != -1) {
206                DefaultMutableTreeNode node = (DefaultMutableTreeNode) (selPath.getLastPathComponent());
207                this.selectedCategory = (Category) node.getUserObject();
208            } else {
209                this.selectedCategory = null;
210            }
211        }
212
213    }
214
215    @Override
216    public void mouseReleased(MouseEvent arg0) {
217        // TODO Auto-generated method stub
218
219    }
220
221    @Override
222    public void actionPerformed(ActionEvent arg0) {
223        if (arg0.getSource().equals(menuAjouter)) {
224            String nomcategorie = JOptionPane.showInputDialog(null, Messages.getMessage("categories_enterName"),
225                    Messages.getMessage("categories_add"), JOptionPane.INFORMATION_MESSAGE);
226            //if the name of the category is not empty
227            if (!(nomcategorie == null) && !nomcategorie.equals("")) {
228                //if there is a parent category
229                if (selectedCategory != null) {
230                    //try to create a category
231                    if (CategoryService.getInstance(Main.sessionManager).create(nomcategorie,
232                            selectedCategory.getIdentifier())) {
233                        setUpUi();
234                    } else {
235                        JOptionPane.showMessageDialog(null, Messages.getMessage("categoryCreationError"), Messages
236                                .getMessage("error"), JOptionPane.ERROR_MESSAGE);
237
238                    }
239                } else {
240                    if (CategoryService.getInstance(Main.sessionManager).create(nomcategorie)) {
241                        setUpUi();
242                    } else {
243                        JOptionPane.showMessageDialog(null, Messages.getMessage("categoryCreationError"), Messages
244                                .getMessage("error"), JOptionPane.ERROR_MESSAGE);
245
246                    }
247                }
248            }
249        } else if (arg0.getSource().equals(menuActualiser)) {
250            setUpUi();
251        }
252    }
253}
Note: See TracBrowser for help on using the repository browser.