📄 virtualfilesystemnode.java
字号:
package jcdwriter.model;import javax.swing.tree.*;import java.util.*;import java.io.File;/* Author: Ram Mallappa * */public class VirtualFileSystemNode extends DefaultMutableTreeNode implements Comparator{ boolean isVirtualFileSystemRoot = false; String realPath; HashSet files; HashSet deletedFiles; boolean isCreatedDirectory = false; public VirtualFileSystemNode(String name) { super(name); } public void setIsCreatedDirectory(boolean aTrueOrFalse){ isCreatedDirectory = aTrueOrFalse; } public boolean getIsCreatedDirectory(){ return isCreatedDirectory; } public VirtualFileSystemNode(String aName, String aRealPath) { this(aName); realPath = aRealPath; } public boolean isVirtualFileSystemRoot() { return isVirtualFileSystemRoot; } public boolean isGraftPoint(){ return this.realPath() != null; } public boolean isCreatedDirectory(){ return isCreatedDirectory; } public void isCreatedDirectory(boolean aTrueOrFalse){ isCreatedDirectory = aTrueOrFalse; } public void isVirtualFileSystemRoot(boolean aFlag) { isVirtualFileSystemRoot = aFlag; } public String name(){ return((String)getUserObject()); } public HashSet files(){ if(files != null) return files; else return new HashSet(); } public void deleteNode(VirtualFileSystemNode aNode){ /* A node being deleted may be: 1. A subdirectory (or sub-sub directory) of a grafted node: - Make grafted node as created node - Graft its children (leave those grafted by user alone) - delete aNode 2. A node created on fly (on jcdwriter): Drop aNode from tree 3. A grafted node itself Drop aNode from tree */ if (!(aNode.isGraftPoint() || aNode.isCreatedDirectory())) { /* case 1 above */ Enumeration childrenColl; childrenColl = children(); VirtualFileSystemNode nodeToReGraft; /* We need to figure out the path until receiver. Then attach reciever's userObject * then attach aNode's userObject and use this as prefix while creating realPath for * children of aNode */ StringBuffer aRealPathStr = new StringBuffer(); TreeNode[] aPath = this.getPath(); /* aPath is upto root, find the index of one that has realPath set while searching * backwards */ int index = aPath.length - 1; while (((VirtualFileSystemNode)aPath[index]).realPath() == null) { index--; } /* Now get the realPath at index and add names of nodes below it until receiver to get * realPath for children of aNode (one being deleted) */ aRealPathStr.append(((VirtualFileSystemNode)aPath[index]).realPath()); /* after finding a graft point (one with realPath) remove it's realPath as we are * going to regraft it's children. Mark it as a created directory */ ((VirtualFileSystemNode)aPath[index]).realPath(null); ((VirtualFileSystemNode)aPath[index]).isCreatedDirectory(true); for (int i = index + 1; i < aPath.length; i++) { aRealPathStr.append("/"); aRealPathStr.append(((VirtualFileSystemNode)aPath[i]).name()); } // aRealPathStr.append("/"); // aRealPathStr.append(aNode.name()); String parentsRealPath = aRealPathStr.toString(); while(childrenColl.hasMoreElements()) { nodeToReGraft = (VirtualFileSystemNode)childrenColl.nextElement(); System.out.print("Regrafted: " + nodeToReGraft.getUserObject() + " to " ); nodeToReGraft.realPath(parentsRealPath + "/\"" + nodeToReGraft.getUserObject().toString() + "\""); System.out.println(nodeToReGraft.realPath()); }; if (files != null) { Iterator i = files.iterator(); while (i.hasNext()) { nodeToReGraft = (VirtualFileSystemNode)i.next(); nodeToReGraft.realPath(parentsRealPath + "/\"" + nodeToReGraft.getUserObject().toString() + "\""); Log.writeln("VFSNode", nodeToReGraft.getUserObject() + " to " + nodeToReGraft.realPath(), 5); } } realPath(null); this.isCreatedDirectory(true); } /* case 2 and 3 above do just this */ VirtualFileSystemNode aParent = ((VirtualFileSystemNode)aNode.getParent()); aParent.remove(aNode); aNode.removeFromParent(); } public String realPath() { return (realPath); } public void realPath(String aPath) { realPath = aPath; } private void addFileNode(VirtualFileSystemNode aNewNode) { if (files == null) files = new HashSet(); if (containsFileMatching(aNewNode)){ // System.out.println(aNewNode + " already present" ); } else { files.add(aNewNode); } } private void addDeletedNode(VirtualFileSystemNode aNewNode) { if (deletedFiles == null) deletedFiles = new HashSet(); deletedFiles.add(aNewNode); } private boolean containsFileMatching(VirtualFileSystemNode aNode) { Iterator it = files().iterator(); while (it.hasNext()) { if (equals(((VirtualFileSystemNode)it.next()).getUserObject())) return true; } return false; } public String toString() { return(super.toString()); } public void addDummyChild() { this.add(new VirtualFileSystemNode("Loading...")); } private ArrayList contentNodesForDir(File dir) { ArrayList list = new ArrayList(); VirtualFileSystemNode node; File contents[] = dir.listFiles(); if (contents != null) { for(int i = 0; i < contents.length; i++) { if(contents[i].isDirectory() && !(contents[i].isHidden())) { node = new VirtualFileSystemNode(contents[i].getName(), contents[i].getAbsolutePath()); node.add(new VirtualFileSystemNode("Loading...")); list.add(node); } } } return list; } public long fileSystemSize(String prefix){ /* Report the file system size starting at 'this' as root */ long totalSize = 0; File f; Enumeration childrenColl = children(); String newPrefix; if (this.isVirtualFileSystemRoot() || this.isCreatedDirectory()) { if (files != null) { Iterator i = files.iterator(); while (i.hasNext()) { totalSize = totalSize + ((VirtualFileSystemNode)i.next()).fileSystemSize(null); } } } else { if (this.realPath() == null) { int i = 1 + 9; //for putting break points. } if (prefix == null) f = new File(this.realPath()); else f = new File(prefix + "/" + (String)getUserObject()); if(f.isDirectory()){ if (files != null) { Iterator i = files.iterator(); while (i.hasNext()) { totalSize = totalSize + ((VirtualFileSystemNode)i.next()).fileSystemSize(f.getAbsolutePath()); } } } else{ totalSize = totalSize + f.length(); } } if (this.realPath() == null) { if (prefix == null) newPrefix = null; else newPrefix = prefix + "/" + getUserObject(); } else { newPrefix = this.realPath(); } while (childrenColl.hasMoreElements()){ totalSize = totalSize + ((VirtualFileSystemNode)childrenColl.nextElement()).fileSystemSize(newPrefix); } // System.out.println(totalSize); return totalSize; } public void printTree(int depth){ /* Print if this node is graft root. And ask the children to do the same.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -