1 | package fr.mael.jiwigo.ui.search.tree; |
---|
2 | |
---|
3 | import java.awt.BorderLayout; |
---|
4 | import java.awt.Dimension; |
---|
5 | import java.awt.event.MouseEvent; |
---|
6 | import java.awt.event.MouseListener; |
---|
7 | import java.util.ArrayList; |
---|
8 | |
---|
9 | import javax.swing.JFileChooser; |
---|
10 | import javax.swing.JFrame; |
---|
11 | import javax.swing.JPanel; |
---|
12 | import javax.swing.JScrollPane; |
---|
13 | import javax.swing.JTextField; |
---|
14 | import javax.swing.JTree; |
---|
15 | import javax.swing.event.TreeSelectionEvent; |
---|
16 | import javax.swing.event.TreeSelectionListener; |
---|
17 | import javax.swing.tree.DefaultMutableTreeNode; |
---|
18 | import javax.swing.tree.DefaultTreeModel; |
---|
19 | import javax.swing.tree.TreeSelectionModel; |
---|
20 | |
---|
21 | import fr.mael.jiwigo.ui.search.File; |
---|
22 | |
---|
23 | /** |
---|
24 | Copyright (c) 2010, Mael |
---|
25 | All rights reserved. |
---|
26 | |
---|
27 | Redistribution and use in source and binary forms, with or without |
---|
28 | modification, are permitted provided that the following conditions are met: |
---|
29 | * Redistributions of source code must retain the above copyright |
---|
30 | notice, this list of conditions and the following disclaimer. |
---|
31 | * Redistributions in binary form must reproduce the above copyright |
---|
32 | notice, this list of conditions and the following disclaimer in the |
---|
33 | documentation and/or other materials provided with the distribution. |
---|
34 | * Neither the name of jiwigo nor the |
---|
35 | names of its contributors may be used to endorse or promote products |
---|
36 | derived from this software without specific prior written permission. |
---|
37 | |
---|
38 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
39 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
40 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
41 | DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY |
---|
42 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
43 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
44 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
45 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
46 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
47 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
48 | |
---|
49 | * @author mael |
---|
50 | * File tree |
---|
51 | */ |
---|
52 | public class FileTree extends JPanel implements TreeSelectionListener, MouseListener { |
---|
53 | private String path; |
---|
54 | |
---|
55 | private JTextField fieldPath; |
---|
56 | |
---|
57 | private DefaultMutableTreeNode root = new DefaultMutableTreeNode(""); |
---|
58 | |
---|
59 | private JTree tree; |
---|
60 | |
---|
61 | public static void main(String[] args) { |
---|
62 | JFrame frame = new JFrame(); |
---|
63 | frame.add(new FileTree("/home/mael/Documents")); |
---|
64 | |
---|
65 | frame.pack(); |
---|
66 | frame.setVisible(true); |
---|
67 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | * Constructor |
---|
72 | * @param path : the path of the directory where to search for files |
---|
73 | */ |
---|
74 | public FileTree(String path) { |
---|
75 | super(new BorderLayout()); |
---|
76 | this.path = path; |
---|
77 | fieldPath = new JTextField(path); |
---|
78 | fieldPath.addMouseListener(this); |
---|
79 | tree = new JTree(root); |
---|
80 | tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); |
---|
81 | tree.addTreeSelectionListener(this); |
---|
82 | tree.setCellRenderer(new MyTreeCellRenderer()); |
---|
83 | tree.setRootVisible(false); |
---|
84 | JScrollPane treeView = new JScrollPane(tree); |
---|
85 | Dimension minimumSize = new Dimension(100, 50); |
---|
86 | treeView.setMinimumSize(minimumSize); |
---|
87 | this.add(treeView, BorderLayout.CENTER); |
---|
88 | this.add(fieldPath, BorderLayout.NORTH); |
---|
89 | reload(); |
---|
90 | } |
---|
91 | |
---|
92 | public void reload() { |
---|
93 | root.removeAllChildren(); |
---|
94 | createNodes(root); |
---|
95 | ((DefaultTreeModel) tree.getModel()).reload(); |
---|
96 | } |
---|
97 | |
---|
98 | private void createNodes(DefaultMutableTreeNode root) { |
---|
99 | DefaultMutableTreeNode fileNode = null; |
---|
100 | File f = new File(path); |
---|
101 | try { |
---|
102 | File fls[] = f.listFiles(); |
---|
103 | ArrayList<File> filter = new ArrayList<File>(); |
---|
104 | for (File file : fls) { |
---|
105 | if ((file.isDirectory() && !(file.getName().charAt(0) == '.')) |
---|
106 | || (file.isFile() && ((file.getName().toLowerCase().endsWith(".jpg") |
---|
107 | || file.getName().toLowerCase().endsWith(".jpeg") || file.getName().toLowerCase() |
---|
108 | .endsWith(".png"))))) { |
---|
109 | filter.add(file); |
---|
110 | } |
---|
111 | } |
---|
112 | for (File file : filter) { |
---|
113 | fileNode = new DefaultMutableTreeNode(file); |
---|
114 | root.add(fileNode); |
---|
115 | recursiveNodeCreation(fileNode, file); |
---|
116 | |
---|
117 | } |
---|
118 | } catch (Exception e) { |
---|
119 | e.printStackTrace(); |
---|
120 | } |
---|
121 | |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | * recursive method for the creation of the nodes |
---|
126 | * @param categoryNode |
---|
127 | * @param category |
---|
128 | */ |
---|
129 | private void recursiveNodeCreation(DefaultMutableTreeNode categoryNode, File file) { |
---|
130 | if (file.isDirectory() && !(file.getName().charAt(0) == '.')) { |
---|
131 | File fls[] = file.listFiles(); |
---|
132 | ArrayList<File> filter = new ArrayList<File>(); |
---|
133 | for (File fich : fls) { |
---|
134 | if (!(fich.getName().charAt(0) == '.') |
---|
135 | && fich.isDirectory() |
---|
136 | || (fich.isFile() && ((fich.getName().toLowerCase().endsWith(".jpg") |
---|
137 | || fich.getName().toLowerCase().endsWith(".jpeg") || fich.getName().toLowerCase() |
---|
138 | .endsWith(".png"))))) { |
---|
139 | filter.add(fich); |
---|
140 | } |
---|
141 | } |
---|
142 | for (File fich : filter) { |
---|
143 | DefaultMutableTreeNode node = new DefaultMutableTreeNode(fich); |
---|
144 | categoryNode.add(node); |
---|
145 | recursiveNodeCreation(node, fich); |
---|
146 | } |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | @Override |
---|
151 | public void valueChanged(TreeSelectionEvent arg0) { |
---|
152 | |
---|
153 | } |
---|
154 | |
---|
155 | @Override |
---|
156 | public void mouseClicked(MouseEvent arg0) { |
---|
157 | if (arg0.getSource().equals(fieldPath)) { |
---|
158 | JFileChooser chooser = new JFileChooser(); |
---|
159 | chooser.setCurrentDirectory(new java.io.File(path)); |
---|
160 | chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
---|
161 | chooser.setAcceptAllFileFilterUsed(false); |
---|
162 | if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { |
---|
163 | path = chooser.getSelectedFile().getAbsolutePath(); |
---|
164 | fieldPath.setText(path); |
---|
165 | reload(); |
---|
166 | } |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | @Override |
---|
171 | public void mouseEntered(MouseEvent arg0) { |
---|
172 | // TODO Auto-generated method stub |
---|
173 | |
---|
174 | } |
---|
175 | |
---|
176 | @Override |
---|
177 | public void mouseExited(MouseEvent arg0) { |
---|
178 | // TODO Auto-generated method stub |
---|
179 | |
---|
180 | } |
---|
181 | |
---|
182 | @Override |
---|
183 | public void mousePressed(MouseEvent arg0) { |
---|
184 | // TODO Auto-generated method stub |
---|
185 | |
---|
186 | } |
---|
187 | |
---|
188 | @Override |
---|
189 | public void mouseReleased(MouseEvent arg0) { |
---|
190 | // TODO Auto-generated method stub |
---|
191 | |
---|
192 | } |
---|
193 | |
---|
194 | public JTree getTree() { |
---|
195 | return tree; |
---|
196 | } |
---|
197 | |
---|
198 | } |
---|