Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  *   - Redistributions of source code must retain the above copyright
  9.  *     notice, this list of conditions and the following disclaimer.
  10.  *
  11.  *   - Redistributions in binary form must reproduce the above copyright
  12.  *     notice, this list of conditions and the following disclaimer in the
  13.  *     documentation and/or other materials provided with the distribution.
  14.  *
  15.  *   - Neither the name of Sun Microsystems nor the names of its
  16.  *     contributors may be used to endorse or promote products derived
  17.  *     from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31.  
  32. import javax.swing.tree.DefaultMutableTreeNode;
  33. import javax.mail.Store;
  34. import javax.mail.Folder;
  35. import javax.mail.MessagingException;
  36.  
  37. /**
  38.  * Node which represents a Folder in the javax.mail apis.
  39.  *
  40.  * @author Christopher Cotton
  41.  */
  42. public class FolderTreeNode extends DefaultMutableTreeNode {
  43.    
  44.     protected Folder    folder = null;
  45.     protected boolean   hasLoaded = false;
  46.  
  47.     /**
  48.      * creates a tree node that points to the particular Store.
  49.      *
  50.      * @param what      the store for this node
  51.      */
  52.     public FolderTreeNode(Folder what) {
  53.         super(what);
  54.         folder = what;
  55.     }
  56.  
  57.    
  58.     /**
  59.      * a Folder is a leaf if it cannot contain sub folders
  60.      */
  61.     public boolean isLeaf() {
  62.         try {
  63.             if ((folder.getType() & Folder.HOLDS_FOLDERS) == 0)
  64.                 return true;
  65.         } catch (MessagingException me) { }
  66.        
  67.         // otherwise it does hold folders, and therefore not
  68.         // a leaf
  69.         return false;
  70.     }
  71.    
  72.     /**
  73.      * returns the folder for this node
  74.      */
  75.     public Folder getFolder() {
  76.         return folder;
  77.     }
  78.    
  79.  
  80.  
  81.     /**
  82.      * return the number of children for this folder node. The first
  83.      * time this method is called we load up all of the folders
  84.      * under the store's defaultFolder
  85.      */
  86.  
  87.     public int getChildCount() {
  88.         if (!hasLoaded) {
  89.             loadChildren();
  90.         }
  91.         return super.getChildCount();
  92.     }
  93.    
  94.     protected void loadChildren() {
  95.         // if it is a leaf, just say we have loaded them
  96.         if (isLeaf()) {
  97.             hasLoaded = true;
  98.             return;
  99.         }
  100.  
  101.         try {
  102.             // Folder[] sub = folder.listSubscribed();
  103.             Folder[] sub = folder.list();
  104.  
  105.             // add a FolderTreeNode for each Folder
  106.             int num = sub.length;
  107.             for(int i = 0; i < num; i++) {
  108.                 FolderTreeNode node = new FolderTreeNode(sub[i]);
  109.                 // we used insert here, since add() would make
  110.                 // another recursive call to getChildCount();
  111.                 insert(node, i);
  112.             }
  113.            
  114.         } catch (MessagingException me) {
  115.             me.printStackTrace();
  116.         }
  117.     }
  118.  
  119.  
  120.     /**
  121.      * override toString() since we only want to display a folder's
  122.      * name, and not the full path of the folder
  123.      */
  124.     public String toString() {
  125.         return folder.getName();
  126.     }
  127.    
  128. }
  129.  
  130.