Normand Briere
2019-07-27 1af7d3700724834e40ad8636bc9a56cdc3b19b15
cFileSystemModel.java
....@@ -1,10 +1,12 @@
11
22 import java.io.*;
3
+import javax.swing.Icon;
34 import javax.swing.event.*;
45 import javax.swing.tree.*;
56
67 class cFileSystemModel implements TreeModel
78 {
9
+
810 File root;
911 cFilter filter;
1012
....@@ -23,88 +25,175 @@
2325 {
2426 }
2527
26
- public Object getChild(Object parent, int index)
28
+ public Object getChild(Object parent, int index)
2729 {
2830 //new Exception().printStackTrace();
29
-
31
+
3032 //System.out.println("child[" + index + "] = " + ((File) parent).listFiles(filter)[index]);
31
-
33
+
3234 File[] files = table.get(parent);
33
- assert(files != null);
35
+ assert (files != null);
3436 //return ((File) parent).listFiles(filter)[index];
3537 return files[index];
36
-
37
- }
3838
39
+ }
3940 java.util.Hashtable<File, File[]> table = new java.util.Hashtable<File, File[]>();
40
-
41
- public int getChildCount(Object parent)
41
+
42
+ public int getChildCount(Object parent)
4243 {
43
- if( isLeaf(parent) )
44
+ if (isLeaf(parent))
45
+ {
4446 return 0;
45
- else
47
+ } else
4648 {
4749 //System.out.println(parent + " childcount = " + ((File) parent).listFiles(filter).length);
4850 File[] files = table.get(parent);
49
-
50
- if(files == null)
51
+
52
+ if (files == null)
5153 {
5254 files = ((File) parent).listFiles(filter);
5355 table.put(((File) parent), files);
5456 }
55
-
57
+
5658 return files.length;
5759 }
5860 }
5961
60
- public int getIndexOfChild(Object parent, Object child)
62
+ public int getIndexOfChild(Object parent, Object child)
6163 {
6264 assert false;
6365 return 0;
6466 }
6567
66
- public Object getRoot()
68
+ public Object getRoot()
6769 {
6870 return root;
6971 }
7072
71
- public boolean isLeaf(Object node)
73
+ public boolean isLeaf(Object node)
7274 {
7375 return !((File) node).isDirectory();
7476 }
7577
76
- public void removeTreeModelListener(TreeModelListener l)
78
+ public void removeTreeModelListener(TreeModelListener l)
7779 {
7880 }
7981
80
- public void valueForPathChanged(TreePath path, Object newValue)
82
+ public void valueForPathChanged(TreePath path, Object newValue)
8183 {
8284 }
8385
86
+ static java.util.Hashtable<String, javax.swing.ImageIcon> icons = new java.util.Hashtable<String, javax.swing.ImageIcon>();
87
+
88
+
8489 static class Renderer extends DefaultTreeCellRenderer
8590 {
86
- public Renderer() {
91
+
92
+ public Renderer()
93
+ {
8794 }
8895
8996 public java.awt.Component getTreeCellRendererComponent(
90
- cTree tree,
91
- Object value,
92
- boolean sel,
93
- boolean expanded,
94
- boolean leaf,
95
- int row,
96
- boolean hasFocus)
97
+ //cTree tree,
98
+ javax.swing.JTree tree,
99
+ Object value,
100
+ boolean sel,
101
+ boolean expanded,
102
+ boolean leaf,
103
+ int row,
104
+ boolean hasFocus)
97105 {
98
- System.out.println(value);
106
+ //System.out.println(value);
99107 super.getTreeCellRendererComponent(
100
- tree, value, sel,
101
- expanded, leaf, row,
102
- hasFocus);
108
+ tree, value, sel,
109
+ expanded, leaf, row,
110
+ hasFocus);
111
+
112
+ String valueString = value.toString();
113
+
114
+ if (valueString.toLowerCase().endsWith(".gfd") || valueString.toLowerCase().endsWith(".obj") || valueString.toLowerCase().endsWith(".3ds"))
115
+ {
116
+ if (true)
117
+ {
118
+ // Small icons
119
+ String valueTruncated = valueString.substring(0, valueString.length()-4);
120
+
121
+ //System.out.println("valueTruncated = " + valueTruncated);
122
+
123
+ javax.swing.ImageIcon rendererIcon = icons.get(valueTruncated);
124
+
125
+ if (rendererIcon == null)
126
+ {
127
+ if (new File(valueTruncated + ".jpg").exists())
128
+ {
129
+ rendererIcon = new javax.swing.ImageIcon(valueTruncated + ".jpg");
130
+ }
131
+ else
132
+ {
133
+ if (new File(valueTruncated + ".png").exists())
134
+ {
135
+ rendererIcon = new javax.swing.ImageIcon(valueTruncated + ".png");
136
+ }
137
+ }
138
+
139
+ if (rendererIcon == null)
140
+ {
141
+ rendererIcon = new javax.swing.ImageIcon();
142
+ }
143
+
144
+ icons.put(valueTruncated, rendererIcon);
145
+ }
146
+
147
+ setIcon(rendererIcon);
148
+ }
149
+ else
150
+ {
151
+ // Large icons
152
+ String[] split = valueString.split("/");
153
+
154
+ String valueTruncated = "";
155
+
156
+ for (int i=1; i<split.length-1; i++)
157
+ {
158
+ valueTruncated += "/" + split[i];
159
+ }
160
+
161
+ valueTruncated += "/icon.jpg";
162
+
163
+ System.out.println(valueTruncated);
164
+
165
+ javax.swing.ImageIcon rendererIcon = icons.get(valueTruncated);
166
+
167
+ if (rendererIcon == null)
168
+ {
169
+ if (new File(valueTruncated).exists())
170
+ {
171
+ rendererIcon = new javax.swing.ImageIcon(valueTruncated);
172
+ }
173
+
174
+ if (rendererIcon == null)
175
+ {
176
+ rendererIcon = new javax.swing.ImageIcon();
177
+ }
178
+
179
+ icons.put(valueTruncated, rendererIcon);
180
+ }
181
+
182
+ setIcon(rendererIcon);
183
+ }
184
+ }
185
+ else
186
+ {
187
+ //setIcon(null);
188
+// Icon systemIcon = javax.swing.filechooser.FileSystemView.getFileSystemView().getSystemIcon( new File(valueString) );
189
+// setIcon(systemIcon);
190
+ }
191
+
103192 setToolTipText("This book is in the Tutorial series.");
104193
105194 return this;
106195 }
107
-
196
+
108197 public void setText(String text)
109198 {
110199 //System.out.println(text);