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.*;
  34.  
  35. /**
  36.  * Node which represents a Store in the javax.mail apis.
  37.  *
  38.  * @author Christopher Cotton
  39.  */
  40. public class StoreTreeNode extends DefaultMutableTreeNode {
  41.    
  42.     protected Store     store = null;
  43.     protected Folder    folder = null;
  44.     protected String    display = null;
  45.  
  46.     /**
  47.      * creates a tree node that points to the particular Store.
  48.      *
  49.      * @param what      the store for this node
  50.      */
  51.     public StoreTreeNode(Store what) {
  52.         super(what);
  53.         store = what;
  54.     }
  55.  
  56.    
  57.     /**
  58.      * a Store is never a leaf node.  It can always contain stuff
  59.      */
  60.     public boolean isLeaf() {
  61.         return false;
  62.     }
  63.    
  64.  
  65.     /**
  66.      * return the number of children for this store node. The first
  67.      * time this method is called we load up all of the folders
  68.      * under the store's defaultFolder
  69.      */
  70.  
  71.     public int getChildCount() {
  72.         if (folder == null) {
  73.             loadChildren();
  74.         }
  75.         return super.getChildCount();
  76.     }
  77.    
  78.     protected void loadChildren() {
  79.         try {
  80.             // connect to the Store if we need to
  81.             if (!store.isConnected()) {
  82.                 store.connect();
  83.             }
  84.  
  85.             // get the default folder, and list the
  86.             // subscribed folders on it
  87.             folder = store.getDefaultFolder();
  88.             // Folder[] sub = folder.listSubscribed();
  89.             Folder[] sub = folder.list();
  90.  
  91.             // add a FolderTreeNode for each Folder
  92.             int num = sub.length;
  93.             for(int i = 0; i < num; i++) {
  94.                 FolderTreeNode node = new FolderTreeNode(sub[i]);
  95.                 // we used insert here, since add() would make
  96.                 // another recursive call to getChildCount();
  97.                 insert(node, i);
  98.             }
  99.            
  100.         } catch (MessagingException me) {
  101.             me.printStackTrace();
  102.         }
  103.     }
  104.  
  105.     /**
  106.      * We override toString() so we can display the store URLName
  107.      * without the password.
  108.      */
  109.  
  110.     public String toString() {
  111.         if (display == null) {
  112.             URLName url = store.getURLName();
  113.             if (url == null) {
  114.                 display = store.toString();
  115.             } else {
  116.                 // don't show the password
  117.                 URLName too = new URLName( url.getProtocol(), url.getHost(), url.getPort(),
  118.                                            url.getFile(), url.getUsername(), null);
  119.                 display = too.toString();
  120.             }
  121.         }
  122.        
  123.         return display;
  124.     }
  125.    
  126.    
  127. }
  128.  
  129.