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.mail.gui.table.action;
019:
020: import java.awt.Color;
021: import java.awt.Graphics2D;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ActionListener;
024: import java.awt.image.BufferedImage;
025:
026: import javax.swing.Icon;
027: import javax.swing.ImageIcon;
028: import javax.swing.JMenuItem;
029:
030: import org.columba.api.gui.frame.IFrameMediator;
031: import org.columba.api.selection.ISelectionListener;
032: import org.columba.api.selection.SelectionChangedEvent;
033: import org.columba.core.command.CommandProcessor;
034: import org.columba.core.gui.menu.IMenu;
035: import org.columba.mail.command.IMailFolderCommandReference;
036: import org.columba.mail.folder.command.ColorMessageCommand;
037: import org.columba.mail.gui.frame.MailFrameMediator;
038: import org.columba.mail.gui.table.selection.TableSelectionChangedEvent;
039: import org.columba.mail.util.MailResourceLoader;
040:
041: /**
042: * Creates a menu with a list of colors to select.
043: *
044: * @author fdietz
045: */
046:
047: public class ColorMessageMenu extends IMenu implements ActionListener,
048: ISelectionListener {
049: // TODO (@author fdietz): add central place, which keeps a list of all possible
050: // colors, and provides a custom color configuration possibility
051: public static String[] items = {
052: MailResourceLoader.getString("dialog", "color", "blue"),
053: MailResourceLoader.getString("dialog", "color", "gray"),
054: MailResourceLoader.getString("dialog", "color", "green"),
055: MailResourceLoader.getString("dialog", "color", "red"),
056: MailResourceLoader.getString("dialog", "color", "yellow"),
057: MailResourceLoader.getString("dialog", "color", "custom") };
058:
059: public static Color[] colors = { Color.blue, Color.gray,
060: Color.green, Color.red, Color.yellow, Color.black };
061:
062: /**
063: * @param controller
064: * @param caption
065: */
066: public ColorMessageMenu(IFrameMediator controller) {
067: super (controller, MailResourceLoader.getString("dialog",
068: "color", "menu_color_message"), "menu_color_message");
069:
070: createSubMenu();
071:
072: ((MailFrameMediator) controller)
073: .registerTableSelectionListener(this );
074: }
075:
076: protected void createSubMenu() {
077: // TODO (@author fdietz): implement custom menuitem renderer
078: JMenuItem item = new JMenuItem(MailResourceLoader.getString(
079: "dialog", "color", "none"));
080: item.setActionCommand("NONE");
081: item.addActionListener(this );
082: add(item);
083: addSeparator();
084:
085: for (int i = 0; i < items.length; i++) {
086: item = new JMenuItem(items[i]);
087: item.setIcon(createIcon(colors[i]));
088: item.setActionCommand(items[i]);
089: item.addActionListener(this );
090: add(item);
091: }
092: }
093:
094: public void actionPerformed(ActionEvent e) {
095: String action = e.getActionCommand();
096:
097: // get current message list selection
098: IMailFolderCommandReference r = ((MailFrameMediator) getFrameMediator())
099: .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(
109: new ColorMessageCommand(r));
110: } else {
111: // which menuitem was selected?
112: int result = -1;
113:
114: for (int i = 0; i < items.length; i++) {
115: if (action.equals(items[i])) {
116: result = i;
117: break;
118: }
119: }
120:
121: // add color selection to reference
122:
123: r.setColorValue(colors[result].getRGB());
124:
125: // pass command to scheduler
126: CommandProcessor.getInstance().addOp(
127: new ColorMessageCommand(r));
128: }
129: }
130:
131: public void selectionChanged(SelectionChangedEvent e) {
132: if (((TableSelectionChangedEvent) e).getUids().length > 0) {
133: setEnabled(true);
134: } else {
135: setEnabled(false);
136: }
137: }
138:
139: private Icon createIcon(Color color) {
140: int width = 16;
141: int height = 16;
142: BufferedImage image = new BufferedImage(width, height,
143: BufferedImage.TYPE_INT_ARGB);
144:
145: Graphics2D graphics = (Graphics2D) image.getGraphics();
146: graphics.setColor(darker(color));
147: graphics.drawRect(1, 1, width - 3, height - 3);
148: graphics.setColor(color);
149: graphics.fillRect(2, 2, width - 4, height - 4);
150: graphics.dispose();
151:
152: return new ImageIcon(image);
153: }
154:
155: private final static double FACTOR = 0.90;
156:
157: private Color darker(Color c) {
158: return new Color(Math.max((int) (c.getRed() * FACTOR), 0), Math
159: .max((int) (c.getGreen() * FACTOR), 0), Math.max(
160: (int) (c.getBlue() * FACTOR), 0));
161: }
162: }
|