KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > StoreTreeNode


1 /*
2  * @(#)StoreTreeNode.java 1.9 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.*;
41
42 /**
43  * Node which represents a Store in the javax.mail apis.
44  *
45  * @version 1.9, 01/05/23
46  * @author Christopher Cotton
47  */

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

59     public StoreTreeNode(Store what) {
60     super(what);
61     store = what;
62     }
63
64     
65     /**
66      * a Store is never a leaf node. It can always contain stuff
67      */

68     public boolean isLeaf() {
69     return false;
70     }
71    
72
73     /**
74      * return the number of children for this store node. The first
75      * time this method is called we load up all of the folders
76      * under the store's defaultFolder
77      */

78
79     public int getChildCount() {
80     if (folder == null) {
81         loadChildren();
82     }
83     return super.getChildCount();
84     }
85     
86     protected void loadChildren() {
87     try {
88         // connect to the Store if we need to
89
if (!store.isConnected()) {
90         store.connect();
91         }
92
93         // get the default folder, and list the
94
// subscribed folders on it
95
folder = store.getDefaultFolder();
96         // Folder[] sub = folder.listSubscribed();
97
Folder[] sub = folder.list();
98
99         // add a FolderTreeNode for each Folder
100
int num = sub.length;
101         for(int i = 0; i < num; i++) {
102         FolderTreeNode node = new FolderTreeNode(sub[i]);
103         // we used insert here, since add() would make
104
// another recursive call to getChildCount();
105
insert(node, i);
106         }
107         
108     } catch (MessagingException me) {
109         me.printStackTrace();
110     }
111     }
112
113     /**
114      * We override toString() so we can display the store URLName
115      * without the password.
116      */

117
118     public String JavaDoc toString() {
119     if (display == null) {
120         URLName url = store.getURLName();
121         if (url == null) {
122         display = store.toString();
123         } else {
124         // don't show the password
125
URLName too = new URLName( url.getProtocol(), url.getHost(), url.getPort(),
126                        url.getFile(), url.getUsername(), null);
127         display = too.toString();
128         }
129     }
130     
131     return display;
132     }
133     
134     
135 }
136
137
Popular Tags