KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.pooka.gui;
2 import net.suberic.pooka.*;
3 import net.suberic.pooka.cache.*;
4 import javax.swing.table.*;
5 import java.awt.*;
6 import javax.swing.*;
7 import java.util.Date JavaDoc;
8 import java.util.Calendar JavaDoc;
9
10 /**
11  * This class overrides the default TableCellRenderer in order to
12  * show things like unread messages, etc.
13  */

14
15 public class DefaultFolderCellRenderer extends DefaultTableCellRenderer {
16     static Font unreadFont = null;
17     static Color defaultColor = null;
18     static Color uncachedColor = null;
19
20     public static Font getUnreadFont() {
21     return unreadFont;
22     }
23     
24     public static void setUnreadFont(Font newValue) {
25     unreadFont = newValue;
26     }
27
28     public static Color getDefaultColor() {
29     if (defaultColor != null)
30         return defaultColor;
31     else {
32         defaultColor = Color.getColor(Pooka.getProperty("CachedMessages.notCached.foregroundColor", "black"));
33         return defaultColor;
34
35     }
36     }
37
38     public static Color getUncachedColor() {
39     if (uncachedColor != null)
40         return uncachedColor;
41     else {
42         String JavaDoc colorString = Pooka.getProperty("CachedMessages.notCached.foregroundColor", "red");
43         try {
44         uncachedColor = new Color(Integer.parseInt(colorString));
45         } catch (Exception JavaDoc e) {
46         uncachedColor = Color.red;
47         }
48         return uncachedColor;
49     }
50     }
51
52     public Component getTableCellRendererComponent(JTable table, Object JavaDoc value, boolean isSelected, boolean hasFocus, int row, int column) {
53
54     Component returnValue = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
55
56     if (value instanceof TableCellIcon) {
57         TableCellIcon tcIcon = (TableCellIcon)value;
58         Component icon = tcIcon.getIcon();
59         if (icon != null) {
60         icon.setBackground(returnValue.getBackground());
61         }
62         return icon;
63     } else if (value instanceof RowCounter) {
64         if (returnValue instanceof JLabel)
65         ((JLabel)returnValue).setText(Integer.toString(row + 1));
66         else {
67         JLabel label = new JLabel(Integer.toString(row + 1));
68         label.setBackground(returnValue.getBackground());
69         label.setForeground(returnValue.getForeground());
70         label.setFont(returnValue.getFont());
71         returnValue = label;
72         }
73     } else if (value instanceof Date JavaDoc) {
74         Date JavaDoc displayDate = (Date JavaDoc)value;
75         String JavaDoc dateText = null;
76         Calendar JavaDoc current = Calendar.getInstance();
77         current.set(Calendar.HOUR_OF_DAY, 0);
78         current.set(Calendar.MINUTE, 0);
79         if (current.getTime().before(displayDate)) {
80         dateText = Pooka.getDateFormatter().todayFormat.format(displayDate);
81         } else {
82         current.add(Calendar.DAY_OF_YEAR, (current.getMaximum(Calendar.DAY_OF_WEEK) - 1) * -1);
83         if (current.getTime().before(displayDate)) {
84             dateText = Pooka.getDateFormatter().thisWeekFormat.format(displayDate);
85         } else {
86             dateText = Pooka.getDateFormatter().shortFormat.format(displayDate);
87         }
88         }
89
90         if (returnValue instanceof JLabel)
91         ((JLabel)returnValue).setText(dateText);
92         else {
93         JLabel label = new JLabel(dateText);
94         label.setBackground(returnValue.getBackground());
95         label.setForeground(returnValue.getForeground());
96         label.setFont(returnValue.getFont());
97         returnValue = label;
98         }
99     }
100
101
102     FolderTableModel ftm = null;
103
104     if (table.getModel() instanceof FolderTableModel) {
105         ftm = (FolderTableModel) table.getModel();
106         MessageProxy msg = ftm.getMessageProxy(row);
107
108         if ( msg != null) {
109         if (! (msg.isSeen())) {
110             if (getUnreadFont() != null) {
111             returnValue.setFont(getUnreadFont());
112             } else {
113             // create the new font.
114
String JavaDoc fontStyle = Pooka.getProperty("FolderTable.UnreadStyle", "");
115             Font f = null;
116             
117             if (fontStyle.equalsIgnoreCase("BOLD"))
118                 f = returnValue.getFont().deriveFont(Font.BOLD);
119             else if (fontStyle.equalsIgnoreCase("ITALIC"))
120                 f = returnValue.getFont().deriveFont(Font.ITALIC);
121             
122             if (f == null)
123                 f = returnValue.getFont();
124             
125             setUnreadFont(f);
126             returnValue.setFont(f);
127             }
128         }
129
130         if (msg.getMessageInfo().getFolderInfo() instanceof CachingFolderInfo) {
131             CachingFolderInfo cfi = (CachingFolderInfo) msg.getMessageInfo().getFolderInfo();
132             if (cfi.showCacheInfo()) {
133             CachingMimeMessage cmm = (CachingMimeMessage) msg.getMessageInfo().getMessage();
134             long uid = cmm.getUID();
135             if (cfi.isCached(uid)) {
136                 returnValue.setForeground(getDefaultColor());
137             } else {
138                 returnValue.setForeground(getUncachedColor());
139             }
140             } else {
141             returnValue.setForeground(getDefaultColor());
142             }
143         }
144         }
145
146     }
147
148     return returnValue;
149     }
150 } //end class DefaultFolderCellRenderer
151
Popular Tags