001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.calendar.ui.comp;
019:
020: import java.awt.Color;
021: import java.awt.Component;
022: import java.awt.Graphics2D;
023: import java.awt.image.BufferedImage;
024: import java.util.Enumeration;
025:
026: import javax.swing.DefaultListCellRenderer;
027: import javax.swing.Icon;
028: import javax.swing.ImageIcon;
029: import javax.swing.JComboBox;
030: import javax.swing.JList;
031:
032: import org.columba.calendar.base.api.ICalendarItem;
033: import org.columba.calendar.config.Config;
034: import org.columba.calendar.config.api.ICalendarList;
035:
036: public class CalendarPicker extends JComboBox {
037:
038: // private Hashtable<String, String> table = new Hashtable<String,
039: // String>(10);
040:
041: public CalendarPicker() {
042: super ();
043:
044: ICalendarList list = Config.getInstance().getCalendarList();
045: Enumeration<ICalendarItem> e = list.getElements();
046: while (e.hasMoreElements()) {
047: ICalendarItem item = e.nextElement();
048: addItem(item);
049:
050: // table.put(item.getId(), item.getName());
051: }
052:
053: setSelectedIndex(0);
054:
055: // custom renderer to convert from calendar id to calendar name
056: setRenderer(new MyListCellRenderer());
057:
058: }
059:
060: class MyListCellRenderer extends DefaultListCellRenderer {
061:
062: MyListCellRenderer() {
063:
064: }
065:
066: /**
067: * @see javax.swing.DefaultListCellRenderer#getListCellRendererComponent(javax.swing.JList,
068: * java.lang.Object, int, boolean, boolean)
069: */
070: @Override
071: public Component getListCellRendererComponent(JList list,
072: Object value, int index, boolean isSelected,
073: boolean cellHasFocus) {
074:
075: super .getListCellRendererComponent(list, value, index,
076: isSelected, cellHasFocus);
077:
078: ICalendarItem item = (ICalendarItem) value;
079: String name = item.getName();
080:
081: setText(name);
082: setIcon(createIcon(item.getColor()));
083:
084: return this ;
085: }
086:
087: }
088:
089: private Icon createIcon(Color color) {
090: int width = 16;
091: int height = 16;
092: BufferedImage image = new BufferedImage(width, height,
093: BufferedImage.TYPE_INT_ARGB);
094:
095: Graphics2D graphics = (Graphics2D) image.getGraphics();
096: graphics.setColor(darker(color));
097: graphics.drawRect(1, 1, width - 3, height - 3);
098: graphics.setColor(color);
099: graphics.fillRect(2, 2, width - 4, height - 4);
100: graphics.dispose();
101:
102: return new ImageIcon(image);
103: }
104:
105: private final static double FACTOR = 0.90;
106:
107: private Color darker(Color c) {
108: return new Color(Math.max((int) (c.getRed() * FACTOR), 0), Math
109: .max((int) (c.getGreen() * FACTOR), 0), Math.max(
110: (int) (c.getBlue() * FACTOR), 0));
111: }
112:
113: }
|