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

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

Modifications to use the last version of jiwigo-ws-api which manages proxy errors.

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