KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > table > SubjectTreeRenderer


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.mail.gui.table;
17
18 import java.awt.Color JavaDoc;
19 import java.awt.Component JavaDoc;
20 import java.awt.Font JavaDoc;
21
22 import javax.swing.JTable JavaDoc;
23 import javax.swing.JTree JavaDoc;
24 import javax.swing.UIManager JavaDoc;
25 import javax.swing.table.TableColumn JavaDoc;
26 import javax.swing.tree.DefaultTreeCellRenderer JavaDoc;
27
28 import org.columba.mail.gui.table.model.MessageNode;
29 import org.columba.mail.message.ColumbaHeader;
30 import org.columba.mail.message.IColumbaHeader;
31 import org.columba.ristretto.message.Flags;
32
33 /**
34  * Renderer for the JTree in the JTable, which is responsible for displaying the
35  * Subject: headerfield.
36  * <p>
37  * I'm still not convinced which method to calculate the bounds of the table
38  * column is faster. <br>
39  * The first one overwrites paint() and layout(), the other just overwrites
40  * setBounds() only, using the passed JTableColumn. Personally, I prefer the
41  * second version, because it should be much faster than calculating the column
42  * size, based on the text and font settings.
43  *
44  *
45  * @author fdietz
46  */

47 public class SubjectTreeRenderer extends DefaultTreeCellRenderer JavaDoc {
48     private Font JavaDoc plainFont;
49
50     private Font JavaDoc boldFont;
51
52     private Font JavaDoc underlinedFont;
53
54     private JTable JavaDoc table;
55
56     private TableColumn JavaDoc tc;
57
58     /**
59      * @param table
60      */

61     public SubjectTreeRenderer(JTable JavaDoc table) {
62         super();
63
64         this.table = table;
65
66         boldFont = UIManager.getFont("Label.font");
67         boldFont = boldFont.deriveFont(Font.BOLD);
68
69         plainFont = UIManager.getFont("Label.font");
70
71         underlinedFont = UIManager.getFont("Tree.font");
72         underlinedFont = underlinedFont.deriveFont(Font.ITALIC);
73
74         setOpaque(true);
75
76         setBackground(null);
77         setBackgroundNonSelectionColor(null);
78     }
79
80     public void setBounds(int x, int y, int w, int h) {
81         if (tc == null) {
82             tc = table.getColumn("Subject");
83         }
84
85         super.setBounds(x, y, tc.getWidth() - x, h);
86     }
87
88     /**
89      *
90      * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable,
91      * java.lang.Object, boolean, boolean, int, int)
92      */

93     public Component JavaDoc getTreeCellRendererComponent(JTree JavaDoc tree, Object JavaDoc value,
94             boolean selected, boolean expanded, boolean leaf, int row,
95             boolean hasFocus) {
96         super.getTreeCellRendererComponent(tree, value, selected, expanded,
97                 leaf, row, hasFocus);
98
99         MessageNode messageNode = (MessageNode) value;
100
101         if (messageNode.getUserObject().equals("root")) {
102             setText("...");
103             setIcon(null);
104
105             return this;
106         }
107
108         IColumbaHeader header = messageNode.getHeader();
109
110         if (header == null) {
111             return this;
112         }
113
114         Flags flags = ((ColumbaHeader) header).getFlags();
115
116         if (flags != null) {
117             if (!flags.getSeen()) {
118                 if (!getFont().equals(boldFont)) {
119                     setFont(boldFont);
120                 }
121             } else if (messageNode.isHasRecentChildren()) {
122                 if (!getFont().equals(underlinedFont)) {
123                     setFont(underlinedFont);
124                 }
125             } else {
126                 if (!getFont().equals(plainFont)) {
127                     setFont(plainFont);
128                 }
129             }
130         }
131
132         Color JavaDoc msgColor = (Color JavaDoc) header.get("columba.color");
133
134         if (selected)
135             setBackground(UIManager.getColor("Table.selectionBackground"));
136         else
137             setBackground(table.getBackground());
138
139         if (msgColor != null) {
140             if (selected)
141                 setForeground(UIManager.getColor("Table.selectionForeground"));
142             else {
143                 if (msgColor.equals(Color.BLACK) == false)
144                     setForeground(msgColor);
145                 else
146                     setForeground(table.getForeground());
147
148             }
149         }
150
151         String JavaDoc subject = (String JavaDoc) header.get("columba.subject");
152
153         if (subject != null) {
154             setText(subject);
155         } else {
156             setText("null");
157         }
158
159         setIcon(null);
160
161         return this;
162     }
163
164 }
Popular Tags