KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.pooka.gui;
2 import net.suberic.pooka.*;
3 import javax.swing.tree.*;
4 import java.awt.*;
5 import javax.mail.MessagingException JavaDoc;
6 import javax.swing.JTree JavaDoc;
7
8 /**
9  * This class overrides the default TreeCellRenderer in order to
10  * provide notification of some such, like for unread messages.
11  * Subclasses could probably add additional enhancements.
12  *
13  */

14
15 public class DefaultFolderTreeCellRenderer extends DefaultTreeCellRenderer {
16   /* grr. it looks like the DefaultTreeCellRenderer returns the same
17      component, which is annoying. that means that we have to reset the
18      font information each time. or at least, that's what i'm doing. :)
19   */

20
21
22   //private boolean hasFocus;
23

24   Font specialFont = null;
25
26   Font defaultFont = null;
27
28   public DefaultFolderTreeCellRenderer() {
29     super();
30   }
31   public Component getTreeCellRendererComponent(JTree JavaDoc tree, Object JavaDoc value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
32     super.getTreeCellRendererComponent(tree,value,sel,expanded,leaf,row,hasFocus);
33     // from super().
34
/*
35     String stringValue = tree.convertValueToText(value, sel,
36                                                  expanded, leaf, row, hasFocus);
37
38     this.hasFocus = hasFocus;
39
40     setText(stringValue);
41     if(sel)
42       setForeground(getTextSelectionColor());
43     else
44       setForeground(getTextNonSelectionColor());
45
46     // There needs to be a way to specify disabled icons.
47     if (!tree.isEnabled()) {
48       setEnabled(false);
49       if (leaf) {
50         setDisabledIcon(getLeafIcon());
51       } else if (expanded) {
52         setDisabledIcon(getOpenIcon());
53       } else {
54         setDisabledIcon(getClosedIcon());
55       }
56     }
57     else {
58       setEnabled(true);
59       if (leaf) {
60         setIcon(getLeafIcon());
61       } else if (expanded) {
62         setIcon(getOpenIcon());
63       } else {
64         setIcon(getClosedIcon());
65       }
66     }
67
68     selected = sel;
69     */

70
71     // end part from DefaultTreeCellRenderer
72

73     TreePath tp = tree.getPathForRow(row);
74
75     if (tp != null && tp.getLastPathComponent() instanceof FolderNode) {
76       FolderNode node = (FolderNode)tp.getLastPathComponent();
77
78       if (node.getFolderInfo().hasNewMessages())
79         setText("* " + getText() + " *");
80
81       if (isSpecial(node)) {
82         setFontToSpecial();
83       } else {
84         setFontToDefault();
85       }
86     } else {
87       setFontToDefault();
88     }
89
90     return this;
91   }
92
93   public void setFontToDefault() {
94     if (getDefaultFont() != null) {
95       setFont(getDefaultFont());
96     } else {
97       // create the new font.
98
String JavaDoc fontStyle;
99       fontStyle = Pooka.getProperty("FolderTree.readStyle", "");
100
101       Font f = null;
102
103       if (this.getFont() == null)
104         return;
105
106       if (fontStyle.equalsIgnoreCase("BOLD"))
107         f = this.getFont().deriveFont(Font.BOLD);
108       else if (fontStyle.equalsIgnoreCase("ITALIC"))
109         f = this.getFont().deriveFont(Font.ITALIC);
110       else if (fontStyle.equals(""))
111         f = this.getFont().deriveFont(Font.PLAIN);
112
113       if (f == null)
114         f = this.getFont();
115
116       setDefaultFont(f);
117       this.setFont(f);
118     }
119
120   }
121
122   /**
123    * This sets the font of the displayed component to the special font.
124    */

125   public void setFontToSpecial() {
126     if (getSpecialFont() != null) {
127       setFont(getSpecialFont());
128     } else {
129       // create the new font.
130
String JavaDoc fontStyle;
131       fontStyle = Pooka.getProperty("FolderTree.UnreadStyle", "BOLD");
132
133       Font f = null;
134
135       Font thisFont = this.getFont();
136       if (thisFont != null) {
137         if (fontStyle.equalsIgnoreCase("BOLD"))
138           f = thisFont.deriveFont(Font.BOLD);
139         else if (fontStyle.equalsIgnoreCase("ITALIC"))
140           f = thisFont.deriveFont(Font.ITALIC);
141         else if (fontStyle.equalsIgnoreCase("PLAIN"))
142           f = thisFont.deriveFont(Font.PLAIN);
143
144         if (f == null)
145           f = thisFont;
146
147         setSpecialFont(f);
148         this.setFont(f);
149       }
150     }
151
152   }
153
154   /**
155    * Returns whether or not we should render the default style or a
156    * special style.
157    */

158   public boolean isSpecial (MailTreeNode node) {
159     if (node instanceof FolderNode)
160       return (node != null && ((FolderNode)node).getFolderInfo().hasUnread());
161     else
162       return false;
163   }
164
165   public Font getSpecialFont() {
166     return specialFont;
167   }
168
169   public void setSpecialFont(Font newValue) {
170     specialFont = newValue;
171   }
172
173   public Font getDefaultFont() {
174     return defaultFont;
175   }
176
177   public void setDefaultFont(Font newValue) {
178     defaultFont = newValue;
179   }
180
181
182   /**
183    * Returns the color the text is drawn with when the node is selected.
184    */

185   public Color getTextSelectionColor() {
186     MainPanel mp = Pooka.getMainPanel();
187     if (mp != null) {
188       FolderPanel fp = mp.getFolderPanel();
189
190       if (fp != null) {
191         javax.swing.plaf.metal.MetalTheme JavaDoc currentTheme = fp .getCurrentTheme();
192         if (currentTheme != null) {
193           return currentTheme.getHighlightedTextColor();
194         }
195       }
196     }
197     return super.getTextSelectionColor();
198   }
199
200   /**
201    * Returns the color the text is drawn with when the node isn't selected.
202    */

203   public Color getTextNonSelectionColor() {
204     MainPanel mp = Pooka.getMainPanel();
205     if (mp != null) {
206       FolderPanel fp = mp.getFolderPanel();
207
208       if (fp != null) {
209         javax.swing.plaf.metal.MetalTheme JavaDoc currentTheme = fp .getCurrentTheme();
210         if (currentTheme != null) {
211           return currentTheme.getUserTextColor();
212         }
213       }
214     }
215     return super.getTextNonSelectionColor();
216   }
217
218   /**
219    * Returns the color to use for the background if node is selected.
220    */

221   public Color getBackgroundSelectionColor() {
222     MainPanel mp = Pooka.getMainPanel();
223     if (mp != null) {
224       FolderPanel fp = mp.getFolderPanel();
225
226       if (fp != null) {
227         javax.swing.plaf.metal.MetalTheme JavaDoc currentTheme = fp .getCurrentTheme();
228         if (currentTheme != null) {
229           return currentTheme.getTextHighlightColor();
230         }
231       }
232     }
233     return super.getBackgroundSelectionColor();
234   }
235
236   /**
237    * Returns the background color to be used for non selected nodes.
238    */

239   public Color getBackgroundNonSelectionColor() {
240     MainPanel mp = Pooka.getMainPanel();
241     if (mp != null) {
242       FolderPanel fp = mp.getFolderPanel();
243
244       if (fp != null) {
245         javax.swing.plaf.metal.MetalTheme JavaDoc currentTheme = fp .getCurrentTheme();
246
247         if (currentTheme != null) {
248           return currentTheme.getWindowBackground();
249         }
250       }
251     }
252
253     return super.getBackgroundNonSelectionColor();
254   }
255
256   /**
257    * Returns the color the border is drawn.
258    */

259   public Color getBorderSelectionColor() {
260     MainPanel mp = Pooka.getMainPanel();
261     if (mp != null) {
262       FolderPanel fp = mp.getFolderPanel();
263
264       if (fp != null) {
265         javax.swing.plaf.metal.MetalTheme JavaDoc currentTheme = fp .getCurrentTheme();
266         if (currentTheme != null) {
267           return currentTheme.getFocusColor();
268         }
269       }
270     }
271
272     return super.getBorderSelectionColor();
273   }
274
275
276 } //end class DefaultFolderTreeCellRenderer
277

278
Popular Tags