KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > SpamFilter > SpamStatTableCellRenderer


1 package SnowMailClient.SpamFilter;
2
3 import snow.sortabletable.*;
4 import snow.lookandfeel.*;
5 import SnowMailClient.model.MailMessage;
6 import SnowMailClient.SnowMailClientApp;
7 import SnowMailClient.view.FolderView;
8
9 import javax.swing.*;
10 import javax.swing.border.*;
11 import javax.swing.table.*;
12 import java.awt.*;
13 import java.beans.*;
14
15 /** DefaultTableCellRenderer seems not to like transparent bg ???
16  ### can be optmized for performance...
17 */

18 public final class SpamStatTableCellRenderer extends JLabel implements TableCellRenderer
19 {
20   Font normalFont, boldFont;
21   FolderView folderView;
22   SortableTableModel sortableTableModel;
23
24   private Color unknownBackground = new Color(180,180, 250);
25
26   public SpamStatTableCellRenderer(FolderView folderView,
27                                      SortableTableModel sortableTableModel )
28   {
29     super();
30     this.folderView = folderView;
31     this.sortableTableModel = sortableTableModel;
32
33     setup();
34   }
35
36   /**
37    * Notification from the <code>UIManager</code> that the look and feel
38    * [L&F] has changed.
39    * Replaces the current UI object with the latest version from the
40    * <code>UIManager</code>.
41    */

42   public void updateUI()
43   {
44      super.updateUI();
45      setup();
46   }
47
48   private void setup()
49   {
50      normalFont = UIManager.getFont("Label.font");
51      int fontSize = normalFont.getSize();
52      boldFont = new Font(normalFont.getFontName(), Font.BOLD, fontSize+2);
53      Border emptyBorder = new EmptyBorder(fontSize/4, fontSize/2, fontSize/4, fontSize/2);
54      this.setBorder(emptyBorder);
55
56   }
57
58   public Component getTableCellRendererComponent(
59          JTable table, Object JavaDoc value, boolean isSelected, boolean hasFocus,
60          int row, int column)
61   {
62      this.setText(value.toString());
63
64      int ind = sortableTableModel.getIndexInUnsortedFromTablePos(row);
65      boolean newMail = false;
66
67      MailMessage mess = this.folderView.getMailFolder().getMessageAt(ind);
68      newMail = mess.getIsNew();
69
70      // selection
71
if(isSelected && column!=5 && column!=0)
72      {
73         setBackground(UIManager.getColor("Tree.selectionBackground"));
74         setOpaque(true);
75      }
76      else
77      {
78         setOpaque(false);
79         if(column==0)
80         {
81           // from background => spam or white list info
82
String JavaDoc from = mess.getFromAddress().getMailAddress();
83           if(SnowMailClientApp.getInstance().getAddressBook().getAddress(from)!=null)
84           {
85              if(isSelected)
86              {
87                setBackground(ThemesManager.getInstance().getGreen().darker());
88              }
89              else
90              {
91                setBackground(ThemesManager.getInstance().getGreen());
92              }
93              setOpaque(true);
94           }
95           else if(SnowMailClientApp.getInstance().getSpamBook().getAddress(from)!=null)
96           {
97              if(isSelected)
98              {
99                setBackground(ThemesManager.getInstance().getRed().darker());
100              }
101              else
102              {
103                setBackground(ThemesManager.getInstance().getRed());
104              }
105              setOpaque(true);
106           }
107           else
108           {
109              if(isSelected)
110              {
111                setBackground(UIManager.getColor("Tree.selectionBackground"));
112                setOpaque(true);
113              }
114              else
115              {
116                setOpaque(false);
117              }
118           }
119         }
120         else if(column==5) // spam filter
121
{
122           setText("");
123           double prob = mess.getSPAMProbability();
124           if(WordStatistic.isSpam(prob))
125           {
126              if(isSelected)
127              {
128                setBackground(ThemesManager.getInstance().getRed().darker());
129              }
130              else
131              {
132                setBackground(ThemesManager.getInstance().getRed());
133              }
134              setOpaque(true);
135           }
136           else
137           {
138              setOpaque(false);
139           }
140
141         }
142      }
143
144      if(newMail)
145      {
146        setFont(this.boldFont);
147      }
148      else
149      {
150        this.setFont(normalFont);
151      }
152      return this;
153   }
154
155
156
157 }// SpamStatTableCellRenderer
Popular Tags