KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > view > FolderViewTableCellRenderer


1 package SnowMailClient.view;
2
3 import snow.sortabletable.*;
4 import SnowMailClient.model.MailMessage;
5 import SnowMailClient.SnowMailClientApp;
6 import SnowMailClient.SpamFilter.*;
7 import snow.lookandfeel.*;
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 FolderViewTableCellRenderer extends JLabel implements TableCellRenderer
19 {
20   Font normalFont, boldFont;
21   FolderView folderView;
22   SortableTableModel sortableTableModel;
23
24
25   /**
26   */

27   public FolderViewTableCellRenderer(FolderView folderView, // basic fine grain table model
28
SortableTableModel sortableTableModel ) // sortable model (view)
29
{
30     super();
31     this.folderView = folderView;
32     this.sortableTableModel = sortableTableModel;
33
34     setup();
35   } // Constructor
36

37   /**
38    * Notification from the <code>UIManager</code> that the look and feel
39    * [L&F] has changed.
40    * Replaces the current UI object with the latest version from the
41    * <code>UIManager</code>.
42    */

43   public void updateUI()
44   {
45      super.updateUI();
46      setup();
47   }
48
49   private void setup()
50   {
51      normalFont = UIManager.getFont("Table.font");
52      int fontSize = normalFont.getSize();
53      boldFont = //new Font(normalFont.getFontName(), Font.BOLD, fontSize);
54
normalFont.deriveFont(Font.BOLD);
55      Border emptyBorder = new EmptyBorder(fontSize/4, fontSize/2, fontSize/4, fontSize/2);
56      this.setBorder(emptyBorder);
57
58   }
59
60   public Component getTableCellRendererComponent(
61          JTable table, Object JavaDoc value, boolean isSelected, boolean hasFocus,
62          int row, int viewColumn)
63   {
64      String JavaDoc text = value.toString();
65      this.setText(text);
66      setOpaque(false);
67
68      int ind = sortableTableModel.getIndexInUnsortedFromTablePos(row);
69
70      if(this.folderView.getMailFolder()==null) return this; // ###
71

72      MailMessage mess = this.folderView.getMailFolder().getMessageAt(ind);
73      boolean newMail = mess.getIsNew();
74      if(newMail)
75      {
76        setFont(this.boldFont);
77      }
78      else
79      {
80        this.setFont(normalFont);
81      }
82
83      // reordering or visibility switch may have changed index maping !!
84
int column = sortableTableModel.getColumnForViewIndex(viewColumn);
85
86      this.setForeground(UIManager.getColor("Tree.selectionForeground"));
87
88      // selection
89
if(isSelected) // && column!=0 && column!=5)
90
{
91         setBackground(UIManager.getColor("Tree.selectionBackground"));
92         setOpaque(true);
93      }
94      else
95      {
96         setOpaque(false);
97      }
98
99      if(true)
100      {
101         if(column==0)
102         {
103           // from background => spam or white list info
104
String JavaDoc from = mess.getFromAddress().getMailAddress();
105           if(SnowMailClientApp.getInstance().getAddressBook().getAddress(from)!=null)
106           {
107              if(isSelected)
108              {
109                setBackground(ThemesManager.getInstance().getGreen().darker());
110              }
111              else
112              {
113                setBackground(ThemesManager.getInstance().getGreen());
114              }
115              setOpaque(true);
116           }
117           else if(SnowMailClientApp.getInstance().getSpamBook().getAddress(from)!=null)
118           {
119              if(isSelected)
120              {
121                setBackground(ThemesManager.getInstance().getRed().darker());
122              }
123              else
124              {
125                setBackground(ThemesManager.getInstance().getRed());
126              }
127              setOpaque(true);
128           }
129           else
130           {
131              if(isSelected)
132              {
133                setBackground(UIManager.getColor("Tree.selectionBackground"));
134                setOpaque(true);
135              }
136              else
137              {
138                setOpaque(false);
139              }
140           }
141         }
142
143         else if(column==4) // size
144
{
145            if(mess.getSize()>500000)
146            {
147              setForeground(ThemesManager.getInstance().getRed().darker());
148            }
149         }
150
151         else if(column==5) // spam filter
152
{
153           this.setFont(normalFont);
154           double prob = mess.getSPAMProbability();
155           if(WordStatistic.isSpam(prob))
156           {
157              setText("S");
158           }
159           else
160           {
161              setText("");
162           }
163
164           if(mess.getIsFalsePositive())
165           {
166              // is HAM but spam prob great => false positive.
167
//
168
setOpaque(true);
169              setText("F");
170              //setForeground(Color.red);
171
setBackground( Color.RED ); // saturated red !
172
}
173           else if(mess.getIsSPAM())
174           {
175              setOpaque(true);
176              if(isSelected)
177              {
178                setBackground(ThemesManager.getInstance().getRed().darker());
179              }
180              else
181              {
182                setBackground(ThemesManager.getInstance().getRed());
183              }
184           }
185           else if(mess.getIsHAM())
186           {
187              setOpaque(true);
188              if(isSelected)
189              {
190                setBackground(ThemesManager.getInstance().getGreen().darker());
191              }
192              else
193              {
194                setBackground(ThemesManager.getInstance().getGreen());
195              }
196           }
197           else
198           {
199              setOpaque(false); // no background
200
}
201
202         }
203      }
204
205
206      return this;
207   }
208
209
210
211 } // FolderViewTableCellRenderer
Popular Tags