1 | package fr.mael.jiwigo.ui.search.tree; |
---|
2 | |
---|
3 | import java.awt.BorderLayout; |
---|
4 | import java.awt.Dimension; |
---|
5 | import java.awt.FlowLayout; |
---|
6 | import java.awt.event.ActionEvent; |
---|
7 | import java.awt.event.ActionListener; |
---|
8 | import java.awt.event.MouseEvent; |
---|
9 | import java.awt.event.MouseListener; |
---|
10 | import java.util.ArrayList; |
---|
11 | |
---|
12 | import javax.swing.ImageIcon; |
---|
13 | import javax.swing.JButton; |
---|
14 | import javax.swing.JFileChooser; |
---|
15 | import javax.swing.JFrame; |
---|
16 | import javax.swing.JMenuItem; |
---|
17 | import javax.swing.JPanel; |
---|
18 | import javax.swing.JPopupMenu; |
---|
19 | import javax.swing.JScrollPane; |
---|
20 | import javax.swing.JTextField; |
---|
21 | import javax.swing.JTree; |
---|
22 | import javax.swing.event.TreeSelectionEvent; |
---|
23 | import javax.swing.event.TreeSelectionListener; |
---|
24 | import javax.swing.tree.DefaultMutableTreeNode; |
---|
25 | import javax.swing.tree.DefaultTreeModel; |
---|
26 | import javax.swing.tree.TreePath; |
---|
27 | import javax.swing.tree.TreeSelectionModel; |
---|
28 | |
---|
29 | import fr.mael.jiwigo.transverse.ImagesManagement; |
---|
30 | import fr.mael.jiwigo.transverse.util.Tools; |
---|
31 | import fr.mael.jiwigo.ui.mainframe.DialogPrivacyLevel; |
---|
32 | import fr.mael.jiwigo.ui.search.DialogChooseCategory; |
---|
33 | import 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 | */ |
---|
64 | public class FileTree extends JPanel implements TreeSelectionListener, MouseListener, ActionListener { |
---|
65 | private String path; |
---|
66 | |
---|
67 | private JTextField 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 | /** |
---|
80 | * the menu to add a category |
---|
81 | */ |
---|
82 | private JMenuItem menuSend; |
---|
83 | |
---|
84 | public static void main(String[] args) { |
---|
85 | JFrame frame = new JFrame(); |
---|
86 | frame.add(new FileTree("/home/mael/Documents")); |
---|
87 | |
---|
88 | frame.pack(); |
---|
89 | frame.setVisible(true); |
---|
90 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * Constructor |
---|
95 | * @param path : the path of the directory where to search for files |
---|
96 | */ |
---|
97 | public FileTree(String path) { |
---|
98 | super(new BorderLayout()); |
---|
99 | this.path = path; |
---|
100 | fieldPath = new JTextField(path); |
---|
101 | fieldPath.addMouseListener(this); |
---|
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 | root.removeAllChildren(); |
---|
132 | createNodes(root); |
---|
133 | ((DefaultTreeModel) tree.getModel()).reload(); |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * This method creates filters the files to display depending on their |
---|
138 | * extension (only image files) |
---|
139 | * @param root |
---|
140 | */ |
---|
141 | private void createNodes(DefaultMutableTreeNode root) { |
---|
142 | DefaultMutableTreeNode fileNode = null; |
---|
143 | File f = new File(path); |
---|
144 | try { |
---|
145 | File fls[] = f.listFiles(); |
---|
146 | ArrayList<File> filter = new ArrayList<File>(); |
---|
147 | for (File file : fls) { |
---|
148 | //only png and jpg are displayed |
---|
149 | if ((file.isDirectory() && !(file.getName().charAt(0) == '.')) |
---|
150 | || (file.isFile() && ((file.getName().toLowerCase().endsWith(".jpg") |
---|
151 | || file.getName().toLowerCase().endsWith(".jpeg") || file.getName().toLowerCase() |
---|
152 | .endsWith(".png"))))) { |
---|
153 | filter.add(file); |
---|
154 | } |
---|
155 | } |
---|
156 | //call to the method that creates the files hierarchy |
---|
157 | for (File file : filter) { |
---|
158 | fileNode = new DefaultMutableTreeNode(file); |
---|
159 | root.add(fileNode); |
---|
160 | recursiveNodeCreation(fileNode, file); |
---|
161 | |
---|
162 | } |
---|
163 | } catch (Exception e) { |
---|
164 | e.printStackTrace(); |
---|
165 | } |
---|
166 | |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | * recursive method for the creation of the nodes |
---|
171 | * @param categoryNode |
---|
172 | * @param category |
---|
173 | */ |
---|
174 | private void recursiveNodeCreation(DefaultMutableTreeNode categoryNode, File file) { |
---|
175 | if (file.isDirectory() && !(file.getName().charAt(0) == '.')) { |
---|
176 | File fls[] = file.listFiles(); |
---|
177 | ArrayList<File> filter = new ArrayList<File>(); |
---|
178 | for (File fich : fls) { |
---|
179 | if (!(fich.getName().charAt(0) == '.') |
---|
180 | && fich.isDirectory() |
---|
181 | || (fich.isFile() && ((fich.getName().toLowerCase().endsWith(".jpg") |
---|
182 | || fich.getName().toLowerCase().endsWith(".jpeg") || fich.getName().toLowerCase() |
---|
183 | .endsWith(".png"))))) { |
---|
184 | filter.add(fich); |
---|
185 | } |
---|
186 | } |
---|
187 | for (File fich : filter) { |
---|
188 | DefaultMutableTreeNode node = new DefaultMutableTreeNode(fich); |
---|
189 | categoryNode.add(node); |
---|
190 | recursiveNodeCreation(node, fich); |
---|
191 | } |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | @Override |
---|
196 | public void valueChanged(TreeSelectionEvent arg0) { |
---|
197 | |
---|
198 | } |
---|
199 | |
---|
200 | @Override |
---|
201 | public void mouseClicked(MouseEvent mouseEvent) { |
---|
202 | if (mouseEvent.getSource().equals(fieldPath)) { |
---|
203 | JFileChooser chooser = new JFileChooser(); |
---|
204 | chooser.setCurrentDirectory(new java.io.File(path)); |
---|
205 | chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
---|
206 | chooser.setAcceptAllFileFilterUsed(false); |
---|
207 | if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { |
---|
208 | path = chooser.getSelectedFile().getAbsolutePath(); |
---|
209 | fieldPath.setText(path); |
---|
210 | reload(); |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | if (mouseEvent.getButton() == 3) { |
---|
215 | filesToSend = new ArrayList<File>(); |
---|
216 | TreePath treePath[] = tree.getSelectionPaths(); |
---|
217 | for (TreePath treeP : treePath) { |
---|
218 | DefaultMutableTreeNode node = (DefaultMutableTreeNode) treeP.getLastPathComponent(); |
---|
219 | File file = (File) node.getUserObject(); |
---|
220 | filesToSend.add(file); |
---|
221 | } |
---|
222 | JPopupMenu popup = new JPopupMenu(); |
---|
223 | menuSend = new JMenuItem("Envoyer"); |
---|
224 | menuSend.addActionListener(this); |
---|
225 | popup.add(menuSend); |
---|
226 | popup.show(tree, mouseEvent.getX(), mouseEvent.getY()); |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | @Override |
---|
231 | public void mouseEntered(MouseEvent arg0) { |
---|
232 | // TODO Auto-generated method stub |
---|
233 | |
---|
234 | } |
---|
235 | |
---|
236 | @Override |
---|
237 | public void mouseExited(MouseEvent arg0) { |
---|
238 | // TODO Auto-generated method stub |
---|
239 | |
---|
240 | } |
---|
241 | |
---|
242 | @Override |
---|
243 | public void mousePressed(MouseEvent arg0) { |
---|
244 | // TODO Auto-generated method stub |
---|
245 | |
---|
246 | } |
---|
247 | |
---|
248 | @Override |
---|
249 | public void mouseReleased(MouseEvent arg0) { |
---|
250 | // TODO Auto-generated method stub |
---|
251 | |
---|
252 | } |
---|
253 | |
---|
254 | public JTree getTree() { |
---|
255 | return tree; |
---|
256 | } |
---|
257 | |
---|
258 | @Override |
---|
259 | public void actionPerformed(ActionEvent paramActionEvent) { |
---|
260 | if (paramActionEvent.getSource().equals(menuSend)) { |
---|
261 | //first, the dialog that allows to choose the privacy level of the photo(s) is called |
---|
262 | if (!ImagesManagement.getInstance().isRememberPrivacyLevel()) { |
---|
263 | new DialogPrivacyLevel(); |
---|
264 | } |
---|
265 | //then the dialog that allow to choose the category is called |
---|
266 | new DialogChooseCategory(filesToSend); |
---|
267 | } else if (paramActionEvent.getSource().equals(refreshButton)) { |
---|
268 | reload(); |
---|
269 | } |
---|
270 | } |
---|
271 | } |
---|