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

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

Adds a wait cursor for time consuming operations.

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