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