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

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

Translation

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