KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > FolderTreeNode


1 /*
2  * @(#)FolderTreeNode.java 1.8 01/05/23
3  *
4  * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26  * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
27  * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31  * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that Software is not designed, licensed or intended
35  * for use in the design, construction, operation or maintenance of any
36  * nuclear facility.
37  */

38
39 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
40 import javax.mail.Store JavaDoc;
41 import javax.mail.Folder JavaDoc;
42 import javax.mail.MessagingException JavaDoc;
43
44 /**
45  * Node which represents a Folder in the javax.mail apis.
46  *
47  * @version 1.8, 01/05/23
48  * @author Christopher Cotton
49  */

50 public class FolderTreeNode extends DefaultMutableTreeNode JavaDoc {
51     
52     protected Folder JavaDoc folder = null;
53     protected boolean hasLoaded = false;
54
55     /**
56      * creates a tree node that points to the particular Store.
57      *
58      * @param what the store for this node
59      */

60     public FolderTreeNode(Folder JavaDoc what) {
61     super(what);
62     folder = what;
63     }
64
65     
66     /**
67      * a Folder is a leaf if it cannot contain sub folders
68      */

69     public boolean isLeaf() {
70     try {
71         if ((folder.getType() & Folder.HOLDS_FOLDERS) == 0)
72             return true;
73     } catch (MessagingException JavaDoc me) { }
74     
75     // otherwise it does hold folders, and therefore not
76
// a leaf
77
return false;
78     }
79    
80     /**
81      * returns the folder for this node
82      */

83     public Folder JavaDoc getFolder() {
84     return folder;
85     }
86     
87
88
89     /**
90      * return the number of children for this folder node. The first
91      * time this method is called we load up all of the folders
92      * under the store's defaultFolder
93      */

94
95     public int getChildCount() {
96     if (!hasLoaded) {
97         loadChildren();
98     }
99     return super.getChildCount();
100     }
101     
102     protected void loadChildren() {
103     // if it is a leaf, just say we have loaded them
104
if (isLeaf()) {
105         hasLoaded = true;
106         return;
107     }
108
109     try {
110         // Folder[] sub = folder.listSubscribed();
111
Folder JavaDoc[] sub = folder.list();
112
113         // add a FolderTreeNode for each Folder
114
int num = sub.length;
115         for(int i = 0; i < num; i++) {
116         FolderTreeNode node = new FolderTreeNode(sub[i]);
117         // we used insert here, since add() would make
118
// another recursive call to getChildCount();
119
insert(node, i);
120         }
121         
122     } catch (MessagingException JavaDoc me) {
123         me.printStackTrace();
124     }
125     }
126
127
128     /**
129      * override toString() since we only want to display a folder's
130      * name, and not the full path of the folder
131      */

132     public String JavaDoc toString() {
133     return folder.getName();
134     }
135     
136 }
137
138
Popular Tags