1 | package fr.mael.jiwigo.ui.mainframe; |
---|
2 | |
---|
3 | import java.awt.BorderLayout; |
---|
4 | import java.awt.Dimension; |
---|
5 | import java.awt.event.ActionEvent; |
---|
6 | import java.awt.event.ActionListener; |
---|
7 | import java.awt.event.MouseEvent; |
---|
8 | import java.awt.event.MouseListener; |
---|
9 | import java.util.List; |
---|
10 | |
---|
11 | import javax.swing.JMenuItem; |
---|
12 | import javax.swing.JOptionPane; |
---|
13 | import javax.swing.JPanel; |
---|
14 | import javax.swing.JPopupMenu; |
---|
15 | import javax.swing.JScrollPane; |
---|
16 | import javax.swing.JTree; |
---|
17 | import javax.swing.event.TreeSelectionEvent; |
---|
18 | import javax.swing.event.TreeSelectionListener; |
---|
19 | import javax.swing.tree.DefaultMutableTreeNode; |
---|
20 | import javax.swing.tree.DefaultTreeModel; |
---|
21 | import javax.swing.tree.TreePath; |
---|
22 | import javax.swing.tree.TreeSelectionModel; |
---|
23 | |
---|
24 | import fr.mael.jiwigo.om.Category; |
---|
25 | import fr.mael.jiwigo.service.CategoryService; |
---|
26 | import fr.mael.jiwigo.transverse.util.Messages; |
---|
27 | import 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 | */ |
---|
58 | public 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 | * the tree |
---|
67 | */ |
---|
68 | private JTree tree; |
---|
69 | /** |
---|
70 | * the menu to add a category |
---|
71 | */ |
---|
72 | private JMenuItem menuAjouter; |
---|
73 | /** |
---|
74 | * the menu to refresh the categories |
---|
75 | */ |
---|
76 | private JMenuItem menuActualiser; |
---|
77 | /** |
---|
78 | * the root of the tree which is not displayed |
---|
79 | */ |
---|
80 | private DefaultMutableTreeNode root = new DefaultMutableTreeNode(""); |
---|
81 | /** |
---|
82 | * the selected category |
---|
83 | */ |
---|
84 | private Category selectedCategory; |
---|
85 | |
---|
86 | /** |
---|
87 | * Constructor |
---|
88 | */ |
---|
89 | public CategoriesTree() { |
---|
90 | super(new BorderLayout()); |
---|
91 | //creation of the tree |
---|
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 | * 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 | /* (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 | if (!MainFrame.getInstance().isBrowserVisible()) { |
---|
128 | MainFrame.getInstance().setBrowserVisible(); |
---|
129 | } |
---|
130 | MainFrame.getInstance().addTabb(new ThumbnailCategoryPanel(category)); |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * creation of the nodes |
---|
135 | * @param root the root |
---|
136 | */ |
---|
137 | private void createNodes(DefaultMutableTreeNode root) { |
---|
138 | DefaultMutableTreeNode category = null; |
---|
139 | try { |
---|
140 | List<Category> list = CategoryService.getInstance().construireArbre(); |
---|
141 | for (Category cat : list) { |
---|
142 | if (cat.getCategoriesMeres().size() == 0) { |
---|
143 | category = new DefaultMutableTreeNode(cat); |
---|
144 | root.add(category); |
---|
145 | recursiveNodeCreation(category, cat); |
---|
146 | } |
---|
147 | |
---|
148 | } |
---|
149 | } catch (Exception e) { |
---|
150 | LOG.error(Outil.getStackTrace(e)); |
---|
151 | JOptionPane.showMessageDialog(null, Messages.getMessage("treeCreationError"), Messages.getMessage("error"), |
---|
152 | JOptionPane.ERROR_MESSAGE); |
---|
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 | @Override |
---|
171 | public void mouseClicked(MouseEvent arg0) { |
---|
172 | |
---|
173 | } |
---|
174 | |
---|
175 | @Override |
---|
176 | public void mouseEntered(MouseEvent arg0) { |
---|
177 | // TODO Auto-generated method stub |
---|
178 | |
---|
179 | } |
---|
180 | |
---|
181 | @Override |
---|
182 | public void mouseExited(MouseEvent arg0) { |
---|
183 | // TODO Auto-generated method stub |
---|
184 | |
---|
185 | } |
---|
186 | |
---|
187 | @Override |
---|
188 | public void mousePressed(MouseEvent e) { |
---|
189 | int selRow = tree.getRowForLocation(e.getX(), e.getY()); |
---|
190 | TreePath selPath = tree.getPathForLocation(e.getX(), e.getY()); |
---|
191 | //begin bug:0001832 |
---|
192 | tree.setSelectionRow(selRow); |
---|
193 | //end |
---|
194 | |
---|
195 | if (e.getButton() == 3) { |
---|
196 | JPopupMenu popup = new JPopupMenu(); |
---|
197 | menuAjouter = new JMenuItem(Messages.getMessage("categories_add")); |
---|
198 | menuAjouter.addActionListener(this); |
---|
199 | popup.add(menuAjouter); |
---|
200 | menuActualiser = new JMenuItem(Messages.getMessage("categories_update")); |
---|
201 | menuActualiser.addActionListener(this); |
---|
202 | popup.add(menuActualiser); |
---|
203 | popup.show(tree, e.getX(), e.getY()); |
---|
204 | if (selRow != -1) { |
---|
205 | DefaultMutableTreeNode node = (DefaultMutableTreeNode) (selPath.getLastPathComponent()); |
---|
206 | this.selectedCategory = (Category) node.getUserObject(); |
---|
207 | } else { |
---|
208 | this.selectedCategory = null; |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | } |
---|
213 | |
---|
214 | @Override |
---|
215 | public void mouseReleased(MouseEvent arg0) { |
---|
216 | // TODO Auto-generated method stub |
---|
217 | |
---|
218 | } |
---|
219 | |
---|
220 | @Override |
---|
221 | public void actionPerformed(ActionEvent arg0) { |
---|
222 | if (arg0.getSource().equals(menuAjouter)) { |
---|
223 | String nomcategorie = JOptionPane.showInputDialog(null, Messages.getMessage("categories_enterName"), |
---|
224 | Messages.getMessage("categories_add"), JOptionPane.INFORMATION_MESSAGE); |
---|
225 | //if the name of the category is not empty |
---|
226 | if (!(nomcategorie == null) && !nomcategorie.equals("")) { |
---|
227 | //if there is a parent category |
---|
228 | if (selectedCategory != null) { |
---|
229 | //try to create a category |
---|
230 | if (CategoryService.getInstance().creer(nomcategorie, selectedCategory.getIdentifiant())) { |
---|
231 | setUpUi(); |
---|
232 | } else { |
---|
233 | JOptionPane.showMessageDialog(null, Messages.getMessage("categoryCreationError"), Messages |
---|
234 | .getMessage("error"), JOptionPane.ERROR_MESSAGE); |
---|
235 | |
---|
236 | } |
---|
237 | } else { |
---|
238 | if (CategoryService.getInstance().creer(nomcategorie)) { |
---|
239 | setUpUi(); |
---|
240 | } else { |
---|
241 | JOptionPane.showMessageDialog(null, Messages.getMessage("categoryCreationError"), Messages |
---|
242 | .getMessage("error"), JOptionPane.ERROR_MESSAGE); |
---|
243 | |
---|
244 | } |
---|
245 | } |
---|
246 | } |
---|
247 | } else if (arg0.getSource().equals(menuActualiser)) { |
---|
248 | setUpUi(); |
---|
249 | } |
---|
250 | } |
---|
251 | } |
---|