KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > utils > gui > FileViewWithAttributes


1 package snow.utils.gui;
2
3 import java.io.File JavaDoc;
4 import java.awt.*;
5 import javax.swing.*;
6 import javax.swing.filechooser.*;
7
8 /** Used to customize a JFileChooser. Can change the appearing names and icons.
9 */

10 public class FileViewWithAttributes extends FileView
11 {
12   final static int fontSize = UIManager.getFont("Tree.font").getSize();
13   private final FileIcon fileIcon = new FileIcon(false, fontSize+2);
14   private final FileIcon dirIcon = new FileIcon(true, fontSize+2);
15
16   /**
17    * If .java file, add length to name
18    */

19   public String JavaDoc getName(File JavaDoc file)
20   {
21     String JavaDoc filename = file.getName();
22     if (filename.endsWith(".java"))
23     {
24        //filename += " : " + file.length();
25
return filename;
26     }
27     return null;
28   }
29
30   private boolean isRoot(File JavaDoc f)
31   {
32     File JavaDoc[] roots = File.listRoots();
33     for(int i=0; i<roots.length; i++)
34     {
35       if(roots[i].equals(f)) return true;
36     }
37     return false;
38   }
39
40   /**
41    * Return special icons for .java and .class files
42    */

43   public Icon getIcon(File JavaDoc file)
44   {
45     // without this test, the floppy and removables devices are asked each time
46
// from the system...
47
if(isRoot(file)) return null;
48
49     if (file.isDirectory())
50     {
51        if(file.canWrite() && file.canRead())
52        {
53          dirIcon.setType(FileIcon.IconColor.Green);
54          return dirIcon;
55        }
56        if(file.canRead())
57        {
58          dirIcon.setType(FileIcon.IconColor.Red);
59          return dirIcon;
60        }
61        dirIcon.setType(FileIcon.IconColor.RedGreen);
62        return dirIcon;
63     }
64
65     // file is not a directory
66

67     if(file.canWrite() && file.canRead())
68     {
69        fileIcon.setType(FileIcon.IconColor.Green);
70        return fileIcon;
71     }
72     if(file.canRead())
73     {
74        fileIcon.setType(FileIcon.IconColor.Red);
75        return fileIcon;
76     }
77
78     fileIcon.setType(FileIcon.IconColor.RedGreen);
79     return fileIcon;
80   }
81
82   /*test
83   public static void main(String args[]) {
84     SwingUtilities.invokeLater(new Runnable() {
85       public void run() {
86         JFileChooser fileChooser =
87                new JFileChooser(".");
88         FileViewWithAttributes view = new FileViewWithAttributes();
89         fileChooser.setFileView(view);
90         int status = fileChooser.showOpenDialog(null);
91         System.exit(0);
92       }
93     });
94   }*/

95 }
Popular Tags