source: extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/ui/search/tree/FileTree.java @ 10809

Last change on this file since 10809 was 10809, checked in by mlg, 13 years ago

Changement in the file browsing
Jiwigo does not browse the user directory on startup (memory consumption is limited while the user does not use the file browser to search images).

File size: 9.0 KB
Line 
1package fr.mael.jiwigo.ui.search.tree;
2
3import java.awt.BorderLayout;
4import java.awt.Dimension;
5import java.awt.FlowLayout;
6import java.awt.event.ActionEvent;
7import java.awt.event.ActionListener;
8import java.awt.event.MouseEvent;
9import java.awt.event.MouseListener;
10import java.util.ArrayList;
11
12import javax.swing.ImageIcon;
13import javax.swing.JButton;
14import javax.swing.JFileChooser;
15import javax.swing.JMenuItem;
16import javax.swing.JPanel;
17import javax.swing.JPopupMenu;
18import javax.swing.JScrollPane;
19import javax.swing.JTree;
20import javax.swing.event.TreeSelectionEvent;
21import javax.swing.event.TreeSelectionListener;
22import javax.swing.tree.DefaultMutableTreeNode;
23import javax.swing.tree.DefaultTreeModel;
24import javax.swing.tree.TreePath;
25import javax.swing.tree.TreeSelectionModel;
26
27import fr.mael.jiwigo.transverse.ImagesManagement;
28import fr.mael.jiwigo.transverse.util.Messages;
29import fr.mael.jiwigo.transverse.util.Tools;
30import fr.mael.jiwigo.ui.field.HintTextField;
31import fr.mael.jiwigo.ui.mainframe.DialogPrivacyLevel;
32import fr.mael.jiwigo.ui.search.DialogChooseCategory;
33import fr.mael.jiwigo.ui.search.File;
34
35/**
36    Copyright (c) 2010, Mael
37    All rights reserved.
38
39    Redistribution and use in source and binary forms, with or without
40    modification, are permitted provided that the following conditions are met:
41     * Redistributions of source code must retain the above copyright
42       notice, this list of conditions and the following disclaimer.
43     * Redistributions in binary form must reproduce the above copyright
44       notice, this list of conditions and the following disclaimer in the
45       documentation and/or other materials provided with the distribution.
46     * Neither the name of jiwigo nor the
47       names of its contributors may be used to endorse or promote products
48       derived from this software without specific prior written permission.
49
50    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
51    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
52    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53    DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY
54    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
55    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
56    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
57    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60
61    * @author mael
62    * File tree
63*/
64public class FileTree extends JPanel implements TreeSelectionListener, MouseListener, ActionListener {
65    private String path;
66
67    private HintTextField fieldPath;
68
69    private JButton refreshButton;
70
71    private JPanel panelNorth;
72
73    private DefaultMutableTreeNode root = new DefaultMutableTreeNode("");
74
75    private JTree tree;
76
77    private ArrayList<File> filesToSend;
78
79    private File f;
80    private File fls[];
81    private ArrayList<File> filter;
82    private File fls2[];
83    private ArrayList<File> filter2 = new ArrayList<File>();
84    /**
85     * the menu to add a category
86     */
87    private JMenuItem menuSend;
88
89    /**
90     * Constructor
91     * @param path : the path of the directory where to search for files
92     */
93    public FileTree(String path) {
94        super(new BorderLayout());
95        this.path = path;
96        fieldPath = new HintTextField(Messages.getMessage("fileTree_clickToChoose"));
97        if (path != null) {
98            fieldPath.setText(path);
99        }
100        fieldPath.addMouseListener(this);
101        fieldPath.setPreferredSize(new Dimension(150, 30));
102        refreshButton = new JButton();
103        refreshButton.setIcon(new ImageIcon(Tools.getURL("fr/mael/jiwigo/img/refresh.png")));
104        refreshButton.addActionListener(this);
105        refreshButton.setFocusPainted(false);
106        refreshButton.setBorderPainted(false);
107        refreshButton.setContentAreaFilled(false);
108
109        panelNorth = new JPanel(new FlowLayout());
110        panelNorth.add(fieldPath);
111        panelNorth.add(refreshButton);
112
113        tree = new JTree(root);
114        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
115        tree.addTreeSelectionListener(this);
116        tree.setCellRenderer(new MyTreeCellRenderer());
117        tree.setRootVisible(false);
118        tree.addMouseListener(this);
119        JScrollPane treeView = new JScrollPane(tree);
120        Dimension minimumSize = new Dimension(100, 50);
121        treeView.setMinimumSize(minimumSize);
122        this.add(treeView, BorderLayout.CENTER);
123        this.add(panelNorth, BorderLayout.NORTH);
124        reload();
125    }
126
127    /**
128     * Method that reload the tree
129     */
130    public void reload() {
131        if (path != null) {
132            root.removeAllChildren();
133            createNodes(root);
134            ((DefaultTreeModel) tree.getModel()).reload();
135        }
136    }
137
138    /**
139     * This method creates filters the files to display depending on their
140     * extension (only image files)
141     * @param root
142     */
143    private void createNodes(DefaultMutableTreeNode root) {
144        DefaultMutableTreeNode fileNode = null;
145        f = new File(path);
146        try {
147            fls = null;
148            filter = null;
149            System.runFinalization();
150            fls = f.listFiles();
151            filter = new ArrayList<File>();
152            for (File file : fls) {
153                //only png and jpg are displayed
154                if ((file.isDirectory() && !(file.getName().charAt(0) == '.'))
155                        || (file.isFile() && ((file.getName().toLowerCase().endsWith(".jpg")
156                                || file.getName().toLowerCase().endsWith(".jpeg") || file.getName().toLowerCase()
157                                .endsWith(".png"))))) {
158                    filter.add(file);
159                }
160            }
161            //call to the method that creates the files hierarchy
162            for (File file : filter) {
163                fileNode = new DefaultMutableTreeNode(file);
164                root.add(fileNode);
165                recursiveNodeCreation(fileNode, file);
166
167            }
168        } catch (Exception e) {
169            e.printStackTrace();
170        }
171
172    }
173
174    /**
175     * recursive method for the creation of the nodes
176     * @param categoryNode
177     * @param category
178     */
179    private void recursiveNodeCreation(DefaultMutableTreeNode categoryNode, File file) {
180        if (file.isDirectory() && !(file.getName().charAt(0) == '.')) {
181            fls2 = null;
182            filter2 = null;
183            System.runFinalization();
184            fls2 = file.listFiles();
185            filter2 = new ArrayList<File>();
186            for (File fich : fls2) {
187                if (!(fich.getName().charAt(0) == '.')
188                        && fich.isDirectory()
189                        || (fich.isFile() && ((fich.getName().toLowerCase().endsWith(".jpg")
190                                || fich.getName().toLowerCase().endsWith(".jpeg") || fich.getName().toLowerCase()
191                                .endsWith(".png"))))) {
192                    filter2.add(fich);
193                }
194            }
195            for (File fich : filter2) {
196                DefaultMutableTreeNode node = new DefaultMutableTreeNode(fich);
197                categoryNode.add(node);
198                recursiveNodeCreation(node, fich);
199            }
200        }
201    }
202
203    @Override
204    public void valueChanged(TreeSelectionEvent arg0) {
205
206    }
207
208    @Override
209    public void mouseClicked(MouseEvent mouseEvent) {
210        if (mouseEvent.getSource().equals(fieldPath)) {
211            JFileChooser chooser = new JFileChooser();
212            if (path != null) {
213                chooser.setCurrentDirectory(new java.io.File(path));
214            }
215            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
216            chooser.setAcceptAllFileFilterUsed(false);
217            if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
218                path = chooser.getSelectedFile().getAbsolutePath();
219                fieldPath.setText(path);
220                reload();
221            }
222        }
223
224        if (mouseEvent.getButton() == 3) {
225            filesToSend = new ArrayList<File>();
226            TreePath treePath[] = tree.getSelectionPaths();
227            for (TreePath treeP : treePath) {
228                DefaultMutableTreeNode node = (DefaultMutableTreeNode) treeP.getLastPathComponent();
229                File file = (File) node.getUserObject();
230                filesToSend.add(file);
231            }
232            JPopupMenu popup = new JPopupMenu();
233            menuSend = new JMenuItem("Send");
234            menuSend.addActionListener(this);
235            popup.add(menuSend);
236            popup.show(tree, mouseEvent.getX(), mouseEvent.getY());
237        }
238    }
239
240    @Override
241    public void mouseEntered(MouseEvent arg0) {
242        // TODO Auto-generated method stub
243
244    }
245
246    @Override
247    public void mouseExited(MouseEvent arg0) {
248        // TODO Auto-generated method stub
249
250    }
251
252    @Override
253    public void mousePressed(MouseEvent arg0) {
254        // TODO Auto-generated method stub
255
256    }
257
258    @Override
259    public void mouseReleased(MouseEvent arg0) {
260        // TODO Auto-generated method stub
261
262    }
263
264    public JTree getTree() {
265        return tree;
266    }
267
268    @Override
269    public void actionPerformed(ActionEvent paramActionEvent) {
270        if (paramActionEvent.getSource().equals(menuSend)) {
271            //first, the dialog that allows to choose the privacy level of the photo(s) is called
272            if (!ImagesManagement.getInstance().isRememberPrivacyLevel()) {
273                new DialogPrivacyLevel();
274            }
275            //then the dialog that allow to choose the category is called
276            new DialogChooseCategory(filesToSend);
277        } else if (paramActionEvent.getSource().equals(refreshButton)) {
278            reload();
279        }
280    }
281}
Note: See TracBrowser for help on using the repository browser.