Normand Briere
2019-07-01 9079880942d022ee32fd3a543843e132e52deb1a
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,136 @@
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
+ String valueTruncated = valueString.substring(0, valueString.length()-4);
117
+
118
+ System.out.println(valueTruncated);
119
+
120
+ javax.swing.ImageIcon rendererIcon = icons.get(valueTruncated);
121
+
122
+ if (rendererIcon == null)
123
+ {
124
+ if (new File(valueTruncated + ".jpg").exists())
125
+ {
126
+ rendererIcon = new javax.swing.ImageIcon(valueTruncated + ".jpg");
127
+ }
128
+ else
129
+ {
130
+ if (new File(valueTruncated + ".png").exists())
131
+ {
132
+ rendererIcon = new javax.swing.ImageIcon(valueTruncated + ".png");
133
+ }
134
+ }
135
+
136
+ if (rendererIcon == null)
137
+ {
138
+ rendererIcon = new javax.swing.ImageIcon();
139
+ }
140
+
141
+ icons.put(valueTruncated, rendererIcon);
142
+ }
143
+
144
+ setIcon(rendererIcon);
145
+ }
146
+ else
147
+ {
148
+ //setIcon(null);
149
+// Icon systemIcon = javax.swing.filechooser.FileSystemView.getFileSystemView().getSystemIcon( new File(valueString) );
150
+// setIcon(systemIcon);
151
+ }
152
+
103153 setToolTipText("This book is in the Tutorial series.");
104154
105155 return this;
106156 }
107
-
157
+
108158 public void setText(String text)
109159 {
110160 //System.out.println(text);