欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现的Windows资源管理器实例

程序员文章站 2024-03-04 19:52:48
本文实例讲述了java实现的windows资源管理器。分享给大家供大家参考。具体如下: filetree.java文件如下: // filetree.java...

本文实例讲述了java实现的windows资源管理器。分享给大家供大家参考。具体如下:

filetree.java文件如下:

// filetree.java
/***********************************************************
 *  author: jason
 *   email: tl21cen@hotmail.com
 * csdn blog: http://blog.csdn.net/unagain/
 ***********************************************************/
package tl.exercise.swing;
import java.awt.component;
import java.io.file;
import java.util.vector;
import javax.swing.icon;
import javax.swing.jtree;
import javax.swing.event.treeexpansionevent;
import javax.swing.event.treeexpansionlistener;
import javax.swing.event.treemodellistener;
import javax.swing.event.treeselectionevent;
import javax.swing.event.treeselectionlistener;
import javax.swing.filechooser.filesystemview;
import javax.swing.tree.defaulttreecellrenderer;
import javax.swing.tree.treemodel;
import javax.swing.tree.treepath;
public class filetree extends jtree {
  static final long serialversionuid = 0;
  private filelist thelist;
  public filetree(filelist list) {
    thelist = list;
    setmodel(new filesystemmodel(new foldernode()));
    this.setcellrenderer(new folderrenderer());
    addtreeselectionlistener(new treeselectionlistener() {
      public void valuechanged(treeselectionevent tse) {
      }
    });   
    this.setselectionrow(0);
  }
  public void firevaluechanged(treeselectionevent tse) {
    treepath tp = tse.getnewleadselectionpath();
    object o = tp.getlastpathcomponent();
    // thelist.firetreeselectionchanged((pathnode)o);
    thelist.firetreeselectionchanged((foldernode) o);
  }
  public void firetreecollapsed(treepath path) {
    super.firetreecollapsed(path);
    treepath curpath = getselectionpath();
    if (path.isdescendant(curpath)) {
      setselectionpath(path);
    }
  }
  public void firetreewillexpand(treepath path) {
    system.out.println("path will expand is " + path);
  }
  public void firetreewillcollapse(treepath path) {
    system.out.println("path will collapse is " + path);
  }
  class expansionlistener implements treeexpansionlistener {
    filetree tree;
    public expansionlistener(filetree ft) {
      tree = ft;
    }
    public void treecollapsed(treeexpansionevent tee) {
    }
    public void treeexpanded(treeexpansionevent tee) {
    }
  }
}
class filesystemmodel implements treemodel {
  i_filesystem theroot;
  char filetype = i_filesystem.directory;
  public filesystemmodel(i_filesystem fs) {
    theroot = fs;
  }
  public object getroot() {
    return theroot;
  }
  public object getchild(object parent, int index) {
    return ((i_filesystem) parent).getchild(filetype, index);
  }
  public int getchildcount(object parent) {
    return ((i_filesystem) parent).getchildcount(filetype);
  }
  public boolean isleaf(object node) {
    return ((i_filesystem) node).isleaf(filetype);
  }
  public int getindexofchild(object parent, object child) {
    return ((i_filesystem) parent).getindexofchild(filetype, child);
  }
  public void valueforpathchanged(treepath path, object newvalue) {
  }
  public void addtreemodellistener(treemodellistener l) {
  }
  public void removetreemodellistener(treemodellistener l) {
  }
}
interface i_filesystem {
  final public static char directory = 'd';
  final public static char file = 'f';
  final public static char all = 'a';
  public icon geticon();
  public i_filesystem getchild(char filetype, int index);
  public int getchildcount(char filetype);
  public boolean isleaf(char filetype);
  public int getindexofchild(char filetype, object child);
}
/**
 * a data model for a jtree. this model explorer windows file system directly.
 *
 * <p>
 * perhaps there is a fatal bug with this design. for speed, each of instances
 * of this model contains file objects of subdirectory, up to now, there isn't
 * any method to release them until program be end. i'm afraid that the memory
 * would be full of if the file system is large enough and jvm memery size
 * setted too small.
 *
 * <p>
 * i won't pay more attention to solve it. it isn't goal of current a exercise.
 *
 * @author jason
 */
class foldernode implements i_filesystem {
  // private static foldernode theroot;
  private static filesystemview fsview;
  private static boolean showhiden = true;;
  private file thefile;
  private vector<file> all = new vector<file>();
  private vector<file> folder = new vector<file>();
  /**
   * set that whether apply hiden file.
   *
   * @param ifshow
   */
  public void setshowhiden(boolean ifshow) {
    showhiden = ifshow;
  }
  public icon geticon() {
    return fsview.getsystemicon(thefile);
  }
  public string tostring() {
    // return fsview.
    return fsview.getsystemdisplayname(thefile);
  }
  /**
   * create a root node. by default, it should be the desktop in window file
   * system.
   *
   */
  public foldernode() {
    fsview = filesystemview.getfilesystemview();
    thefile = fsview.gethomedirectory();
    preparechildren();
  }
  private void preparechildren() {
   file[] files = fsview.getfiles(thefile, showhiden);
    for (int i = 0; i < files.length; i++) {
      all.add(files[i]);
      if (files[i].isdirectory()
          && !files[i].tostring().tolowercase().endswith(".lnk")) {
        folder.add(files[i]);
      }
    }
  }
  private foldernode(file file) {
    thefile = file;
    preparechildren();
  }
  public foldernode getchild(char filetype, int index) {
    if (i_filesystem.directory == filetype) {
      return new foldernode(folder.get(index));
    } else if (i_filesystem.all == filetype) {
      return new foldernode(all.get(index));
    } else if (i_filesystem.file == filetype) {
      return null;
    } else {
      return null;
    }
  }
  public int getchildcount(char filetype) {
    if (i_filesystem.directory == filetype) {
      return folder.size();
    } else if (i_filesystem.all == filetype) {
      return all.size();
    } else if (i_filesystem.file == filetype) {
      return -1;
    } else {
      return -1;
    }
  }
  public boolean isleaf(char filetype) {
    if (i_filesystem.directory == filetype) {
      return folder.size() == 0;
    } else if (i_filesystem.all == filetype) {
      return all.size() == 0;
    } else if (i_filesystem.file == filetype) {
      return true;
    } else {
      return true;
    }
  }
  public int getindexofchild(char filetype, object child) {
    if (child instanceof foldernode) {
      if (i_filesystem.directory == filetype) {
        return folder.indexof(((foldernode) child).thefile);
      } else if (i_filesystem.all == filetype) {
        return all.indexof(((foldernode) child).thefile);
      } else if (i_filesystem.file == filetype) {
        return -1;
      } else {
        return -1;
      }
    } else {
      return -1;
    }
  }
}
class folderrenderer extends defaulttreecellrenderer {
  private static final long serialversionuid = 1l;
  public component gettreecellrenderercomponent(jtree tree, object value,
      boolean sel, boolean expanded, boolean leaf, int row,
      boolean hasfocus) {
    i_filesystem node = (i_filesystem) value;
    icon icon = node.geticon();
    setleaficon(icon);
    setopenicon(icon);
    setclosedicon(icon);
    return super.gettreecellrenderercomponent(tree, value, sel, expanded,
       leaf, row, hasfocus);
  }
}

希望本文所述对大家的java程序设计有所帮助。