KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > thinlet > drafts > FolderBrowser


1 package thinlet.drafts;
2
3 import java.awt.*;
4 import java.io.*;
5 import java.text.*;
6 import thinlet.*;
7
8 public class FolderBrowser {
9     
10     private Image openedicon, closedicon, fileicon;
11     
12     public void init(Thinlet thinlet, Object JavaDoc tree) {
13         //System.out.println(System.getProperty("user.home") + " " + File.separatorChar);
14
addNode(thinlet, tree, "C:", true);
15     }
16     
17     public void expand(Thinlet thinlet, Object JavaDoc tree, Object JavaDoc node) {
18         if (thinlet.getIcon(node, "icon") == closedicon) {
19             if (openedicon == null) {
20                 openedicon = thinlet.getIcon("/icon/open.gif");
21             }
22             thinlet.setIcon(node, "icon", openedicon);
23         }
24         
25         if (thinlet.getProperty(node, "load") == Boolean.TRUE) {
26             String JavaDoc path = "";
27             for (Object JavaDoc item = node; item != tree; item = thinlet.getParent(item)) {
28                 path = thinlet.getString(item, "text") + File.separatorChar + path;
29             }
30             String JavaDoc[] list = new File(path).list();
31             
32             int dircount = 0;
33             for (int i = 0; i < list.length; i++) { // separate directories and files
34
if (new File(path, list[i]).isDirectory()) {
35                     String JavaDoc swap = list[dircount]; list[dircount] = list[i]; list[i] = swap;
36                     dircount++;
37                 }
38             }
39             
40             Collator collator = Collator.getInstance();
41             collator.setStrength(Collator.SECONDARY);
42             for (int i = 0; i < list.length; i++) { // sort names
43
for (int j = i; (j > 0) && ((i < dircount) || (j > dircount)) &&
44                         (collator.compare(list[j - 1], list[j]) > 0); j--) {
45                     String JavaDoc swap = list[j]; list[j] = list[j - 1]; list[j - 1] = swap;
46                 }
47             }
48             
49             thinlet.removeAll(node);
50             for (int i = 0; i < list.length; i++) {
51                 addNode(thinlet, node, list[i], i < dircount);
52             }
53             thinlet.putProperty(node, "load", null);
54         }
55     }
56     
57     public void collapse(Thinlet thinlet, Object JavaDoc node) {
58         if (thinlet.getIcon(node, "icon") == openedicon) {
59             thinlet.setIcon(node, "icon", closedicon);
60         }
61     }
62     
63     private void addNode(Thinlet thinlet, Object JavaDoc node, String JavaDoc text, boolean directory) {
64         Object JavaDoc subnode = Thinlet.create("node");
65         thinlet.setString(subnode, "text", text);
66         if (directory) {
67             if (closedicon == null) { closedicon = thinlet.getIcon("/icon/folder.gif"); }
68             thinlet.setIcon(subnode, "icon", closedicon);
69         } else {
70             if (fileicon == null) { fileicon = thinlet.getIcon("/icon/new.gif"); }
71             thinlet.setIcon(subnode, "icon", fileicon);
72         }
73         thinlet.add(node, subnode);
74         if (directory) {
75             thinlet.setBoolean(subnode, "expanded", false);
76             thinlet.putProperty(subnode, "load", Boolean.TRUE);
77             Object JavaDoc loading = Thinlet.create("node");
78             thinlet.setString(loading, "text", "loading...");
79             thinlet.add(subnode, loading);
80         }
81     }
82 }
Popular Tags