KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > table > action > ColorMessageMenu


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.mail.gui.table.action;
19
20 import java.awt.Color JavaDoc;
21 import java.awt.Graphics2D JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.awt.image.BufferedImage JavaDoc;
25
26 import javax.swing.Icon JavaDoc;
27 import javax.swing.ImageIcon JavaDoc;
28 import javax.swing.JMenuItem JavaDoc;
29
30 import org.columba.api.gui.frame.IFrameMediator;
31 import org.columba.api.selection.ISelectionListener;
32 import org.columba.api.selection.SelectionChangedEvent;
33 import org.columba.core.command.CommandProcessor;
34 import org.columba.core.gui.menu.IMenu;
35 import org.columba.mail.command.IMailFolderCommandReference;
36 import org.columba.mail.folder.command.ColorMessageCommand;
37 import org.columba.mail.gui.frame.MailFrameMediator;
38 import org.columba.mail.gui.table.selection.TableSelectionChangedEvent;
39 import org.columba.mail.util.MailResourceLoader;
40
41 /**
42  * Creates a menu with a list of colors to select.
43  *
44  * @author fdietz
45  */

46
47
48 public class ColorMessageMenu extends IMenu implements ActionListener JavaDoc,
49         ISelectionListener {
50     // TODO (@author fdietz): add central place, which keeps a list of all possible
51
// colors, and provides a custom color configuration possibility
52
public static String JavaDoc[] items = {
53             MailResourceLoader.getString("dialog", "color", "blue"),
54             MailResourceLoader.getString("dialog", "color", "gray"),
55             MailResourceLoader.getString("dialog", "color", "green"),
56             MailResourceLoader.getString("dialog", "color", "red"),
57             MailResourceLoader.getString("dialog", "color", "yellow"),
58             MailResourceLoader.getString("dialog", "color", "custom") };
59
60     public static Color JavaDoc[] colors = { Color.blue, Color.gray, Color.green,
61             Color.red, Color.yellow, Color.black };
62
63     /**
64      * @param controller
65      * @param caption
66      */

67     public ColorMessageMenu(IFrameMediator controller) {
68         super(controller, MailResourceLoader.getString("dialog", "color",
69                 "menu_color_message"),"menu_color_message");
70
71         createSubMenu();
72
73         ((MailFrameMediator) controller).registerTableSelectionListener(this);
74     }
75
76     protected void createSubMenu() {
77         // TODO (@author fdietz): implement custom menuitem renderer
78
JMenuItem JavaDoc item = new JMenuItem JavaDoc(MailResourceLoader.getString("dialog",
79                 "color", "none"));
80         item.setActionCommand("NONE");
81         item.addActionListener(this);
82         add(item);
83         addSeparator();
84
85         for (int i = 0; i < items.length; i++) {
86             item = new JMenuItem JavaDoc(items[i]);
87             item.setIcon(createIcon(colors[i]));
88             item.setActionCommand(items[i]);
89             item.addActionListener(this);
90             add(item);
91         }
92     }
93
94     public void actionPerformed(ActionEvent JavaDoc e) {
95         String JavaDoc action = e.getActionCommand();
96
97         // get current message list selection
98
IMailFolderCommandReference r = ((MailFrameMediator) getFrameMediator())
99                 .getTableSelection();
100
101         if (action.equals("NONE")) {
102             // remove color
103
// add color selection to reference
104

105             r.setColorValue(0);
106
107             // pass command to scheduler
108
CommandProcessor.getInstance().addOp(new ColorMessageCommand(r));
109         } else {
110             // which menuitem was selected?
111
int result = -1;
112
113             for (int i = 0; i < items.length; i++) {
114                 if (action.equals(items[i])) {
115                     result = i;
116                     break;
117                 }
118             }
119
120             // add color selection to reference
121

122             r.setColorValue(colors[result].getRGB());
123
124             // pass command to scheduler
125
CommandProcessor.getInstance().addOp(new ColorMessageCommand(r));
126         }
127     }
128
129     public void selectionChanged(SelectionChangedEvent e) {
130         if (((TableSelectionChangedEvent) e).getUids().length > 0) {
131             setEnabled(true);
132         } else {
133             setEnabled(false);
134         }
135     }
136     
137     private Icon JavaDoc createIcon(Color JavaDoc color) {
138         int width = 16;
139         int height = 16;
140         BufferedImage JavaDoc image = new BufferedImage JavaDoc(width, height,
141                 BufferedImage.TYPE_INT_ARGB);
142
143         Graphics2D JavaDoc graphics = (Graphics2D JavaDoc) image.getGraphics();
144         graphics.setColor(darker(color));
145         graphics.drawRect(1, 1, width - 3, height - 3);
146         graphics.setColor(color);
147         graphics.fillRect(2, 2, width - 4, height - 4);
148         graphics.dispose();
149
150         return new ImageIcon JavaDoc(image);
151     }
152
153     private final static double FACTOR = 0.90;
154
155     private Color JavaDoc darker(Color JavaDoc c) {
156         return new Color JavaDoc(Math.max((int) (c.getRed() * FACTOR), 0), Math.max(
157                 (int) (c.getGreen() * FACTOR), 0), Math.max(
158                 (int) (c.getBlue() * FACTOR), 0));
159     }
160 }
161
162
Popular Tags