package fr.mael.jiwigo.ui.mainframe; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.List; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreePath; import javax.swing.tree.TreeSelectionModel; import fr.mael.jiwigo.om.Category; import fr.mael.jiwigo.service.CategoryService; import fr.mael.jiwigo.transverse.util.Messages; import fr.mael.jiwigo.transverse.util.Outil; /** Copyright (c) 2010, Mael All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of jiwigo nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * @author mael * Arbre des catégories */ public class CategoriesTree extends JPanel implements TreeSelectionListener, MouseListener, ActionListener { /** * Logger */ public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory .getLog(CategoriesTree.class); /** * the tree */ private JTree tree; /** * the menu to add a category */ private JMenuItem menuAjouter; /** * the menu to refresh the categories */ private JMenuItem menuActualiser; /** * the root of the tree which is not displayed */ private DefaultMutableTreeNode root = new DefaultMutableTreeNode(""); /** * the selected category */ private Category selectedCategory; /** * Constructor */ public CategoriesTree() { super(new BorderLayout()); //creation of the tree createNodes(root); tree = new JTree(root); tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); tree.addTreeSelectionListener(this); tree.setRootVisible(false); tree.addMouseListener(this); JScrollPane treeView = new JScrollPane(tree); Dimension minimumSize = new Dimension(100, 50); treeView.setMinimumSize(minimumSize); add(treeView, BorderLayout.CENTER); } /** * Method that allows to update the tree */ public void setUpUi() { //begin bug:0001830 root.removeAllChildren(); createNodes(root); ((DefaultTreeModel) tree.getModel()).reload(); //end } /* (non-Javadoc) * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent) */ public void valueChanged(TreeSelectionEvent e) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); // if (node == null) return; Object catSelectionnee = node.getUserObject(); Category category = (Category) catSelectionnee; //MainFrame.imagesPanel.rafraichir(category.getIdentifiant(), false); MainFrame.getInstance().addTabb(new ThumbnailCategoryPanel(category)); } /** * creation of the nodes * @param root the root */ private void createNodes(DefaultMutableTreeNode root) { DefaultMutableTreeNode category = null; try { List list = CategoryService.getInstance().construireArbre(); for (Category cat : list) { if (cat.getCategoriesMeres().size() == 0) { category = new DefaultMutableTreeNode(cat); root.add(category); recursiveNodeCreation(category, cat); } } } catch (Exception e) { LOG.error(Outil.getStackTrace(e)); JOptionPane.showMessageDialog(null, Messages.getMessage("treeCreationError"), Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); } } /** * recursive method for the creation of the nodes * @param categoryNode * @param category */ private void recursiveNodeCreation(DefaultMutableTreeNode categoryNode, Category category) { for (Category cat : category.getCategoriesFilles()) { DefaultMutableTreeNode node = new DefaultMutableTreeNode(cat); categoryNode.add(node); recursiveNodeCreation(node, cat); } } @Override public void mouseClicked(MouseEvent arg0) { } @Override public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { int selRow = tree.getRowForLocation(e.getX(), e.getY()); TreePath selPath = tree.getPathForLocation(e.getX(), e.getY()); //begin bug:0001832 tree.setSelectionRow(selRow); //end if (e.getButton() == 3) { JPopupMenu popup = new JPopupMenu(); menuAjouter = new JMenuItem(Messages.getMessage("categories_add")); menuAjouter.addActionListener(this); popup.add(menuAjouter); menuActualiser = new JMenuItem(Messages.getMessage("categories_update")); menuActualiser.addActionListener(this); popup.add(menuActualiser); popup.show(tree, e.getX(), e.getY()); if (selRow != -1) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) (selPath.getLastPathComponent()); this.selectedCategory = (Category) node.getUserObject(); } else { this.selectedCategory = null; } } } @Override public void mouseReleased(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void actionPerformed(ActionEvent arg0) { if (arg0.getSource().equals(menuAjouter)) { String nomcategorie = JOptionPane.showInputDialog(null, Messages.getMessage("categories_enterName"), Messages.getMessage("categories_add"), JOptionPane.INFORMATION_MESSAGE); //if the name of the category is not empty if (!(nomcategorie == null) && !nomcategorie.equals("")) { //if there is a parent category if (selectedCategory != null) { //try to create a category if (CategoryService.getInstance().creer(nomcategorie, selectedCategory.getIdentifiant())) { setUpUi(); } else { JOptionPane.showMessageDialog(null, Messages.getMessage("categoryCreationError"), Messages .getMessage("error"), JOptionPane.ERROR_MESSAGE); } } else { if (CategoryService.getInstance().creer(nomcategorie)) { setUpUi(); } else { JOptionPane.showMessageDialog(null, Messages.getMessage("categoryCreationError"), Messages .getMessage("error"), JOptionPane.ERROR_MESSAGE); } } } } else if (arg0.getSource().equals(menuActualiser)) { setUpUi(); } } }