01: package snow.utils.gui;
02:
03: import java.io.File;
04: import java.awt.*;
05: import javax.swing.*;
06: import javax.swing.filechooser.*;
07:
08: /** Used to customize a JFileChooser. Can change the appearing names and icons.
09: */
10: public class FileViewWithAttributes extends FileView {
11: final static int fontSize = UIManager.getFont("Tree.font")
12: .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: @Override
20: public String getName(File file) {
21: String filename = file.getName();
22: if (filename.endsWith(".java")) {
23: //filename += " : " + file.length();
24: return filename;
25: }
26: return null;
27: }
28:
29: private boolean isRoot(File f) {
30: File[] roots = File.listRoots();
31: for (int i = 0; i < roots.length; i++) {
32: if (roots[i].equals(f))
33: return true;
34: }
35: return false;
36: }
37:
38: /**
39: * Return special icons for .java and .class files
40: */
41: @Override
42: public Icon getIcon(File file) {
43: // without this test, the floppy and removables devices are asked each time
44: // from the system...
45: if (isRoot(file))
46: return null;
47:
48: if (file.isDirectory()) {
49: if (file.canWrite() && file.canRead()) {
50: dirIcon.setType(FileIcon.IconColor.Green);
51: return dirIcon;
52: }
53: if (file.canRead()) {
54: dirIcon.setType(FileIcon.IconColor.Red);
55: return dirIcon;
56: }
57: dirIcon.setType(FileIcon.IconColor.RedGreen);
58: return dirIcon;
59: }
60:
61: // file is not a directory
62:
63: if (file.canWrite() && file.canRead()) {
64: fileIcon.setType(FileIcon.IconColor.Green);
65: return fileIcon;
66: }
67: if (file.canRead()) {
68: fileIcon.setType(FileIcon.IconColor.Red);
69: return fileIcon;
70: }
71:
72: fileIcon.setType(FileIcon.IconColor.RedGreen);
73: return fileIcon;
74: }
75:
76: /*test
77: public static void main(String args[]) {
78: SwingUtilities.invokeLater(new Runnable() {
79: public void run() {
80: JFileChooser fileChooser =
81: new JFileChooser(".");
82: FileViewWithAttributes view = new FileViewWithAttributes();
83: fileChooser.setFileView(view);
84: int status = fileChooser.showOpenDialog(null);
85: System.exit(0);
86: }
87: });
88: }*/
89: }
|