01: package org.columba.calendar.ui.box;
02:
03: import java.awt.Color;
04: import java.awt.Graphics2D;
05: import java.awt.event.ActionListener;
06: import java.awt.image.BufferedImage;
07: import java.util.Enumeration;
08:
09: import javax.swing.ButtonGroup;
10: import javax.swing.Icon;
11: import javax.swing.ImageIcon;
12: import javax.swing.JPopupMenu;
13: import javax.swing.JRadioButtonMenuItem;
14:
15: import org.columba.calendar.base.api.ICalendarItem;
16: import org.columba.calendar.config.Config;
17: import org.columba.calendar.config.api.ICalendarList;
18:
19: public class CalendarMenu extends JPopupMenu {
20:
21: private ButtonGroup group = new ButtonGroup();
22:
23: public CalendarMenu(ActionListener l) {
24:
25: ICalendarList list = Config.getInstance().getCalendarList();
26: Enumeration<ICalendarItem> e = list.getElements();
27: while (e.hasMoreElements()) {
28: ICalendarItem folder = e.nextElement();
29:
30: JRadioButtonMenuItem item = createMenuItem(folder);
31: item.addActionListener(l);
32: group.add(item);
33: add(item);
34: }
35:
36: // select first item
37: JRadioButtonMenuItem item = (JRadioButtonMenuItem) getComponent(0);
38: item.setSelected(true);
39: }
40:
41: private JRadioButtonMenuItem createMenuItem(ICalendarItem folder) {
42: JRadioButtonMenuItem item = new JRadioButtonMenuItem();
43: item.setActionCommand(folder.getId());
44: item.setText(folder.getName());
45: item.setIcon(createIcon(folder.getColor()));
46: return item;
47: }
48:
49: private Icon createIcon(Color color) {
50: int width = 16;
51: int height = 16;
52: BufferedImage image = new BufferedImage(width, height,
53: BufferedImage.TYPE_INT_ARGB);
54:
55: Graphics2D graphics = (Graphics2D) image.getGraphics();
56: graphics.setColor(darker(color));
57: graphics.drawRect(1, 1, width - 3, height - 3);
58: graphics.setColor(color);
59: graphics.fillRect(2, 2, width - 4, height - 4);
60: graphics.dispose();
61:
62: return new ImageIcon(image);
63: }
64:
65: private final static double FACTOR = 0.90;
66:
67: private Color darker(Color c) {
68: return new Color(Math.max((int) (c.getRed() * FACTOR), 0), Math
69: .max((int) (c.getGreen() * FACTOR), 0), Math.max(
70: (int) (c.getBlue() * FACTOR), 0));
71: }
72:
73: }
|