1 20 21 import java.io.File ; 22 import javax.swing.tree.DefaultMutableTreeNode ; 23 import javax.swing.JOptionPane ; 24 25 26 31 32 class MyTreeNode extends DefaultMutableTreeNode { 33 private static String osName = System.getProperty("os.name").toLowerCase(); 34 private boolean explored = false; 35 36 private void errorDisplayingPermission() { 37 JOptionPane.showMessageDialog(null, 38 "You do not have the permissions necessary to view the contents of \"" 39 + getFile() + "\"", "Error Displaying Folder", 40 JOptionPane.ERROR_MESSAGE); 41 } 42 43 private void errorDisplayingNoDisk() { 44 File currentFile = getFile(); 45 String fileName = currentFile.getName(); 46 String errorMsg = 47 "The file : " 48 + fileName 49 + " does not exist any" 50 + " longer!"; 51 if (osName.startsWith("windows")) { 52 File parentFile = currentFile.getParentFile(); 54 if (currentFile.getParentFile() == null) { 55 errorMsg = "Please insert disk into driver: " + currentFile; 57 } 58 } 59 JOptionPane.showMessageDialog( 60 null, 61 errorMsg, 62 "Error Displaying Folder", 63 JOptionPane.ERROR_MESSAGE); 64 } 65 66 public MyTreeNode(File file) { 68 setUserObject(file); 69 } 70 71 public boolean getAllowsChildren() { 72 return isDirectory(); 73 } 74 75 public boolean isLeaf() { 76 return !isDirectory(); 77 } 78 79 public File getFile() { 80 return (File ) getUserObject(); 81 } 82 83 public boolean isExplored() { 84 return explored; 85 } 86 87 public boolean isDirectory() { 88 File file = getFile(); 89 90 return file.isDirectory(); 91 } 92 93 public void explore() { 94 File file = getFile(); 95 96 if (!isDirectory()) { 97 return; 98 } 99 100 if (!file.exists()) { 102 errorDisplayingNoDisk(); 103 return; 104 } 105 106 if (!file.canRead()) { 108 errorDisplayingPermission(); 109 return; 110 } 111 112 if (!isExplored()) { 113 File [] children = file.listFiles(); 114 115 if (children != null) { 116 for (int i = 0; i < children.length; ++i) { 117 if (children[i].isDirectory()) { 118 add(new MyTreeNode(children[i])); 119 } 120 } 121 } 122 123 explored = true; 124 } 125 } 126 127 public String toString() { 128 File file = (File ) getUserObject(); 129 String filename = file.toString(); 130 131 int index = filename.lastIndexOf(File.separator); 132 133 return (index != -1 && index != filename.length() - 1) 134 ? filename.substring(index + 1) 135 : filename; 136 } 137 138 141 public int getChildrenCount() { 142 File file = getFile(); 143 144 if (!file.exists()) { 146 errorDisplayingNoDisk(); 147 return 0; 148 } 149 150 if (!file.canRead()) { 151 errorDisplayingPermission(); 152 return 0; 153 } 154 if (!isDirectory()) { 155 return 0; 156 } else { 157 File [] children = file.listFiles(); 158 159 return (children != null) ? children.length : 0; 160 } 161 } 162 163 166 public long getSize() { 167 File file = getFile(); 168 169 if (!file.canRead()) { 170 return 0; 171 } 172 173 if (!isDirectory()) { 174 return (file.length()); 175 } 176 177 File [] children = file.listFiles(); 178 179 long size = 0; 180 181 if (children != null) { 182 for (int i = 0; i < children.length; ++i) { 183 size += children[i].length(); 184 } 185 } 186 187 return size; 188 } 189 } 190 | Popular Tags |