KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > calendar > ui > comp > CalendarPicker


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.calendar.ui.comp;
19
20 import java.awt.Color JavaDoc;
21 import java.awt.Component JavaDoc;
22 import java.awt.Graphics2D JavaDoc;
23 import java.awt.image.BufferedImage JavaDoc;
24 import java.util.Enumeration JavaDoc;
25
26 import javax.swing.DefaultListCellRenderer JavaDoc;
27 import javax.swing.Icon JavaDoc;
28 import javax.swing.ImageIcon JavaDoc;
29 import javax.swing.JComboBox JavaDoc;
30 import javax.swing.JList JavaDoc;
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 JavaDoc {
37
38     // private Hashtable<String, String> table = new Hashtable<String,
39
// String>(10);
40

41     public CalendarPicker() {
42         super();
43
44         ICalendarList list = Config.getInstance().getCalendarList();
45         Enumeration JavaDoc<ICalendarItem> e = list.getElements();
46         while (e.hasMoreElements()) {
47             ICalendarItem item = e.nextElement();
48             addItem(item);
49
50             // table.put(item.getId(), item.getName());
51
}
52
53         setSelectedIndex(0);
54
55         // custom renderer to convert from calendar id to calendar name
56
setRenderer(new MyListCellRenderer());
57
58     }
59
60     class MyListCellRenderer extends DefaultListCellRenderer JavaDoc {
61
62         MyListCellRenderer() {
63
64         }
65
66         /**
67          * @see javax.swing.DefaultListCellRenderer#getListCellRendererComponent(javax.swing.JList,
68          * java.lang.Object, int, boolean, boolean)
69          */

70         @Override JavaDoc
71         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc 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 JavaDoc name = item.getName();
79
80             setText(name);
81             setIcon(createIcon(item.getColor()));
82
83             return this;
84         }
85
86     }
87
88     private Icon JavaDoc createIcon(Color JavaDoc color) {
89         int width = 16;
90         int height = 16;
91         BufferedImage JavaDoc image = new BufferedImage JavaDoc(width, height,
92                 BufferedImage.TYPE_INT_ARGB);
93
94         Graphics2D JavaDoc graphics = (Graphics2D JavaDoc) 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 JavaDoc(image);
102     }
103     
104     private final static double FACTOR = 0.90;
105
106     private Color JavaDoc darker(Color JavaDoc c) {
107         return new Color JavaDoc(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