KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > thinlet > common > FileChooser


1 package thinlet.common;
2
3 import java.awt.*;
4 import java.io.*;
5 import java.text.*;
6 import javax.swing.*;
7 import javax.swing.filechooser.*;
8 import thinlet.*;
9
10 public class FileChooser {
11
12     public static final int OPEN_DIALOG = 0;
13     public static final int SAVE_DIALOG = 1;
14     public static final int DIRECTORY_DIALOG = 2;
15     
16     private int type;
17     private Image openedicon, closedicon, fileicon;
18     private static View view;
19     {
20         if (view == null) {
21             try {
22                 view = new View2();
23             } catch (NoClassDefFoundError JavaDoc nfe) {
24                 view = new View();
25             }
26         }
27     }
28     
29     public FileChooser(Thinlet thinlet, int type) {
30         this.type = type;
31         Object JavaDoc dialog = Thinlet.create("dialog");
32         thinlet.setString(dialog, "text", "Choose directory");
33         thinlet.setInteger(dialog, "width", 240);
34         thinlet.setInteger(dialog, "height", 320);
35         thinlet.setBoolean(dialog, "resizable", true);
36         thinlet.setInteger(dialog, "top", -1);
37         thinlet.setInteger(dialog, "left", -1);
38         thinlet.setInteger(dialog, "bottom", -1);
39         thinlet.setInteger(dialog, "right", -1);
40         
41         Object JavaDoc tree = Thinlet.create("tree");
42         thinlet.setInteger(tree, "weightx", 1);
43         thinlet.setInteger(tree, "weighty", 1);
44         thinlet.setBoolean(tree, "angle", true);
45         thinlet.setBoolean(tree, "line", false);
46         thinlet.setMethod(tree, "expand", "expand(thinlet, this, item)", tree, this);
47         thinlet.setMethod(tree, "collapse", "collapse(thinlet, this, item)", tree, this);
48         
49         File[] roots = view.getRoots();
50         for (int i = 0; i < roots.length; i++) {
51             addNode(thinlet, tree, roots[i]);
52         }
53         
54         thinlet.add(dialog, tree);
55         thinlet.add(dialog);
56     }
57     
58     public void expand(Thinlet thinlet, Object JavaDoc tree, Object JavaDoc node) {
59             if (thinlet.getIcon(node, "icon") == closedicon) {
60                 if (openedicon == null) {
61                     openedicon = thinlet.getIcon("/icon/open.gif");
62                 }
63                 thinlet.setIcon(node, "icon", openedicon);
64             }
65         
66             if (thinlet.getProperty(node, "load") == Boolean.TRUE) {
67                 File file = (File) thinlet.getProperty(node, "file");
68                 File[] files = view.getFiles(file);
69                 thinlet.removeAll(node);
70                 for (int i = 0; i < files.length; i++) {
71                     addNode(thinlet, node, files[i]);
72                 }
73                 thinlet.putProperty(node, "load", null);
74             }
75         }
76     
77     public void collapse(Thinlet thinlet, Object JavaDoc tree, Object JavaDoc node) {
78         if (thinlet.getIcon(node, "icon") == openedicon) {
79             thinlet.setIcon(node, "icon", closedicon);
80         }
81     }
82     
83     private void addNode(Thinlet thinlet, Object JavaDoc node, File file) {
84         Object JavaDoc subnode = Thinlet.create("node");
85         thinlet.setString(subnode, "text", view.getText(file));
86         thinlet.setIcon(subnode, "icon", view.getIcon(thinlet, file));
87         thinlet.putProperty(subnode, "file", file);
88         thinlet.add(node, subnode);
89         if (file.isDirectory() || !file.isFile()) {
90             thinlet.setBoolean(subnode, "expanded", false);
91             thinlet.putProperty(subnode, "load", Boolean.TRUE);
92             Object JavaDoc loading = Thinlet.create("node");
93             thinlet.setString(loading, "text", "loading...");
94             thinlet.add(subnode, loading);
95         }
96     }
97     
98     private class View {
99         
100         public File[] getRoots() {
101             return new File[] { new File("C:\\") };
102         }
103         
104         public File[] getFiles(File file) {
105             String JavaDoc[] list = file.list();
106             int dircount = 0;
107             File[] files = new File[list.length];
108             for (int i = 0; i < list.length; i++) {
109                 files[i] = new File(file, list[i]);
110                 if (files[i].isDirectory()) { // separate directories and files
111
String JavaDoc swap = list[dircount]; list[dircount] = list[i]; list[i] = swap;
112                     File fwap = files[dircount]; files[dircount] = files[i]; files[i] = fwap;
113                     dircount++;
114                 }
115             }
116
117             Collator collator = Collator.getInstance();
118             collator.setStrength(Collator.SECONDARY);
119             for (int i = 0; i < list.length; i++) { // sort names
120
for (int j = i; (j > 0) && ((i < dircount) || (j > dircount)) &&
121                         (collator.compare(list[j - 1], list[j]) > 0); j--) {
122                     String JavaDoc swap = list[j]; list[j] = list[j - 1]; list[j - 1] = swap;
123                     File fwap = files[j]; files[j] = files[j - 1]; files[j - 1] = fwap;
124                 }
125             }
126             return files;
127         }
128         
129         public String JavaDoc getText(File file) {
130             return file.getName();
131         }
132         
133         public Image getIcon(Thinlet thinlet, File file) {
134             if (file.isDirectory()) {
135                 if (closedicon == null) { closedicon = thinlet.getIcon("/icon/folder.gif"); }
136                 return closedicon;
137             } else {
138                 if (fileicon == null) { fileicon = thinlet.getIcon("/icon/new.gif"); }
139                 return fileicon;
140             }
141         }
142     }
143     
144     private class View2 extends View {
145         
146         private FileSystemView view = FileSystemView.getFileSystemView();
147         
148         public File[] getRoots() {
149             return view.getRoots();
150         }
151         
152         public File[] getFiles(File file) {
153             return view.getFiles(file, true);
154         }
155         
156         public String JavaDoc getText(File file) {
157             return view.getSystemDisplayName(file);
158         }
159         
160         public Image getIcon(Thinlet thinlet, File file) {
161             Icon icon = view.getSystemIcon(file);
162             return (icon instanceof ImageIcon) ?
163                 ((ImageIcon) icon).getImage() : null;
164         }
165     }
166 }
Popular Tags