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

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

Premier commit. Actuellement, l'application gère :
-Listing des catégories
-Affichage des miniatures
-Ajout de commentaires
-gestion d'un cache pour ne pas télécharger les images plusieurs fois
-gestion d'un zoom dans le navigateur d'images
-navigateur d'images complètement refait, je viens de tester sur un grand écran, à priori, c'est beaucoup mieux
-meilleure gestion des exceptions, avec affichage de messages d'erreurs
-ajout d'un "logger" qui enregistre toutes les exceptions dans un fichier de log
-possibilité de ne pas mettre le "http://" dans l'écran de connexion
-gestion de transformations d'images dans le navigateur d'images : "flip" horizontal et vertical, rotations
-menu dans le navigateur d'images, qui permet d'effectuer toutes les actions, avec en plus, la possibilité d'imprimer une image

en cours d'implémentation : gestion des préférences de l'utilisateur. Actuellement, le fichier xml permettant d'écrire les préférences est géré,
il reste à faire un écran de gestion des préférences, avec un maximum d'options, afin que l'appli soit configurable.

File size: 7.3 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;
10
11import javax.swing.JMenuItem;
12import javax.swing.JOptionPane;
13import javax.swing.JPanel;
14import javax.swing.JPopupMenu;
15import javax.swing.JScrollPane;
16import javax.swing.JTree;
17import javax.swing.event.TreeSelectionEvent;
18import javax.swing.event.TreeSelectionListener;
19import javax.swing.tree.DefaultMutableTreeNode;
20import javax.swing.tree.DefaultTreeModel;
21import javax.swing.tree.TreePath;
22import javax.swing.tree.TreeSelectionModel;
23
24import fr.mael.jiwigo.om.Category;
25import fr.mael.jiwigo.service.CategoryService;
26import fr.mael.jiwigo.transverse.util.Messages;
27import 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 */
58public 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     *  L'arbre
67     */
68    private JTree tree;
69    /**
70     * le menu permettant d'ajouter une catégorie
71     */
72    private JMenuItem menuAjouter;
73    /**
74     * Le menu permettant d'actualiser les catégories
75     */
76    private JMenuItem menuActualiser;
77    /**
78     * la racine du tree, qu'on n'affiche pas
79     */
80    private DefaultMutableTreeNode root = new DefaultMutableTreeNode("");
81    /**
82     * La catégorie selectionnée
83     */
84    private Category selectedCategory;
85
86    /**
87     * Constructeur
88     */
89    public CategoriesTree() {
90        super(new BorderLayout());
91        //creation de l'arbre
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     * Methode qui permet de mettre à jour l'arbre
106     */
107    public void setUpUi() {
108        root.removeAllChildren();
109        createNodes(root);
110        DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
111        model.nodeStructureChanged(root);
112    }
113
114    /* (non-Javadoc)
115     * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent)
116     */
117    public void valueChanged(TreeSelectionEvent e) {
118        DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
119        //
120        if (node == null)
121            return;
122        Object catSelectionnee = node.getUserObject();
123        Category category = (Category) catSelectionnee;
124        MainFrame.imagesPanel.rafraichir(category.getIdentifiant(), false);
125    }
126
127    /**
128     * Creation des noeuds
129     * @param root noeud racine
130     */
131    private void createNodes(DefaultMutableTreeNode root) {
132        DefaultMutableTreeNode category = null;
133        try {
134            List<Category> list = CategoryService.getInstance().construireArbre();
135            for (Category cat : list) {
136                if (cat.getCategoriesMeres().size() == 0) {
137                    category = new DefaultMutableTreeNode(cat);
138                    root.add(category);
139                    recursiveNodeCreation(category, cat);
140                }
141
142            }
143        } catch (Exception e) {
144            LOG.error(Outil.getStackTrace(e));
145            JOptionPane.showMessageDialog(null, Messages.getMessage("treeCreationError"), Messages.getMessage("error"),
146                    JOptionPane.ERROR_MESSAGE);
147        }
148
149    }
150
151    /**
152     * Méthode récursive de creation des noeuds de l'arbre
153     * @param categoryNode
154     * @param category
155     */
156    private void recursiveNodeCreation(DefaultMutableTreeNode categoryNode, Category category) {
157        for (Category cat : category.getCategoriesFilles()) {
158            DefaultMutableTreeNode node = new DefaultMutableTreeNode(cat);
159            categoryNode.add(node);
160            recursiveNodeCreation(node, cat);
161        }
162    }
163
164    @Override
165    public void mouseClicked(MouseEvent arg0) {
166
167    }
168
169    @Override
170    public void mouseEntered(MouseEvent arg0) {
171        // TODO Auto-generated method stub
172
173    }
174
175    @Override
176    public void mouseExited(MouseEvent arg0) {
177        // TODO Auto-generated method stub
178
179    }
180
181    @Override
182    public void mousePressed(MouseEvent e) {
183        int selRow = tree.getRowForLocation(e.getX(), e.getY());
184        TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
185        if (selRow != -1) {
186            if (e.getButton() == 3) {
187                JPopupMenu popup = new JPopupMenu();
188                menuAjouter = new JMenuItem(Messages.getMessage("categories_add"));
189                menuAjouter.addActionListener(this);
190                popup.add(menuAjouter);
191                menuActualiser = new JMenuItem(Messages.getMessage("categories_update"));
192                menuActualiser.addActionListener(this);
193                popup.add(menuActualiser);
194                popup.show(tree, e.getX(), e.getY());
195                DefaultMutableTreeNode node = (DefaultMutableTreeNode) (selPath.getLastPathComponent());
196                this.selectedCategory = (Category) node.getUserObject();
197            }
198        }
199
200    }
201
202    @Override
203    public void mouseReleased(MouseEvent arg0) {
204        // TODO Auto-generated method stub
205
206    }
207
208    @Override
209    public void actionPerformed(ActionEvent arg0) {
210        if (arg0.getSource().equals(menuAjouter)) {
211            String nomcategorie = JOptionPane.showInputDialog(null, Messages.getMessage("categories_enterName"),
212                    Messages.getMessage("categories_add"), JOptionPane.INFORMATION_MESSAGE);
213            if (!(nomcategorie == null) && !nomcategorie.equals("")) {
214                if (CategoryService.getInstance().creer(nomcategorie, selectedCategory.getIdentifiant())) {
215                    setUpUi();
216                } else {
217                    JOptionPane.showMessageDialog(null, Messages.getMessage("categoryCreationError"), Messages
218                            .getMessage("error"), JOptionPane.ERROR_MESSAGE);
219
220                }
221            }
222        } else if (arg0.getSource().equals(menuActualiser)) {
223            setUpUi();
224        }
225    }
226}
Note: See TracBrowser for help on using the repository browser.