1 package org.columba.calendar.ui.comp; 19 20 import java.awt.Color ; 21 import java.awt.Component ; 22 import java.awt.Graphics2D ; 23 import java.awt.image.BufferedImage ; 24 import java.util.Enumeration ; 25 26 import javax.swing.DefaultListCellRenderer ; 27 import javax.swing.Icon ; 28 import javax.swing.ImageIcon ; 29 import javax.swing.JComboBox ; 30 import javax.swing.JList ; 31 32 import org.columba.calendar.base.api.ICalendarItem; 33 import org.columba.calendar.config.Config; 34 import org.columba.calendar.config.api.ICalendarList; 35 36 public class CalendarPicker extends JComboBox { 37 38 41 public CalendarPicker() { 42 super(); 43 44 ICalendarList list = Config.getInstance().getCalendarList(); 45 Enumeration <ICalendarItem> e = list.getElements(); 46 while (e.hasMoreElements()) { 47 ICalendarItem item = e.nextElement(); 48 addItem(item); 49 50 } 52 53 setSelectedIndex(0); 54 55 setRenderer(new MyListCellRenderer()); 57 58 } 59 60 class MyListCellRenderer extends DefaultListCellRenderer { 61 62 MyListCellRenderer() { 63 64 } 65 66 70 @Override 71 public Component getListCellRendererComponent(JList list, Object value, 72 int index, boolean isSelected, boolean cellHasFocus) { 73 74 super.getListCellRendererComponent(list, value, index, isSelected, 75 cellHasFocus); 76 77 ICalendarItem item = (ICalendarItem) value; 78 String name = item.getName(); 79 80 setText(name); 81 setIcon(createIcon(item.getColor())); 82 83 return this; 84 } 85 86 } 87 88 private Icon createIcon(Color color) { 89 int width = 16; 90 int height = 16; 91 BufferedImage image = new BufferedImage (width, height, 92 BufferedImage.TYPE_INT_ARGB); 93 94 Graphics2D graphics = (Graphics2D ) image.getGraphics(); 95 graphics.setColor(darker(color)); 96 graphics.drawRect(1, 1, width - 3, height - 3); 97 graphics.setColor(color); 98 graphics.fillRect(2, 2, width - 4, height - 4); 99 graphics.dispose(); 100 101 return new ImageIcon (image); 102 } 103 104 private final static double FACTOR = 0.90; 105 106 private Color darker(Color c) { 107 return new Color (Math.max((int) (c.getRed() * FACTOR), 0), Math.max( 108 (int) (c.getGreen() * FACTOR), 0), Math.max( 109 (int) (c.getBlue() * FACTOR), 0)); 110 } 111 112 } 113 | Popular Tags |