1
2
3
4
5
6
7 package org.freehep.demo.iconbrowser;
8
9 import javax.swing.tree.*;
10 import javax.swing.Icon;
11 import java.util.*;
12 import java.io.*;
13 import org.freehep.util.images.ImageHandler;
14
15
16
17
18
19
20 public class ZipListTree extends DefaultMutableTreeNode
21 {
22
23 public ZipListTree(String name) throws IOException
24 {
25 super(new ZipArchive("builtin:/"+name));
26 Hashtable hash = new Hashtable();
27 String resource = "/"+name+".list";
28 InputStream in = getClass().getResourceAsStream(resource);
29 if (in == null) throw new IOException("Cannot open "+resource);
30 LineNumberReader reader = new LineNumberReader(new InputStreamReader(in));
31 for (;;)
32 {
33 String line = reader.readLine();
34 if (line == null || line.length() == 0) break;
35 String dirName = IconBrowser.dirName(line);
36 DefaultMutableTreeNode parent = dirName == null ? this : (DefaultMutableTreeNode) hash.get(dirName);
37 if (line.endsWith("/"))
38 {
39 DefaultMutableTreeNode node = new DefaultMutableTreeNode(new ZipDirectory(line));
40 hash.put(line,node);
41 parent.add(node);
42 }
43 else
44 {
45 Object dir = parent.getUserObject();
46 if (dir instanceof ZipDirectory) ((ZipDirectory) dir).addEntry(line);
47 }
48 }
49 }
50 private static class ZipArchive extends ZipDirectory implements IconArchive
51 {
52 ZipArchive(String name)
53 {
54 super(name);
55 }
56 public void close()
57 {
58 }
59 }
60 private static class ZipDirectory implements IconDirectory
61 {
62 ZipDirectory(String name)
63 {
64 this.name = name;
65 }
66 public String getName()
67 {
68 return name;
69 }
70 void addEntry(String name)
71 {
72 entries.add(name);
73 }
74 public String toString()
75 {
76 StringBuffer b = new StringBuffer(IconBrowser.fileName(name));
77 if (!entries.isEmpty())
78 {
79 int n = entries.size();
80 b.append(" (");
81 b.append(n);
82 if (n == 1) b.append(" entry)");
83 else b.append(" entries)");
84 }
85 return b.toString();
86 }
87 public int getNEntries()
88 {
89 return entries.size();
90 }
91 public String getEntryName(int index)
92 {
93 return entries.get(index).toString();
94 }
95 public Icon getEntryIcon(int index)
96 {
97 String name = entries.get(index).toString();
98 return ImageHandler.getIcon(getClass().getResource("/"+name));
99 }
100 private String name;
101 private java.util.List entries = new ArrayList();
102 }
103 }