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 javax.swing.ImageIcon;
12 import java.util.*;
13 import java.util.zip.*;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.File;
17 import org.freehep.util.images.ImageHandler;
18
19
20
21
22
23 class ZipTree extends DefaultMutableTreeNode
24 {
25 private Hashtable hash = new Hashtable();
26 private ZipFile zip;
27
28 ZipTree(ZipFile zip)
29 {
30 super(new ZipArchive(zip));
31 this.zip = zip;
32 Enumeration e = zip.entries();
33 while (e.hasMoreElements())
34 {
35 ZipEntry entry = (ZipEntry) e.nextElement();
36 String fullName = entry.getName();
37 DefaultMutableTreeNode parent = findParentNode(fullName);
38 if (entry.isDirectory())
39 {
40 DefaultMutableTreeNode node = new DefaultMutableTreeNode(new ZipDirectory(zip,fullName));
41 hash.put(fullName,node);
42 parent.add(node);
43 }
44 else
45 {
46 if (fullName.endsWith(".gif") || fullName.endsWith(".png") || fullName.endsWith(".jpg"))
47 {
48 Object dir = parent.getUserObject();
49 if (dir instanceof ZipDirectory) ((ZipDirectory) dir).addEntry(entry);
50 }
51 }
52 }
53
54 boolean changesWereMade = true;
55 while (changesWereMade)
56 {
57 changesWereMade = false;
58 for (DefaultMutableTreeNode node = getFirstLeaf(); node != null; )
59 {
60 DefaultMutableTreeNode next = node.getNextLeaf();
61 if (node != this)
62 {
63 ZipDirectory dir = (ZipDirectory) node.getUserObject();
64 if (dir.getNEntries() == 0)
65 {
66 node.removeFromParent();
67 changesWereMade = true;
68 }
69 }
70 node = next;
71 }
72 }
73 hash = null;
74 }
75 private DefaultMutableTreeNode findParentNode(String fullName)
76 {
77 String dirName = IconBrowser.dirName(fullName);
78 if (dirName == null) return this;
79 DefaultMutableTreeNode parent = (DefaultMutableTreeNode) hash.get(dirName);
80 if (parent == null)
81 {
82 DefaultMutableTreeNode pp = findParentNode(dirName);
83 parent = new DefaultMutableTreeNode(new ZipDirectory(zip,dirName));
84 hash.put(dirName,parent);
85 pp.add(parent);
86 }
87 return parent;
88 }
89 public String toString()
90 {
91 return IconBrowser.fileName(zip.getName(),File.separator);
92 }
93
94 private static class ZipArchive extends ZipDirectory implements IconArchive
95 {
96 ZipArchive(ZipFile file)
97 {
98 super(file,file.getName());
99 }
100 public void close()
101 {
102 try
103 {
104 getZip().close();
105 }
106 catch (IOException x) {}
107 }
108 }
109 private static class ZipDirectory implements IconDirectory
110 {
111 ZipDirectory(ZipFile file, String name)
112 {
113 this.file = file;
114 this.name = name;
115 }
116 public String getName()
117 {
118 return name;
119 }
120 void addEntry(ZipEntry entry)
121 {
122 entries.add(entry);
123 }
124 public String toString()
125 {
126 StringBuffer b = new StringBuffer(IconBrowser.fileName(name));
127 if (!entries.isEmpty())
128 {
129 int n = entries.size();
130 b.append(" (");
131 b.append(n);
132 if (n == 1) b.append(" entry)");
133 else b.append(" entries)");
134 }
135 return b.toString();
136 }
137 public int getNEntries()
138 {
139 return entries.size();
140 }
141 public String getEntryName(int index)
142 {
143 ZipEntry e = (ZipEntry) entries.get(index);
144 return e.getName();
145 }
146 public Icon getEntryIcon(int index)
147 {
148 ZipEntry e = (ZipEntry) entries.get(index);
149 return createIconFromZipEntry(file,e);
150 }
151 private Icon createIconFromZipEntry(ZipFile zip, ZipEntry entry)
152 {
153 try
154 {
155 InputStream in = zip.getInputStream(entry);
156 try
157 {
158 int len = (int) entry.getSize();
159 byte[] data = new byte[len];
160 for (int off = 0; len > 0; )
161 {
162 int l = in.read(data,off,len);
163 off += l;
164 len -= l;
165 }
166 return new ImageIcon(data,entry.getName());
167 }
168 finally
169 {
170 in.close();
171 }
172 }
173 catch (IOException x)
174 {
175 return ImageHandler.brokenIcon;
176 }
177 }
178 ZipFile getZip()
179 {
180 return file;
181 }
182 private String name;
183 private java.util.List entries = new ArrayList();
184 private ZipFile file;
185 }
186 }