⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filenode.java

📁 UCool网络硬盘
💻 JAVA
字号:
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.*;
import java.io.*;
import java.util.*;

class FileNode
{
 protected File m_file;

 public FileNode(){
 	
 }
 public FileNode(File file)
 {
  m_file = file;
 }

 public File getFile()
 {
  return m_file;
 }

 public String toString()
 {
	//System.out.println(m_file.getPath().toString());
  return m_file.getName().length() > 0 ? m_file.getName() :
   m_file.getPath();
   
 }
//展开一个父节点
 public boolean expand(DefaultMutableTreeNode parent)
 {
  DefaultMutableTreeNode flag =
   (DefaultMutableTreeNode)parent.getFirstChild();
  if (flag==null)   // No flag
   return false;
  Object obj = flag.getUserObject();
  if (!(obj instanceof Boolean))
   return false;      // Already expanded

  parent.removeAllChildren();  // Remove Flag

  File[] files = listFiles();
  if (files == null)
   return true;

  Vector v = new Vector();

  for (int k=0; k<files.length; k++)
  {
  	
   File f = files[k];
   //是否显示文件
   /*
   if (!(f.isDirectory()))
	continue;//
	*/
   FileNode newNode = new FileNode(f);

   boolean isAdded = false;
   for (int i=0; i<v.size(); i++)
   {
	FileNode nd = (FileNode)v.elementAt(i);
	if (newNode.compareTo(nd) < 0)
	{
	 v.insertElementAt(newNode, i);
	 isAdded = true;
	 break;
	}
   }
   if (!isAdded)
	v.addElement(newNode);
  }

  for (int i=0; i<v.size(); i++)
  {
   FileNode nd = (FileNode)v.elementAt(i);
   IconData idata = new IconData(UCoolManager.ICON_FOLDER,
   UCoolManager.ICON_EXPANDEDFOLDER, nd);
   DefaultMutableTreeNode node = new
	DefaultMutableTreeNode(idata);
   parent.add(node);

   if (nd.hasSubDirs())
	node.add(new DefaultMutableTreeNode(
	 new Boolean(true) ));
  }

  return true;
 }

 public boolean hasSubDirs()
 {
  File[] files = listFiles();
  if (files == null)
   return false;
  for (int k=0; k<files.length; k++)
  {
   if (files[k].isDirectory())
	return true;
  }
  return false;
 }

 public int compareTo(FileNode toCompare)
 {
  return  m_file.getName().compareToIgnoreCase(
   toCompare.m_file.getName() );
 }

 protected File[] listFiles()
 {
  if (!m_file.isDirectory())
   return null;
  try
  {
   return m_file.listFiles();
  }
  catch (Exception ex)
  {
   JOptionPane.showMessageDialog(null,
	"Error reading directory "+m_file.getAbsolutePath(),
	"Warning", JOptionPane.WARNING_MESSAGE);
   return null;
  }
 }
 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -