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

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

Bug correction :
bug:0001837 : there is now a default translation file (in english), so the application won't crash when a translation is not found
bug:0001833 : the accents are manage when creating a new category
bug:0001832 : on a right click on the categories list, the selection is now visible
bug:0001830 : there is no bug on refreshing the categories tree

Features :
feature:001828 : exif and iptc tags are kept after resizing an image
feature:001827 : pwg.images.addChunk is now fully used : images are split into chunks before being sent

Other features :

  • The user can manage his preferences :

-The web images size
-The chunks size

  • The user can save the login informations (url, login, password) /!\ in a plain text file !
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        //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    }
128
129    /**
130     * Creation des noeuds
131     * @param root noeud racine
132     */
133    private void createNodes(DefaultMutableTreeNode root) {
134        DefaultMutableTreeNode category = null;
135        try {
136            List<Category> list = CategoryService.getInstance().construireArbre();
137            for (Category cat : list) {
138                if (cat.getCategoriesMeres().size() == 0) {
139                    category = new DefaultMutableTreeNode(cat);
140                    root.add(category);
141                    recursiveNodeCreation(category, cat);
142                }
143
144            }
145        } catch (Exception e) {
146            LOG.error(Outil.getStackTrace(e));
147            JOptionPane.showMessageDialog(null, Messages.getMessage("treeCreationError"), Messages.getMessage("error"),
148                    JOptionPane.ERROR_MESSAGE);
149        }
150
151    }
152
153    /**
154     * Méthode récursive de creation des noeuds de l'arbre
155     * @param categoryNode
156     * @param category
157     */
158    private void recursiveNodeCreation(DefaultMutableTreeNode categoryNode, Category category) {
159        for (Category cat : category.getCategoriesFilles()) {
160            DefaultMutableTreeNode node = new DefaultMutableTreeNode(cat);
161            categoryNode.add(node);
162            recursiveNodeCreation(node, cat);
163        }
164    }
165
166    @Override
167    public void mouseClicked(MouseEvent arg0) {
168
169    }
170
171    @Override
172    public void mouseEntered(MouseEvent arg0) {
173        // TODO Auto-generated method stub
174
175    }
176
177    @Override
178    public void mouseExited(MouseEvent arg0) {
179        // TODO Auto-generated method stub
180
181    }
182
183    @Override
184    public void mousePressed(MouseEvent e) {
185        int selRow = tree.getRowForLocation(e.getX(), e.getY());
186        TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
187        //begin bug:0001832
188        tree.setSelectionRow(selRow);
189        //end
190        if (selRow != -1) {
191            if (e.getButton() == 3) {
192                JPopupMenu popup = new JPopupMenu();
193                menuAjouter = new JMenuItem(Messages.getMessage("categories_add"));
194                menuAjouter.addActionListener(this);
195                popup.add(menuAjouter);
196                menuActualiser = new JMenuItem(Messages.getMessage("categories_update"));
197                menuActualiser.addActionListener(this);
198                popup.add(menuActualiser);
199                popup.show(tree, e.getX(), e.getY());
200                DefaultMutableTreeNode node = (DefaultMutableTreeNode) (selPath.getLastPathComponent());
201                this.selectedCategory = (Category) node.getUserObject();
202            }
203        }
204
205    }
206
207    @Override
208    public void mouseReleased(MouseEvent arg0) {
209        // TODO Auto-generated method stub
210
211    }
212
213    @Override
214    public void actionPerformed(ActionEvent arg0) {
215        if (arg0.getSource().equals(menuAjouter)) {
216            String nomcategorie = JOptionPane.showInputDialog(null, Messages.getMessage("categories_enterName"),
217                    Messages.getMessage("categories_add"), JOptionPane.INFORMATION_MESSAGE);
218            if (!(nomcategorie == null) && !nomcategorie.equals("")) {
219                if (CategoryService.getInstance().creer(nomcategorie, selectedCategory.getIdentifiant())) {
220                    setUpUi();
221                } else {
222                    JOptionPane.showMessageDialog(null, Messages.getMessage("categoryCreationError"), Messages
223                            .getMessage("error"), JOptionPane.ERROR_MESSAGE);
224
225                }
226            }
227        } else if (arg0.getSource().equals(menuActualiser)) {
228            setUpUi();
229        }
230    }
231}
Note: See TracBrowser for help on using the repository browser.