KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > EnhancedFolderTreeCellRenderer


1 package net.suberic.pooka.gui;
2 import net.suberic.pooka.*;
3 import net.suberic.util.gui.IconManager;
4 import javax.swing.tree.*;
5 import java.awt.*;
6 import javax.swing.Icon JavaDoc;
7 import javax.swing.ImageIcon JavaDoc;
8 import javax.swing.JTree JavaDoc;
9
10 /**
11  * This is basically an extension of DefaultFolderTreeCellRenderer that has
12  * icon support added.
13  */

14 public class EnhancedFolderTreeCellRenderer extends DefaultFolderTreeCellRenderer {
15
16   protected boolean hasFocus;
17
18   Icon JavaDoc rootIcon;
19   Icon JavaDoc connectedIcon;
20   Icon JavaDoc disconnectedIcon;
21   Icon JavaDoc closedFolderIcon;
22   Icon JavaDoc unavailableIcon;
23   Icon JavaDoc connectedStoreIcon;
24   Icon JavaDoc disconnectedStoreIcon;
25   Icon JavaDoc closedStoreIcon;
26   Icon JavaDoc subfolderIcon;
27   Icon JavaDoc subfolderWithNewIcon;
28   Icon JavaDoc subfolderClosedIcon;
29   Icon JavaDoc connectedWithNewIcon;
30   Icon JavaDoc disconnectedWithNewIcon;
31
32   /**
33    * Creates the EnhancedFolderTreeCellRenderer.
34    */

35   public EnhancedFolderTreeCellRenderer() {
36     super();
37   }
38
39   /**
40    * gets the renderer component.
41    */

42   public Component getTreeCellRendererComponent(JTree JavaDoc tree, Object JavaDoc value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
43     super.getTreeCellRendererComponent(tree,value,sel,expanded,leaf,row,hasFocus);
44     TreePath tp = tree.getPathForRow(row);
45
46     if (tp != null) {
47       Object JavaDoc lastPath = tp.getLastPathComponent();
48       if (lastPath instanceof FolderNode) {
49         FolderNode node = (FolderNode)lastPath;
50
51         FolderInfo fi = node.getFolderInfo();
52
53         if (isSpecial(node)) {
54           setFontToSpecial();
55         } else {
56           setFontToDefault();
57         }
58
59         FolderInfo folderInfo = ((FolderNode)node).getFolderInfo();
60
61         if (folderInfo == null){
62           setIconToClosedFolder();
63         } else {
64           //System.out.println("folderInfo is " + folderInfo.getFolderID() + "; hasNewMessages is " + folderInfo.hasNewMessages() + "; notifyNewMessagesNode is "+ folderInfo.notifyNewMessagesNode());
65
if (!((FolderNode)node).isLeaf()) {
66             //System.out.println("folderInfo is " + folderInfo.getFolderID() + "; hasNewMessages is " + folderInfo.hasNewMessages());
67
if (folderInfo.hasNewMessages() && folderInfo.notifyNewMessagesNode())
68               setIconToSubfolderWithNew();
69             else if ((folderInfo.getType() & javax.mail.Folder.HOLDS_MESSAGES) != 0) {
70               // folder that don't hold messages are always disconnected,
71
// so show their status by their store.
72
if ( folderInfo.isConnected()) {
73                 setIconToSubfolder();
74               } else {
75                 setIconToSubfolderClosed();
76               }
77             } else {
78               if ( folderInfo.getParentStore().isConnected()) {
79                 setIconToSubfolder();
80               } else {
81                 setIconToSubfolderClosed();
82               }
83             }
84           } else if (folderInfo.isConnected()) {
85             if (folderInfo.notifyNewMessagesNode() && folderInfo.hasNewMessages()) {
86               setIconToOpenWithNew();
87             } else
88               setIconToOpen();
89           } else if (folderInfo.isSortaOpen()) {
90             if (folderInfo.notifyNewMessagesNode() && folderInfo.hasNewMessages()) {
91               setIconToDisconnectedWithNew();
92             } else
93               setIconToDisconnected();
94           } else if (!folderInfo.isValid()) {
95             setIconToUnavailable();
96           } else {
97             setIconToClosedFolder();
98           }
99         }
100       } else if (lastPath instanceof StoreNode) {
101         StoreInfo storeInfo = ((StoreNode)lastPath).getStoreInfo();
102         if (storeInfo.isConnected())
103           setIconToConnectedStore();
104         else
105           setIconToDisconnectedStore();
106
107         setFontToDefault();
108       } else {
109         setIconToRoot();
110       }
111     } else {
112       setIconToDisconnected();
113     }
114
115     String JavaDoc stringValue = tree.convertValueToText(value, sel, expanded, leaf, row, hasFocus);
116     setText(stringValue);
117     return this;
118   }
119
120   /**
121    * Sets the icon to the unavailable icon.
122    */

123   public void setIconToUnavailable() {
124     if (getUnavailableIcon() != null)
125       setIcon(getUnavailableIcon());
126     else {
127       // create the new Icon.
128
ImageIcon JavaDoc icon = Pooka.getUIFactory().getIconManager().getIcon("FolderTree.Unavailable");
129
130       if (icon != null) {
131         setUnavailableIcon(icon);
132         setIcon(getUnavailableIcon());
133       }
134     }
135   }
136
137   /**
138    * Sets the icon to the open icon.
139    */

140   public void setIconToOpen() {
141     if (getConnectedIcon() != null)
142       setIcon(getConnectedIcon());
143     else {
144       // create the new Icon.
145
ImageIcon JavaDoc icon = Pooka.getUIFactory().getIconManager().getIcon("FolderTree.Connected");
146
147       if (icon != null) {
148         setConnectedIcon(icon);
149         setIcon(getConnectedIcon());
150       }
151     }
152   }
153
154   /**
155    * Sets the icon to open with new.
156    */

157   public void setIconToOpenWithNew() {
158     if (getConnectedWithNewIcon() != null)
159       setIcon(getConnectedWithNewIcon());
160     else {
161       // create the new Icon.
162
ImageIcon JavaDoc icon = Pooka.getUIFactory().getIconManager().getIcon("FolderTree.ConnectedNew");
163
164       if (icon != null) {
165         setConnectedWithNewIcon(icon);
166         setIcon(getConnectedWithNewIcon());
167       }
168     }
169   }
170
171   /**
172    * Sets the icon to the disconnected icon.
173    */

174   public void setIconToDisconnected() {
175     if (getDisconnectedIcon() != null)
176       setIcon(getDisconnectedIcon());
177     else {
178       // create the new Icon.
179
ImageIcon JavaDoc icon = Pooka.getUIFactory().getIconManager().getIcon("FolderTree.Disconnected");
180
181       if (icon != null) {
182         setDisconnectedIcon(icon);
183         setIcon(getDisconnectedIcon());
184       }
185     }
186   }
187
188   /**
189    * Sets the icon to disconnected with new.
190    */

191   public void setIconToDisconnectedWithNew() {
192     if (getDisconnectedWithNewIcon() != null)
193       setIcon(getDisconnectedWithNewIcon());
194     else {
195       // create the new Icon.
196
ImageIcon JavaDoc icon = Pooka.getUIFactory().getIconManager().getIcon("FolderTree.DisconnectedNew");
197
198       if (icon != null) {
199         setDisconnectedWithNewIcon(icon);
200         setIcon(getDisconnectedWithNewIcon());
201       }
202     }
203   }
204
205   public void setIconToClosedFolder() {
206     if (getClosedFolderIcon() != null)
207       setIcon(getClosedFolderIcon());
208     else {
209       // create the new Icon.
210
ImageIcon JavaDoc icon = Pooka.getUIFactory().getIconManager().getIcon("FolderTree.Closed");
211
212       if (icon != null) {
213         setClosedFolderIcon(icon);
214         setIcon(getClosedFolderIcon());
215       }
216     }
217   }
218
219   public void setIconToDisconnectedStore() {
220     if (getDisconnectedStoreIcon() != null)
221       setIcon(getDisconnectedStoreIcon());
222     else {
223       // create the new Icon.
224
ImageIcon JavaDoc icon = Pooka.getUIFactory().getIconManager().getIcon("FolderTree.StoreDisconnected");
225
226       if (icon != null) {
227         setDisconnectedStoreIcon(icon);
228         setIcon(getDisconnectedStoreIcon());
229       }
230     }
231   }
232
233   public void setIconToConnectedStore() {
234     if (getConnectedStoreIcon() != null)
235       setIcon(getConnectedStoreIcon());
236     else {
237       // create the new Icon.
238
ImageIcon JavaDoc icon = Pooka.getUIFactory().getIconManager().getIcon("FolderTree.StoreConnected");
239
240       if (icon != null) {
241         // create the new Icon.
242
setConnectedStoreIcon(icon);
243         setIcon(getConnectedStoreIcon());
244       }
245     }
246   }
247
248   public void setIconToSubfolderClosed() {
249     if (getSubfolderClosedIcon() != null)
250       setIcon(getSubfolderClosedIcon());
251     else {
252       // create the new Icon.
253
ImageIcon JavaDoc icon = Pooka.getUIFactory().getIconManager().getIcon("FolderTree.SubFolderClosed");
254
255       if (icon != null) {
256         setSubfolderClosedIcon(icon);
257         setIcon(getSubfolderClosedIcon());
258       }
259     }
260   }
261
262   public void setIconToSubfolder() {
263     if (getSubfolderIcon() != null)
264       setIcon(getSubfolderIcon());
265     else {
266       // create the new Icon.
267
ImageIcon JavaDoc icon = Pooka.getUIFactory().getIconManager().getIcon("FolderTree.SubFolder");
268
269       if (icon != null) {
270         setSubfolderIcon(icon);
271         setIcon(getSubfolderIcon());
272       }
273     }
274   }
275
276   public void setIconToSubfolderWithNew() {
277     if (getSubfolderWithNewIcon() != null)
278       setIcon(getSubfolderWithNewIcon());
279     else {
280       // create the new Icon.
281
ImageIcon JavaDoc icon = Pooka.getUIFactory().getIconManager().getIcon("FolderTree.SubFolderNew");
282
283       if (icon != null) {
284         setSubfolderWithNewIcon(icon);
285         setIcon(getSubfolderWithNewIcon());
286       }
287     }
288   }
289
290   public void setIconToRoot() {
291     if (getRootIcon() != null)
292       setIcon(getRootIcon());
293     else {
294       // create the new Icon.
295
ImageIcon JavaDoc icon = Pooka.getUIFactory().getIconManager().getIcon("FolderTree.Root");
296
297       if (icon != null) {
298         setRootIcon(icon);
299         setIcon(getRootIcon());
300       }
301     }
302   }
303
304   public Icon JavaDoc getConnectedIcon() {
305     return connectedIcon;
306   }
307
308   public void setConnectedIcon(Icon JavaDoc newIcon) {
309     connectedIcon = newIcon;
310   }
311
312   public Icon JavaDoc getConnectedWithNewIcon() {
313     return connectedWithNewIcon;
314   }
315
316   public void setConnectedWithNewIcon(Icon JavaDoc newIcon) {
317     connectedWithNewIcon = newIcon;
318   }
319
320   public void setDisconnectedIcon(Icon JavaDoc newIcon) {
321     disconnectedIcon = newIcon;
322   }
323
324   public Icon JavaDoc getDisconnectedIcon() {
325     return disconnectedIcon;
326   }
327   public Icon JavaDoc getDisconnectedWithNewIcon() {
328     return disconnectedWithNewIcon;
329   }
330
331   public Icon JavaDoc getSubfolderWithNewIcon() {
332     return subfolderWithNewIcon;
333   }
334
335   public void setSubfolderWithNewIcon(Icon JavaDoc newIcon) {
336     subfolderWithNewIcon = newIcon;
337   }
338
339   public void setDisconnectedWithNewIcon(Icon JavaDoc newIcon) {
340     disconnectedWithNewIcon = newIcon;
341   }
342
343   public Icon JavaDoc getClosedFolderIcon() {
344     return closedFolderIcon;
345   }
346
347   public void setClosedFolderIcon(Icon JavaDoc newIcon) {
348     closedFolderIcon = newIcon;
349   }
350
351   public Icon JavaDoc getUnavailableIcon() {
352     return unavailableIcon;
353   }
354
355   public void setUnavailableIcon(Icon JavaDoc newIcon) {
356     unavailableIcon = newIcon;
357   }
358
359   public Icon JavaDoc getConnectedStoreIcon() {
360     return connectedStoreIcon;
361   }
362
363   public void setConnectedStoreIcon(Icon JavaDoc newIcon) {
364     connectedStoreIcon = newIcon;
365   }
366
367   public Icon JavaDoc getDisconnectedStoreIcon() {
368     return disconnectedStoreIcon;
369   }
370
371   public void setDisconnectedStoreIcon(Icon JavaDoc newIcon) {
372     disconnectedStoreIcon = newIcon;
373   }
374
375   public Icon JavaDoc getSubfolderIcon() {
376     return subfolderIcon;
377   }
378
379   public void setSubfolderIcon(Icon JavaDoc newIcon) {
380     subfolderIcon = newIcon;
381   }
382
383   public Icon JavaDoc getSubfolderClosedIcon() {
384     return subfolderClosedIcon;
385   }
386
387   public void setSubfolderClosedIcon(Icon JavaDoc newIcon) {
388     subfolderClosedIcon = newIcon;
389   }
390
391   public Icon JavaDoc getRootIcon() {
392     return rootIcon;
393   }
394
395   public void setRootIcon(Icon JavaDoc newIcon) {
396     rootIcon = newIcon;
397   }
398 }
399
400
401
Popular Tags