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

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

Adds confirmation messages for images and categories deletion.

File size: 9.4 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.om.Category;
26import fr.mael.jiwigo.transverse.exception.JiwigoException;
27import fr.mael.jiwigo.transverse.util.Messages;
28import fr.mael.jiwigo.transverse.util.Tools;
29import fr.mael.jiwigo.transverse.util.spring.SpringUtils;
30
31/**
32   Copyright (c) 2010, Mael
33   All rights reserved.
34
35   Redistribution and use in source and binary forms, with or without
36   modification, are permitted provided that the following conditions are met:
37    * Redistributions of source code must retain the above copyright
38      notice, this list of conditions and the following disclaimer.
39    * Redistributions in binary form must reproduce the above copyright
40      notice, this list of conditions and the following disclaimer in the
41      documentation and/or other materials provided with the distribution.
42    * Neither the name of jiwigo nor the
43      names of its contributors may be used to endorse or promote products
44      derived from this software without specific prior written permission.
45
46   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
47   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
48   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
49   DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY
50   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
51   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
52   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
54   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
55   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56
57 * @author mael
58 * Arbre des catégories
59 */
60public class CategoriesTree extends JPanel implements TreeSelectionListener, MouseListener, ActionListener {
61
62    /**
63     * Logger
64     */
65    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
66            .getLog(CategoriesTree.class);
67    /**
68     *  the tree
69     */
70    private JTree tree;
71    /**
72     * the menu to add a category
73     */
74    private JMenuItem menuAdd;
75    /**
76     * the menu to refresh the categories
77     */
78    private JMenuItem menuRefresh;
79    /**
80     * the root of the tree which is not displayed
81     */
82    private DefaultMutableTreeNode root = new DefaultMutableTreeNode("");
83    /**
84     * the selected category
85     */
86    private Category selectedCategory;
87    /**
88     * the menu to add a category
89     */
90    private JMenuItem menuDelete;
91
92    /**
93     * Constructor
94     */
95    public CategoriesTree() {
96        super(new BorderLayout());
97        //creation of the tree
98        createNodes(root);
99        tree = new JTree(root);
100        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
101        tree.addTreeSelectionListener(this);
102        tree.setRootVisible(false);
103        tree.addMouseListener(this);
104        JScrollPane treeView = new JScrollPane(tree);
105        Dimension minimumSize = new Dimension(100, 50);
106        treeView.setMinimumSize(minimumSize);
107        add(treeView, BorderLayout.CENTER);
108    }
109
110    /**
111     * Method that allows to update the tree
112     */
113    public void setUpUi() {
114        //begin bug:0001830
115        root.removeAllChildren();
116        createNodes(root);
117        ((DefaultTreeModel) tree.getModel()).reload();
118        //end
119
120    }
121
122    /* (non-Javadoc)
123     * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent)
124     */
125    public void valueChanged(TreeSelectionEvent e) {
126        DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
127        //
128        if (node == null)
129            return;
130        Object catSelectionnee = node.getUserObject();
131        Category category = (Category) catSelectionnee;
132        //MainFrame.imagesPanel.rafraichir(category.getIdentifiant(), false);
133        if (!MainFrame.getInstance().isBrowserVisible()) {
134            MainFrame.getInstance().setBrowserVisible();
135        }
136        MainFrame.getInstance().addTabb(new ThumbnailCategoryPanel(category));
137    }
138
139    /**
140     * creation of the nodes
141     * @param root the root
142     */
143    private void createNodes(DefaultMutableTreeNode root) {
144        DefaultMutableTreeNode category = null;
145        try {
146            List<Category> list = SpringUtils.getCategoryService().makeTree();
147            for (Category cat : list) {
148                if (cat.getParentCategories().size() == 0) {
149                    category = new DefaultMutableTreeNode(cat);
150                    root.add(category);
151                    recursiveNodeCreation(category, cat);
152                }
153
154            }
155        } catch (Exception e) {
156            LOG.error(Tools.getStackTrace(e));
157            JOptionPane.showMessageDialog(null, Messages.getMessage("treeCreationError"), Messages.getMessage("error"),
158                    JOptionPane.ERROR_MESSAGE);
159        }
160
161    }
162
163    /**
164     * recursive method for the creation of the nodes
165     * @param categoryNode
166     * @param category
167     */
168    private void recursiveNodeCreation(DefaultMutableTreeNode categoryNode, Category category) {
169        for (Category cat : category.getChildCategories()) {
170            DefaultMutableTreeNode node = new DefaultMutableTreeNode(cat);
171            categoryNode.add(node);
172            recursiveNodeCreation(node, cat);
173        }
174    }
175
176    @Override
177    public void mouseClicked(MouseEvent arg0) {
178
179    }
180
181    @Override
182    public void mouseEntered(MouseEvent arg0) {
183        // TODO Auto-generated method stub
184
185    }
186
187    @Override
188    public void mouseExited(MouseEvent arg0) {
189        // TODO Auto-generated method stub
190
191    }
192
193    @Override
194    public void mousePressed(MouseEvent e) {
195        int selRow = tree.getRowForLocation(e.getX(), e.getY());
196        TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
197        //begin bug:0001832
198        tree.setSelectionRow(selRow);
199        //end
200
201        if (e.getButton() == 3) {
202            JPopupMenu popup = new JPopupMenu();
203            menuAdd = new JMenuItem(Messages.getMessage("categories_add"));
204            menuAdd.addActionListener(this);
205            popup.add(menuAdd);
206            menuRefresh = new JMenuItem(Messages.getMessage("categories_update"));
207            menuRefresh.addActionListener(this);
208            popup.add(menuRefresh);
209            menuDelete = new JMenuItem(Messages.getMessage("categories_delete"));
210            menuDelete.addActionListener(this);
211            popup.add(menuDelete);
212            popup.show(tree, e.getX(), e.getY());
213            if (selRow != -1) {
214                DefaultMutableTreeNode node = (DefaultMutableTreeNode) (selPath.getLastPathComponent());
215                this.selectedCategory = (Category) node.getUserObject();
216            } else {
217                this.selectedCategory = null;
218            }
219        }
220
221    }
222
223    @Override
224    public void mouseReleased(MouseEvent arg0) {
225        // TODO Auto-generated method stub
226
227    }
228
229    @Override
230    public void actionPerformed(ActionEvent arg0) {
231        if (arg0.getSource().equals(menuAdd)) {
232            String nomcategorie = JOptionPane.showInputDialog(null, Messages.getMessage("categories_enterName"),
233                    Messages.getMessage("categories_add"), JOptionPane.INFORMATION_MESSAGE);
234            //if the name of the category is not empty
235            if (!(nomcategorie == null) && !nomcategorie.equals("")) {
236                //if there is a parent category
237                if (selectedCategory != null) {
238                    //try to create a category
239                    try {
240                        if (SpringUtils.getCategoryService().create(nomcategorie, selectedCategory.getIdentifier())) {
241                            setUpUi();
242                        } else {
243                            JOptionPane.showMessageDialog(null, Messages.getMessage("categoryCreationError"),
244                                    Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
245
246                        }
247                    } catch (HeadlessException e) {
248                        LOG.error(Tools.getStackTrace(e));
249                    } catch (JiwigoException e) {
250                        LOG.error(Tools.getStackTrace(e));
251                    } catch (Exception e) {
252                        LOG.error(Tools.getStackTrace(e));
253                    }
254                } else {
255                    try {
256                        if (SpringUtils.getCategoryService().create(nomcategorie)) {
257                            setUpUi();
258                        } else {
259                            JOptionPane.showMessageDialog(null, Messages.getMessage("categoryCreationError"),
260                                    Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
261
262                        }
263                    } catch (HeadlessException e) {
264                        LOG.error(Tools.getStackTrace(e));
265                    } catch (JiwigoException e) {
266                        LOG.error(Tools.getStackTrace(e));
267                    } catch (Exception e) {
268                        LOG.error(Tools.getStackTrace(e));
269                    }
270                }
271            }
272        } else if (arg0.getSource().equals(menuRefresh)) {
273            setUpUi();
274        } else if (arg0.getSource().equals(menuDelete)) {
275            int response = JOptionPane.showConfirmDialog(null, Messages.getMessage("categories_confirmationDelete"),
276                    Messages.getMessage("confirmation"), JOptionPane.YES_NO_OPTION);
277            if (response == 0) {
278                try {
279                    if (SpringUtils.getCategoryService().delete(selectedCategory)) {
280                        setUpUi();
281                    } else {
282                        JOptionPane.showMessageDialog(null, Messages.getMessage("deletingCategoryError"),
283                                Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
284                    }
285                } catch (Exception e) {
286                    LOG.error(Tools.getStackTrace(e));
287                    JOptionPane.showMessageDialog(null, Messages.getMessage("deletingCategoryError"),
288                            Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
289                }
290            }
291        }
292    }
293}
Note: See TracBrowser for help on using the repository browser.