01: //The contents of this file are subject to the Mozilla Public License Version 1.1
02: //(the "License"); you may not use this file except in compliance with the
03: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
04: //
05: //Software distributed under the License is distributed on an "AS IS" basis,
06: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
07: //for the specific language governing rights and
08: //limitations under the License.
09: //
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;
20: import java.awt.Component;
21: import java.awt.Font;
22: import java.awt.Graphics2D;
23: import java.awt.image.BufferedImage;
24:
25: import javax.swing.BorderFactory;
26: import javax.swing.Icon;
27: import javax.swing.ImageIcon;
28: import javax.swing.JTable;
29: import javax.swing.table.DefaultTableCellRenderer;
30:
31: import org.columba.calendar.base.api.ICalendarItem;
32:
33: /**
34: *
35: *
36: * @author fdietz
37: */
38:
39: public class DefaultStringRenderer extends DefaultTableCellRenderer {
40:
41: private Font font;
42:
43: public DefaultStringRenderer() {
44: //setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 4));
45: }
46:
47: public Component getTableCellRendererComponent(JTable table,
48: Object value, boolean isSelected, boolean hasFocus,
49: int row, int column) {
50:
51: if (isSelected) {
52: setForeground(table.getSelectionForeground());
53: setBackground(table.getSelectionBackground());
54: } else {
55: setForeground(table.getForeground());
56: setBackground(table.getBackground());
57: }
58:
59: if (font == null) {
60: font = getFont();
61: font = font.deriveFont(Font.PLAIN);
62: }
63:
64: setFont(font);
65:
66: ICalendarItem item = (ICalendarItem) value;
67:
68: setText(item.getName());
69: setIcon(createIcon(item.getColor()));
70:
71: return this ;
72: }
73:
74: private Icon createIcon(Color color) {
75: int width = 16;
76: int height = 16;
77: BufferedImage image = new BufferedImage(width, height,
78: BufferedImage.TYPE_INT_ARGB);
79:
80: Graphics2D graphics = (Graphics2D) image.getGraphics();
81: graphics.setColor(darker(color));
82: graphics.drawRect(1, 1, width - 3, height - 3);
83: graphics.setColor(color);
84: graphics.fillRect(2, 2, width - 4, height - 4);
85: graphics.dispose();
86:
87: return new ImageIcon(image);
88: }
89:
90: private final static double FACTOR = 0.90;
91:
92: private Color darker(Color c) {
93: return new Color(Math.max((int) (c.getRed() * FACTOR), 0), Math
94: .max((int) (c.getGreen() * FACTOR), 0), Math.max(
95: (int) (c.getBlue() * FACTOR), 0));
96: }
97: }
|