KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > calendar > ui > list > DefaultStringRenderer


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

17 package org.columba.calendar.ui.list;
18
19 import java.awt.Color JavaDoc;
20 import java.awt.Component JavaDoc;
21 import java.awt.Font JavaDoc;
22 import java.awt.Graphics2D JavaDoc;
23 import java.awt.image.BufferedImage JavaDoc;
24
25 import javax.swing.BorderFactory JavaDoc;
26 import javax.swing.Icon JavaDoc;
27 import javax.swing.ImageIcon JavaDoc;
28 import javax.swing.JTable JavaDoc;
29 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
30
31 import org.columba.calendar.base.api.ICalendarItem;
32
33 /**
34  *
35  *
36  * @author fdietz
37  */

38
39 public class DefaultStringRenderer extends DefaultTableCellRenderer JavaDoc {
40
41     private Font JavaDoc font;
42
43     public DefaultStringRenderer() {
44         //setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 4));
45
}
46
47     public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value,
48             boolean isSelected, boolean hasFocus, int row, int column) {
49
50         if (isSelected) {
51             setForeground(table.getSelectionForeground());
52             setBackground(table.getSelectionBackground());
53         } else {
54             setForeground(table.getForeground());
55             setBackground(table.getBackground());
56         }
57
58         if (font == null) {
59             font = getFont();
60             font = font.deriveFont(Font.PLAIN);
61         }
62
63         setFont(font);
64
65         ICalendarItem item = (ICalendarItem) value;
66         
67         setText(item.getName());
68         setIcon(createIcon(item.getColor()));
69         
70         return this;
71     }
72     
73     private Icon JavaDoc createIcon(Color JavaDoc color) {
74         int width = 16;
75         int height = 16;
76         BufferedImage JavaDoc image = new BufferedImage JavaDoc(width, height,
77                 BufferedImage.TYPE_INT_ARGB);
78
79         Graphics2D JavaDoc graphics = (Graphics2D JavaDoc) image.getGraphics();
80         graphics.setColor(darker(color));
81         graphics.drawRect(1, 1, width - 3, height - 3);
82         graphics.setColor(color);
83         graphics.fillRect(2, 2, width - 4, height - 4);
84         graphics.dispose();
85
86         return new ImageIcon JavaDoc(image);
87     }
88
89     private final static double FACTOR = 0.90;
90
91     private Color JavaDoc darker(Color JavaDoc c) {
92         return new Color JavaDoc(Math.max((int) (c.getRed() * FACTOR), 0), Math.max(
93                 (int) (c.getGreen() * FACTOR), 0), Math.max(
94                 (int) (c.getBlue() * FACTOR), 0));
95     }
96 }
97
Popular Tags