foldertreenode.java

来自「一个用java写的mail.里面的代码值得我们去研究!学习。」· Java 代码 · 共 173 行

JAVA
173
字号
/*** $Id: FolderTreeNode.java,v 1.3 2001/05/07 12:37:21 kunugi Exp $**** Copyright (c) 2000-2001 Jeff Gay** on behalf of ICEMail.org <http://www.icemail.org>** Copyright (c) 1998-2000 by Timothy Gerard Endres** ** This program is free software.** ** You may redistribute it and/or modify it under the terms of the GNU** General Public License as published by the Free Software Foundation.** Version 2 of the license should be included with this distribution in** the file LICENSE, as well as License.html. If the license is not** included with this distribution, you may find a copy at the FSF web** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.**** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR** REDISTRIBUTION OF THIS SOFTWARE. */package org.icemail.mail;import javax.mail.Store;import javax.mail.Folder;import javax.mail.MessagingException;import javax.swing.tree.DefaultMutableTreeNode;/** * The class represents a Folder and all related messages and sub-folders as nodes on a tree. * * @see FolderTreePanel */public class FolderTreeNode  extends DefaultMutableTreeNode{  private Folder  folder = null;  private boolean hasLoaded = false;  private boolean haveLeaf = false;  private boolean cachedLeaf = false;  /**   * Construct a tree node that references the given folder.   *   * @param folder the folder for this node   */  public  FolderTreeNode( Folder folder ) {    super( folder );    this.folder = folder;  }  /**   * Get the underlying folder of this node.   *   * @return the underlying folder of this node.   */  public Folder  getFolder() {    return this.folder;  }  /**   * Return the folder's name rather than the full path.   *   * @return the folder's name rather than the full path.   */  public String  toString() {    return folder.getName();  }//............................................................  /**   * Returns a shallow copy of this node; the new node has no parent   * or children and has a reference to the same user object, if any.   * <p>   * Implementation of java.swing.tree.DefaultMutableTreeNode.clone()   *   * @return a copy of this node   */  public Object  clone() {    FolderTreeNode newNode = (FolderTreeNode) super.clone();    newNode.folder = folder;    newNode.hasLoaded = hasLoaded;    newNode.haveLeaf = haveLeaf;    newNode.cachedLeaf = cachedLeaf;    return newNode;  }  /**   * Returns true if this node has no children.   * A Folder is a leaf if it does not contain sub-folders.   * <p>   * Implementation of java.swing.tree.DefaultMutableTreeNode.isLeaf()   *   * @return true if the folder contains sub-folders   */  public boolean  isLeaf() {    if ( ! this.haveLeaf ) {      try {        MailUtilities.setStoreConnected( folder.getStore() );        int type = folder.getType();        this.cachedLeaf = ( ( type & Folder.HOLDS_FOLDERS ) == 0 );        this.haveLeaf = true;      } catch ( MessagingException ex ) {      }    }    return this.cachedLeaf;  }  /**   * Returns the number of children of this node.   * <p>   * The first time this method is called, all sub-folders are added.   * <p>   * Implementation of java.swing.tree.DefaultMutableTreeNode.getChildCount()   *   * @return the number of children of this node   */  public int  getChildCount() {    if ( ! this.hasLoaded ) {      this.loadChildren();    }    return super.getChildCount();  }//............................................................  private void  loadChildren() {  // if it is a leaf, just say we have loaded them    if ( this.isLeaf() ) {      this.hasLoaded = true;      return;    }    try {      Folder[] sub = folder.list();      if ( sub.length > 1 ) {        RootTreeNode.sortFolderList( sub );      }    // add a FolderTreeNode for each Folder      int num = sub.length;      for( int i = 0 ; i < num ; i++ ) {        FolderTreeNode node = new FolderTreeNode( sub[i] );      // We use insert here, since add() would make      // another recursive call to getChildCount();        this.insert( node, i );      }      this.hasLoaded = true;    } catch ( MessagingException ex ) {      ex.printStackTrace();    }  }}

⌨️ 快捷键说明

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