Normand Briere
2019-11-07 f868664f7e7626f651e6ade9c9f6863851ef43b7
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,176 @@
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
+ String lcString = valueString.toLowerCase();
114
+
115
+ if (lcString.endsWith(".gfd") || lcString.endsWith(".obj") || lcString.endsWith(".3ds"))
116
+ {
117
+ if (true)
118
+ {
119
+ // Small icons
120
+ String valueTruncated = valueString.substring(0, valueString.length()-4);
121
+
122
+ //System.out.println("valueTruncated = " + valueTruncated);
123
+
124
+ javax.swing.ImageIcon rendererIcon = icons.get(valueTruncated);
125
+
126
+ if (rendererIcon == null)
127
+ {
128
+ if (new File(valueTruncated + ".jpg").exists())
129
+ {
130
+ rendererIcon = new javax.swing.ImageIcon(valueTruncated + ".jpg");
131
+ }
132
+ else
133
+ {
134
+ if (new File(valueTruncated + ".png").exists())
135
+ {
136
+ rendererIcon = new javax.swing.ImageIcon(valueTruncated + ".png");
137
+ }
138
+ }
139
+
140
+ if (rendererIcon == null)
141
+ {
142
+ rendererIcon = ObjEditor.GetIcon("icons/primitives.png");
143
+ }
144
+
145
+ icons.put(valueTruncated, rendererIcon);
146
+ }
147
+
148
+ setIcon(rendererIcon);
149
+ }
150
+ else
151
+ {
152
+ // Large icons
153
+ String[] split = valueString.split("/");
154
+
155
+ String valueTruncated = "";
156
+
157
+ for (int i=1; i<split.length-1; i++)
158
+ {
159
+ valueTruncated += "/" + split[i];
160
+ }
161
+
162
+ valueTruncated += "/icon.jpg";
163
+
164
+ System.out.println(valueTruncated);
165
+
166
+ javax.swing.ImageIcon rendererIcon = icons.get(valueTruncated);
167
+
168
+ if (rendererIcon == null)
169
+ {
170
+ if (new File(valueTruncated).exists())
171
+ {
172
+ rendererIcon = new javax.swing.ImageIcon(valueTruncated);
173
+ }
174
+
175
+ if (rendererIcon == null)
176
+ {
177
+ rendererIcon = new javax.swing.ImageIcon();
178
+ }
179
+
180
+ icons.put(valueTruncated, rendererIcon);
181
+ }
182
+
183
+ setIcon(rendererIcon);
184
+ }
185
+ }
186
+ else
187
+ {
188
+ //setIcon(null);
189
+// Icon systemIcon = javax.swing.filechooser.FileSystemView.getFileSystemView().getSystemIcon( new File(valueString) );
190
+// setIcon(systemIcon);
191
+ }
192
+
103193 setToolTipText("This book is in the Tutorial series.");
104194
105195 return this;
106196 }
107
-
197
+
108198 public void setText(String text)
109199 {
110200 //System.out.println(text);