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

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

Images can be sent from the file new file browser.

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