Java实现的Windows资源管理器实例
本文实例讲述了Java实现的Windows资源管理器。分享给大家供大家参考。具体如下:
FileTree.java文件如下:
//FileTree.java
/***********************************************************
*Author:Jason
*email:tl21cen@hotmail.com
*CSDNblog:http://blog.csdn.net/UnAgain/
***********************************************************/
packagetl.exercise.swing;
importjava.awt.Component;
importjava.io.File;
importjava.util.Vector;
importjavax.swing.Icon;
importjavax.swing.JTree;
importjavax.swing.event.TreeExpansionEvent;
importjavax.swing.event.TreeExpansionListener;
importjavax.swing.event.TreeModelListener;
importjavax.swing.event.TreeSelectionEvent;
importjavax.swing.event.TreeSelectionListener;
importjavax.swing.filechooser.FileSystemView;
importjavax.swing.tree.DefaultTreeCellRenderer;
importjavax.swing.tree.TreeModel;
importjavax.swing.tree.TreePath;
publicclassFileTreeextendsJTree{
staticfinallongserialVersionUID=0;
privateFileListtheList;
publicFileTree(FileListlist){
theList=list;
setModel(newFileSystemModel(newFolderNode()));
this.setCellRenderer(newFolderRenderer());
addTreeSelectionListener(newTreeSelectionListener(){
publicvoidvalueChanged(TreeSelectionEventtse){
}
});
this.setSelectionRow(0);
}
publicvoidfireValueChanged(TreeSelectionEventtse){
TreePathtp=tse.getNewLeadSelectionPath();
Objecto=tp.getLastPathComponent();
//theList.fireTreeSelectionChanged((PathNode)o);
theList.fireTreeSelectionChanged((FolderNode)o);
}
publicvoidfireTreeCollapsed(TreePathpath){
super.fireTreeCollapsed(path);
TreePathcurpath=getSelectionPath();
if(path.isDescendant(curpath)){
setSelectionPath(path);
}
}
publicvoidfireTreeWillExpand(TreePathpath){
System.out.println("Pathwillexpandis"+path);
}
publicvoidfireTreeWillCollapse(TreePathpath){
System.out.println("Pathwillcollapseis"+path);
}
classExpansionListenerimplementsTreeExpansionListener{
FileTreetree;
publicExpansionListener(FileTreeft){
tree=ft;
}
publicvoidtreeCollapsed(TreeExpansionEventtee){
}
publicvoidtreeExpanded(TreeExpansionEventtee){
}
}
}
classFileSystemModelimplementsTreeModel{
I_fileSystemtheRoot;
charfileType=I_fileSystem.DIRECTORY;
publicFileSystemModel(I_fileSystemfs){
theRoot=fs;
}
publicObjectgetRoot(){
returntheRoot;
}
publicObjectgetChild(Objectparent,intindex){
return((I_fileSystem)parent).getChild(fileType,index);
}
publicintgetChildCount(Objectparent){
return((I_fileSystem)parent).getChildCount(fileType);
}
publicbooleanisLeaf(Objectnode){
return((I_fileSystem)node).isLeaf(fileType);
}
publicintgetIndexOfChild(Objectparent,Objectchild){
return((I_fileSystem)parent).getIndexOfChild(fileType,child);
}
publicvoidvalueForPathChanged(TreePathpath,ObjectnewValue){
}
publicvoidaddTreeModelListener(TreeModelListenerl){
}
publicvoidremoveTreeModelListener(TreeModelListenerl){
}
}
interfaceI_fileSystem{
finalpublicstaticcharDIRECTORY='D';
finalpublicstaticcharFILE='F';
finalpublicstaticcharALL='A';
publicIcongetIcon();
publicI_fileSystemgetChild(charfileType,intindex);
publicintgetChildCount(charfileType);
publicbooleanisLeaf(charfileType);
publicintgetIndexOfChild(charfileType,Objectchild);
}
/**
*AdatamodelforaJTree.Thismodelexplorerwindowsfilesystemdirectly.
*
*<p>
*Perhapsthereisafatalbugwiththisdesign.Forspeed,eachofinstances
*ofthismodelcontainsfileobjectsofsubdirectory,uptonow,thereisn't
*anymethodtoreleasethemuntilprogrambeend.I'mafraidthatthememory
*wouldbefullofifthefilesystemislargeenoughandJVMmemerysize
*settedtoosmall.
*
*<p>
*Iwon'tpaymoreattentiontosolveit.itisn'tgoalofcurrentaexercise.
*
*@authorJason
*/
classFolderNodeimplementsI_fileSystem{
//privatestaticFolderNodetheRoot;
privatestaticFileSystemViewfsView;
privatestaticbooleanshowHiden=true;;
privateFiletheFile;
privateVector<File>all=newVector<File>();
privateVector<File>folder=newVector<File>();
/**
*setthatwhetherapplyhidenfile.
*
*@paramifshow
*/
publicvoidsetShowHiden(booleanifshow){
showHiden=ifshow;
}
publicIcongetIcon(){
returnfsView.getSystemIcon(theFile);
}
publicStringtoString(){
//returnfsView.
returnfsView.getSystemDisplayName(theFile);
}
/**
*createarootnode.bydefault,itshouldbetheDeskTopinwindowfile
*system.
*
*/
publicFolderNode(){
fsView=FileSystemView.getFileSystemView();
theFile=fsView.getHomeDirectory();
prepareChildren();
}
privatevoidprepareChildren(){
File[]files=fsView.getFiles(theFile,showHiden);
for(inti=0;i<files.length;i++){
all.add(files[i]);
if(files[i].isDirectory()
&&!files[i].toString().toLowerCase().endsWith(".lnk")){
folder.add(files[i]);
}
}
}
privateFolderNode(Filefile){
theFile=file;
prepareChildren();
}
publicFolderNodegetChild(charfileType,intindex){
if(I_fileSystem.DIRECTORY==fileType){
returnnewFolderNode(folder.get(index));
}elseif(I_fileSystem.ALL==fileType){
returnnewFolderNode(all.get(index));
}elseif(I_fileSystem.FILE==fileType){
returnnull;
}else{
returnnull;
}
}
publicintgetChildCount(charfileType){
if(I_fileSystem.DIRECTORY==fileType){
returnfolder.size();
}elseif(I_fileSystem.ALL==fileType){
returnall.size();
}elseif(I_fileSystem.FILE==fileType){
return-1;
}else{
return-1;
}
}
publicbooleanisLeaf(charfileType){
if(I_fileSystem.DIRECTORY==fileType){
returnfolder.size()==0;
}elseif(I_fileSystem.ALL==fileType){
returnall.size()==0;
}elseif(I_fileSystem.FILE==fileType){
returntrue;
}else{
returntrue;
}
}
publicintgetIndexOfChild(charfileType,Objectchild){
if(childinstanceofFolderNode){
if(I_fileSystem.DIRECTORY==fileType){
returnfolder.indexOf(((FolderNode)child).theFile);
}elseif(I_fileSystem.ALL==fileType){
returnall.indexOf(((FolderNode)child).theFile);
}elseif(I_fileSystem.FILE==fileType){
return-1;
}else{
return-1;
}
}else{
return-1;
}
}
}
classFolderRendererextendsDefaultTreeCellRenderer{
privatestaticfinallongserialVersionUID=1L;
publicComponentgetTreeCellRendererComponent(JTreetree,Objectvalue,
booleansel,booleanexpanded,booleanleaf,introw,
booleanhasFocus){
I_fileSystemnode=(I_fileSystem)value;
Iconicon=node.getIcon();
setLeafIcon(icon);
setOpenIcon(icon);
setClosedIcon(icon);
returnsuper.getTreeCellRendererComponent(tree,value,sel,expanded,
leaf,row,hasFocus);
}
}
希望本文所述对大家的java程序设计有所帮助。