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

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

implements feature : the number of thumbnails in the thumbnails viewer is adapted to the size of the frame
not perfect but better
feature:0001838

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