1 | package fr.mael.jiwigo.ui.search.tree; |
---|
2 | |
---|
3 | import java.awt.Component; |
---|
4 | |
---|
5 | import javax.swing.ImageIcon; |
---|
6 | import javax.swing.JTree; |
---|
7 | import javax.swing.tree.DefaultMutableTreeNode; |
---|
8 | import javax.swing.tree.DefaultTreeCellRenderer; |
---|
9 | |
---|
10 | import fr.mael.jiwigo.transverse.util.Outil; |
---|
11 | import fr.mael.jiwigo.ui.search.File; |
---|
12 | |
---|
13 | /** |
---|
14 | Copyright (c) 2010, Mael |
---|
15 | All rights reserved. |
---|
16 | |
---|
17 | Redistribution and use in source and binary forms, with or without |
---|
18 | modification, are permitted provided that the following conditions are met: |
---|
19 | * Redistributions of source code must retain the above copyright |
---|
20 | notice, this list of conditions and the following disclaimer. |
---|
21 | * Redistributions in binary form must reproduce the above copyright |
---|
22 | notice, this list of conditions and the following disclaimer in the |
---|
23 | documentation and/or other materials provided with the distribution. |
---|
24 | * Neither the name of jiwigo nor the |
---|
25 | names of its contributors may be used to endorse or promote products |
---|
26 | derived from this software without specific prior written permission. |
---|
27 | |
---|
28 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
29 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
30 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
31 | DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY |
---|
32 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
33 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
34 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
35 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
36 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
37 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
38 | |
---|
39 | * @author mael |
---|
40 | * Cell renderer. Allows to display a specific image for a png or a jpg image |
---|
41 | * And allows to display "leaf" folders with a folder gui |
---|
42 | */ |
---|
43 | public class MyTreeCellRenderer extends DefaultTreeCellRenderer { |
---|
44 | |
---|
45 | public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, |
---|
46 | boolean leaf, int row, boolean hasFocus) { |
---|
47 | |
---|
48 | super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus); |
---|
49 | |
---|
50 | DefaultMutableTreeNode theNode = (DefaultMutableTreeNode) value; |
---|
51 | //only if it's a file : the value can be a string and it's not interesting |
---|
52 | if (theNode.getUserObject() instanceof File) { |
---|
53 | File file = (File) theNode.getUserObject(); |
---|
54 | //if it's a directory and a leaf, the default icon is overriden with the |
---|
55 | //"openIcon", which shows to the users that it's a folder. Otherwise it can be confusing |
---|
56 | //and if it's a png or jpg files (which means everything excepts the folders), specific icons |
---|
57 | //are setted |
---|
58 | if (file.isDirectory() && leaf) { |
---|
59 | setIcon(openIcon); |
---|
60 | } else if (file.isFile() && file.getName().toLowerCase().endsWith(".png")) { |
---|
61 | setIcon(new ImageIcon(Outil.getURL("fr/mael/jiwigo/img/png.png"))); |
---|
62 | } else if (file.isFile() |
---|
63 | && (file.getName().toLowerCase().endsWith(".jpg") || file.getName().toLowerCase().endsWith(".jpeg"))) { |
---|
64 | setIcon(new ImageIcon(Outil.getURL("fr/mael/jiwigo/img/jpg.png"))); |
---|
65 | } |
---|
66 | } |
---|
67 | return this; |
---|
68 | } |
---|
69 | |
---|
70 | } |
---|