KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > table > plugins > DateRenderer


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.plugins;
17
18 import java.awt.Component JavaDoc;
19 import java.awt.Font JavaDoc;
20 import java.text.DateFormat JavaDoc;
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.TimeZone JavaDoc;
25
26 import javax.swing.JTable JavaDoc;
27 import javax.swing.UIManager JavaDoc;
28
29 import org.columba.mail.gui.table.model.MessageNode;
30
31
32 public class DateRenderer extends DefaultLabelRenderer {
33     static SimpleDateFormat JavaDoc dfWeek = new SimpleDateFormat JavaDoc("EEE HH:mm", Locale
34             .getDefault());
35
36     // use local date settings
37
DateFormat JavaDoc dfCommon = DateFormat.getDateInstance();
38
39     static final long OneDay = 24 * 60 * 60 * 1000;
40
41     static TimeZone JavaDoc localTimeZone = TimeZone.getDefault();
42
43     private Font JavaDoc boldFont;
44
45     public DateRenderer() {
46         super();
47
48         // setOpaque(true); //MUST do this for background to show up.
49
boldFont = UIManager.getFont("Tree.font");
50         boldFont = boldFont.deriveFont(Font.BOLD);
51
52     }
53
54     public void updateUI() {
55         super.updateUI();
56
57         boldFont = UIManager.getFont("Tree.font");
58         boldFont = boldFont.deriveFont(Font.BOLD);
59
60     }
61
62     public static int getLocalDaysDiff(long t) {
63         return (int) (((System.currentTimeMillis() + localTimeZone
64                 .getRawOffset()) - (((t + localTimeZone.getRawOffset()) / OneDay) * OneDay)) / OneDay);
65     }
66
67     public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value,
68             boolean isSelected, boolean hasFocus, int row, int column) {
69
70         super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
71                 row, column);
72
73         if (value == null) {
74             setText("");
75
76             return this;
77         }
78
79         Date JavaDoc date = (Date JavaDoc) ((MessageNode) value).getHeader()
80                 .get("columba.date");
81
82         if (date == null) {
83             return this;
84         }
85
86         int diff = getLocalDaysDiff(date.getTime());
87
88         // if ( today
89
if ((diff >= 0) && (diff < 7)) {
90             setText(dfWeek.format(date));
91         } else {
92             setText(dfCommon.format(date));
93         }
94
95         return this;
96     }
97 }
98
Popular Tags