[6821] | 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 | /** |
---|
[6980] | 66 | * the tree |
---|
[6821] | 67 | */ |
---|
| 68 | private JTree tree; |
---|
| 69 | /** |
---|
[6980] | 70 | * the menu to add a category |
---|
[6821] | 71 | */ |
---|
| 72 | private JMenuItem menuAjouter; |
---|
| 73 | /** |
---|
[6980] | 74 | * the menu to refresh the categories |
---|
[6821] | 75 | */ |
---|
| 76 | private JMenuItem menuActualiser; |
---|
| 77 | /** |
---|
[6980] | 78 | * the root of the tree which is not displayed |
---|
[6821] | 79 | */ |
---|
| 80 | private DefaultMutableTreeNode root = new DefaultMutableTreeNode(""); |
---|
| 81 | /** |
---|
[6980] | 82 | * the selected category |
---|
[6821] | 83 | */ |
---|
| 84 | private Category selectedCategory; |
---|
| 85 | |
---|
| 86 | /** |
---|
[6980] | 87 | * Constructor |
---|
[6821] | 88 | */ |
---|
| 89 | public CategoriesTree() { |
---|
| 90 | super(new BorderLayout()); |
---|
[6980] | 91 | //creation of the tree |
---|
[6821] | 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 | /** |
---|
[6980] | 105 | * Method that allows to update the tree |
---|
[6821] | 106 | */ |
---|
| 107 | public void setUpUi() { |
---|
[6831] | 108 | //begin bug:0001830 |
---|
[6821] | 109 | root.removeAllChildren(); |
---|
| 110 | createNodes(root); |
---|
[6831] | 111 | ((DefaultTreeModel) tree.getModel()).reload(); |
---|
| 112 | //end |
---|
| 113 | |
---|
[6821] | 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; |
---|
[6958] | 126 | //MainFrame.imagesPanel.rafraichir(category.getIdentifiant(), false); |
---|
| 127 | MainFrame.getInstance().addTabb(new ThumbnailCategoryPanel(category)); |
---|
[6821] | 128 | } |
---|
| 129 | |
---|
| 130 | /** |
---|
[6980] | 131 | * creation of the nodes |
---|
| 132 | * @param root the root |
---|
[6821] | 133 | */ |
---|
| 134 | private void createNodes(DefaultMutableTreeNode root) { |
---|
| 135 | DefaultMutableTreeNode category = null; |
---|
| 136 | try { |
---|
| 137 | List<Category> list = CategoryService.getInstance().construireArbre(); |
---|
| 138 | for (Category cat : list) { |
---|
| 139 | if (cat.getCategoriesMeres().size() == 0) { |
---|
| 140 | category = new DefaultMutableTreeNode(cat); |
---|
| 141 | root.add(category); |
---|
| 142 | recursiveNodeCreation(category, cat); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | } |
---|
| 146 | } catch (Exception e) { |
---|
| 147 | LOG.error(Outil.getStackTrace(e)); |
---|
| 148 | JOptionPane.showMessageDialog(null, Messages.getMessage("treeCreationError"), Messages.getMessage("error"), |
---|
| 149 | JOptionPane.ERROR_MESSAGE); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | /** |
---|
[6980] | 155 | * recursive method for the creation of the nodes |
---|
[6821] | 156 | * @param categoryNode |
---|
| 157 | * @param category |
---|
| 158 | */ |
---|
| 159 | private void recursiveNodeCreation(DefaultMutableTreeNode categoryNode, Category category) { |
---|
| 160 | for (Category cat : category.getCategoriesFilles()) { |
---|
| 161 | DefaultMutableTreeNode node = new DefaultMutableTreeNode(cat); |
---|
| 162 | categoryNode.add(node); |
---|
| 163 | recursiveNodeCreation(node, cat); |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | @Override |
---|
| 168 | public void mouseClicked(MouseEvent arg0) { |
---|
| 169 | |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | @Override |
---|
| 173 | public void mouseEntered(MouseEvent arg0) { |
---|
| 174 | // TODO Auto-generated method stub |
---|
| 175 | |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | @Override |
---|
| 179 | public void mouseExited(MouseEvent arg0) { |
---|
| 180 | // TODO Auto-generated method stub |
---|
| 181 | |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | @Override |
---|
| 185 | public void mousePressed(MouseEvent e) { |
---|
| 186 | int selRow = tree.getRowForLocation(e.getX(), e.getY()); |
---|
| 187 | TreePath selPath = tree.getPathForLocation(e.getX(), e.getY()); |
---|
[6831] | 188 | //begin bug:0001832 |
---|
| 189 | tree.setSelectionRow(selRow); |
---|
| 190 | //end |
---|
[6962] | 191 | |
---|
| 192 | if (e.getButton() == 3) { |
---|
| 193 | JPopupMenu popup = new JPopupMenu(); |
---|
| 194 | menuAjouter = new JMenuItem(Messages.getMessage("categories_add")); |
---|
| 195 | menuAjouter.addActionListener(this); |
---|
| 196 | popup.add(menuAjouter); |
---|
| 197 | menuActualiser = new JMenuItem(Messages.getMessage("categories_update")); |
---|
| 198 | menuActualiser.addActionListener(this); |
---|
| 199 | popup.add(menuActualiser); |
---|
| 200 | popup.show(tree, e.getX(), e.getY()); |
---|
| 201 | if (selRow != -1) { |
---|
[6821] | 202 | DefaultMutableTreeNode node = (DefaultMutableTreeNode) (selPath.getLastPathComponent()); |
---|
| 203 | this.selectedCategory = (Category) node.getUserObject(); |
---|
[6962] | 204 | } else { |
---|
| 205 | this.selectedCategory = null; |
---|
[6821] | 206 | } |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | @Override |
---|
| 212 | public void mouseReleased(MouseEvent arg0) { |
---|
| 213 | // TODO Auto-generated method stub |
---|
| 214 | |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | @Override |
---|
| 218 | public void actionPerformed(ActionEvent arg0) { |
---|
| 219 | if (arg0.getSource().equals(menuAjouter)) { |
---|
| 220 | String nomcategorie = JOptionPane.showInputDialog(null, Messages.getMessage("categories_enterName"), |
---|
| 221 | Messages.getMessage("categories_add"), JOptionPane.INFORMATION_MESSAGE); |
---|
[6962] | 222 | //if the name of the category is not empty |
---|
[6821] | 223 | if (!(nomcategorie == null) && !nomcategorie.equals("")) { |
---|
[6962] | 224 | //if there is a parent category |
---|
| 225 | if (selectedCategory != null) { |
---|
| 226 | //try to create a category |
---|
| 227 | if (CategoryService.getInstance().creer(nomcategorie, selectedCategory.getIdentifiant())) { |
---|
| 228 | setUpUi(); |
---|
| 229 | } else { |
---|
| 230 | JOptionPane.showMessageDialog(null, Messages.getMessage("categoryCreationError"), Messages |
---|
| 231 | .getMessage("error"), JOptionPane.ERROR_MESSAGE); |
---|
| 232 | |
---|
| 233 | } |
---|
[6821] | 234 | } else { |
---|
[6962] | 235 | if (CategoryService.getInstance().creer(nomcategorie)) { |
---|
| 236 | setUpUi(); |
---|
| 237 | } else { |
---|
| 238 | JOptionPane.showMessageDialog(null, Messages.getMessage("categoryCreationError"), Messages |
---|
| 239 | .getMessage("error"), JOptionPane.ERROR_MESSAGE); |
---|
[6821] | 240 | |
---|
[6962] | 241 | } |
---|
[6821] | 242 | } |
---|
| 243 | } |
---|
| 244 | } else if (arg0.getSource().equals(menuActualiser)) { |
---|
| 245 | setUpUi(); |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | } |
---|