📄 filesystemmodel.java
字号:
package org.net9.oops.jsee;//import javax.swing.*;import javax.swing.tree.*;import java.io.*;public class FileSystemModel extends AbstractTreeModel implements Serializable { String root; FileFilter filter; File[] children; File tempParent = null; public class MyFile extends File { public MyFile(String pathname) { super(pathname); } public File[] listFiles(FileFilter filter) { //return excludeNotReadyDrive(File.listRoots()); String osName = System.getProperty("os.name","."); if (osName.startsWith("Windows")) return File.listRoots(); else // only support Unix at the moment return super.listFiles(filter); } }; public FileSystemModel() { root = "/"; //System.getProperty( "user.home" ); filter = new FileFilter() { public boolean accept(File pathname) { return pathname.isDirectory(); } }; } public FileSystemModel( String startPath ) { root = startPath; } public Object getRoot() { return new MyFile( root ); } public Object getChild( Object parent, int index ) { if (tempParent == null || !tempParent.equals((File)parent)) { tempParent = (File)parent; try { children = tempParent.listFiles(filter); } catch (Exception ex) {return null;} } return children[index]; } public int getChildCount( Object parent ) { if (tempParent == null || !tempParent.equals((File)parent)) { tempParent = (File)parent; if ( tempParent.isDirectory() ) { //Modified by Neoya 2002-03-06 try { children = tempParent.listFiles(filter); return children.length; } catch (Exception ex) { return 0; } } else { return 0; } } else { if ( tempParent.isDirectory() ) { if (children!=null) return children.length; else return 0; } else { return 0; } } } public boolean isLeaf( Object node ) { // Check if there is any file under node // Cast node to directory, list file in that directory to an array and compare array length //return ( ((File)node).listFiles(filter).length == 0 )? true:false; // To speed up - fast is better than look good return false; } public void valueForPathChanged( TreePath path, Object newValue ) { } public int getIndexOfChild( Object parent, Object child ) { if (tempParent == null || !tempParent.equals((File)parent)) { tempParent = (File)parent; children = tempParent.listFiles(filter); } File fileSysEntity = (File)child; int result = -1; for ( int i = 0; i < children.length; ++i ) { if ( fileSysEntity.getName().equals( children[i] ) ) { result = i; break; } } return result; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -