1 21 22 package org.armedbear.j; 23 24 import java.util.Arrays ; 25 import java.util.Comparator ; 26 import java.util.Vector ; 27 import javax.swing.tree.DefaultMutableTreeNode ; 28 import javax.swing.tree.DefaultTreeModel ; 29 import javax.swing.tree.TreeNode ; 30 31 public class DirectoryTreeModel extends DefaultTreeModel 32 { 33 private static final boolean ignoreCase = Platform.isPlatformWindows(); 34 35 private static DirectoryTreeModel localTreeModel; 36 private static Vector remoteModels; 37 38 private File rootFile; 40 private DirectoryTreeModel(TreeNode root) 41 { 42 super(root); 43 } 44 45 private DirectoryTreeModel(TreeNode root, File f) 46 { 47 super(root); 48 rootFile = f; 49 } 50 51 private File getRootFile() 52 { 53 return rootFile; 54 } 55 56 private static DirectoryTreeModel getLocalDirectoryTreeModel(File file) 57 { 58 if (localTreeModel == null) 59 setLocalRoot(scanRoot(file)); 60 return localTreeModel; 61 } 62 63 public static DirectoryTreeModel getTreeModel(File file) 64 { 65 if (file.isLocal()) 66 return getLocalDirectoryTreeModel(file); 67 DirectoryTreeModel model; 68 File rootFile = file.getRoot(); 69 if (remoteModels != null) { 70 for (int i = 0; i < remoteModels.size(); i++) { 71 model = (DirectoryTreeModel) remoteModels.get(i); 72 if (rootFile.equals(model.getRootFile())) 73 return model; 74 } 75 } 76 DefaultMutableTreeNode root = 77 new DefaultMutableTreeNode (new DirectoryTreeElement(rootFile)); 78 model = new DirectoryTreeModel(root, rootFile); 79 addChildren(rootFile, root); 80 if (remoteModels == null) 81 remoteModels = new Vector (); 82 remoteModels.add(model); 83 return model; 84 } 85 86 private static synchronized void setLocalRoot(DefaultMutableTreeNode root) 87 { 88 if (localTreeModel != null) 89 localTreeModel.setRoot(root); 90 else 91 localTreeModel = new DirectoryTreeModel(root); 92 } 93 94 public void rescan(File file) 95 { 96 setRoot(scanRoot(file)); 97 } 98 99 private static DefaultMutableTreeNode scanRoot(File file) 100 { 101 DefaultMutableTreeNode root = null; 102 if (Platform.isPlatformWindows()) { 103 File[] roots = File.listRoots(); 104 if (roots != null) { 105 root = new DefaultMutableTreeNode ("Local"); 106 for (int i = 0; i < roots.length; i++) { 107 DefaultMutableTreeNode child = 108 new DefaultMutableTreeNode (new DirectoryTreeElement(roots[i])); 109 root.insert(child, root.getChildCount()); 110 if (roots[i].equals(file.getRoot())) 111 addChildren(roots[i], child); 112 } 113 } else { 114 root = new DefaultMutableTreeNode (new DirectoryTreeElement(file.getRoot())); 115 addChildren(file.getRoot(), root); 116 } 117 } else { 118 File f = file.getRoot(); 120 root = new DefaultMutableTreeNode (new DirectoryTreeElement(file.getRoot())); 121 addChildren(f, root); 122 } 123 return root; 124 } 125 126 private static void addChildren(File parent, DefaultMutableTreeNode node) 127 { 128 File[] list = parent.listFiles(); 129 if (list == null) 130 return; 131 Arrays.sort(list, 132 ignoreCase ? ciFileNameComparator : csFileNameComparator); 133 for (int i = 0; i < list.length; i++) { 134 File f = list[i]; 135 if (f.isDirectory() && !f.isLink()) { 136 DefaultMutableTreeNode child = 137 new DefaultMutableTreeNode (new DirectoryTreeElement(f)); 138 node.insert(child, node.getChildCount()); 139 } 140 } 141 } 142 143 public DefaultMutableTreeNode getNode(File file) 144 { 145 return getNode((DefaultMutableTreeNode ) getRoot(), file); 146 } 147 148 private static DefaultMutableTreeNode getNode(DefaultMutableTreeNode root, File file) 149 { 150 if (root == null || file == null) 151 return null; 152 DefaultMutableTreeNode currentNode = root; 153 while (true) { 154 DefaultMutableTreeNode node = findMatchingChild(currentNode, file); 155 if (node == null) 156 return null; 157 DirectoryTreeElement treeElement = 158 (DirectoryTreeElement) node.getUserObject(); 159 File f = treeElement.getFile(); 160 expandNode(node, f); 161 if (file.canonicalPath().equals(f.canonicalPath())) 162 return node; 163 currentNode = node; 164 } 165 } 166 167 private static DefaultMutableTreeNode findMatchingChild( 169 DefaultMutableTreeNode parent, File file) 170 { 171 if (file != null) { 172 for (int i = 0; i < parent.getChildCount(); i++) { 173 DefaultMutableTreeNode node = 174 (DefaultMutableTreeNode ) parent.getChildAt(i); 175 DirectoryTreeElement treeElement = 176 (DirectoryTreeElement) node.getUserObject(); 177 File f = treeElement.getFile(); 178 if (file.canonicalPath().equals(f.canonicalPath())) 179 return node; 180 String prefix = f.canonicalPath(); 181 if (!prefix.endsWith(f.getSeparator())) 182 prefix += f.getSeparator(); 183 if (file.canonicalPath().startsWith(prefix)) 184 return node; 185 } 186 } 187 return null; 188 } 189 190 public static void expandNode(DefaultMutableTreeNode node, File file) 191 { 192 if (node.getChildCount() == 0) { 193 File[] list = file.listFiles(); 194 if (list == null) 195 return; 196 Arrays.sort(list, 197 ignoreCase ? ciFileNameComparator : csFileNameComparator); 198 for (int i = 0; i < list.length; i++) { 199 File f = list[i]; 200 if (f.isLink()) 201 continue; 202 else if (f.isDirectory()) { 203 DefaultMutableTreeNode child = 204 new DefaultMutableTreeNode (new DirectoryTreeElement(f)); 205 node.insert(child, node.getChildCount()); 206 } 207 } 208 } 209 } 210 211 private final static Comparator csFileNameComparator = new Comparator () { 213 public int compare(Object o1, Object o2) 214 { 215 String name1 = ((File)o1).getName(); 216 String name2 = ((File)o2).getName(); 217 return name1.compareTo(name2); 218 } 219 }; 220 221 private final static Comparator ciFileNameComparator = new Comparator () { 223 public int compare(Object o1, Object o2) 224 { 225 String name1 = ((File)o1).getName(); 226 String name2 = ((File)o2).getName(); 227 return name1.compareToIgnoreCase(name2); 228 } 229 }; 230 } 231 | Popular Tags |