KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > forum > gui > MessageRenderer


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.forum.gui;
20
21 import java.awt.*;
22 import javax.swing.*;
23 import javax.swing.tree.*;
24
25 import org.lucane.applications.forum.ForumPlugin;
26 import org.lucane.applications.forum.model.ForumMessage;
27
28 public class MessageRenderer extends DefaultTreeCellRenderer
29 {
30     private long lastRefresh;
31     
32     private Icon normalIcon;
33     private Icon newIcon;
34     private Icon deletedIcon;
35     
36     public MessageRenderer(ForumPlugin plugin, String JavaDoc forum)
37     {
38         this.lastRefresh = plugin.getLastRefreshTime(forum);
39         
40         this.normalIcon = plugin.getImageIcon("normal.png");
41         this.newIcon = plugin.getImageIcon("new.png");
42         this.deletedIcon = plugin.getImageIcon("deleted.png");
43     }
44         
45     public Component getTreeCellRendererComponent(JTree tree,
46             Object JavaDoc value, boolean sel, boolean expanded, boolean leaf,
47             int row, boolean hasFocus)
48     {
49         JLabel label = (JLabel)super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
50         
51         //a few checks
52
if(! (value instanceof DefaultMutableTreeNode))
53             return label;
54         
55         DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
56         if(! (node.getUserObject() instanceof ForumMessage))
57             return label;
58         
59         //use a custom icon depending on the message date
60
ForumMessage message = (ForumMessage)node.getUserObject();
61         if(message.isVisible())
62         {
63             if(message.getDate().getTime() > lastRefresh)
64                 label.setIcon(newIcon);
65             else
66                 label.setIcon(normalIcon);
67         }
68         else
69             label.setIcon(deletedIcon);
70                 
71         JLabel author = new JLabel("[" + message.getAuthor() + "]");
72         author.setFont(label.getFont());
73         author.setForeground(Color.GRAY);
74         
75         JPanel pnl = new JPanel(new BorderLayout());
76         pnl.add(label, BorderLayout.CENTER);
77         pnl.add(author, BorderLayout.EAST);
78         pnl.setOpaque(false);
79         
80         return pnl;
81     }
82 }
Popular Tags