1 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 38 39 public class DefaultStringRenderer extends DefaultTableCellRenderer { 40 41 private Font font; 42 43 public DefaultStringRenderer() { 44 } 46 47 public Component getTableCellRendererComponent(JTable table, Object 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 createIcon(Color color) { 74 int width = 16; 75 int height = 16; 76 BufferedImage image = new BufferedImage (width, height, 77 BufferedImage.TYPE_INT_ARGB); 78 79 Graphics2D graphics = (Graphics2D ) 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 (image); 87 } 88 89 private final static double FACTOR = 0.90; 90 91 private Color darker(Color c) { 92 return new Color (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 |