001: /**
002: * Copyright (c) 2006, Sun Microsystems, Inc
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * * Redistributions in binary form must reproduce the above
012: * copyright notice, this list of conditions and the following
013: * disclaimer in the documentation and/or other materials provided
014: * with the distribution.
015: * * Neither the name of the TimingFramework project nor the names of its
016: * contributors may be used to endorse or promote products derived
017: * from this software without specific prior written permission.
018: *
019: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
020: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
021: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
022: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
023: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
024: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
025: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
026: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
027: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
028: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
029: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.zilonis.tool.ext.mailman.ui;
031:
032: import java.awt.Color;
033: import java.awt.Component;
034: import java.awt.GradientPaint;
035: import java.awt.Graphics;
036: import java.awt.Graphics2D;
037: import java.awt.Rectangle;
038: import java.awt.RenderingHints;
039: import java.awt.event.ActionEvent;
040: import java.awt.event.ActionListener;
041: import java.awt.event.MouseEvent;
042: import java.util.HashMap;
043: import java.util.List;
044: import java.util.Map;
045:
046: import javax.swing.Icon;
047: import javax.swing.JCheckBoxMenuItem;
048: import javax.swing.JMenu;
049: import javax.swing.JMenuItem;
050: import javax.swing.JPopupMenu;
051: import javax.swing.JTable;
052: import javax.swing.UIManager;
053: import javax.swing.border.Border;
054: import javax.swing.border.EmptyBorder;
055: import javax.swing.table.DefaultTableCellRenderer;
056: import javax.swing.table.TableColumnModel;
057:
058: import org.zilonis.tool.ext.mailman.model.Contact;
059: import org.zilonis.tool.ext.mailman.model.Message;
060:
061: /**
062: *
063: * @author sky
064: */
065: public class MailTable extends JTable {
066: static final public Color STRIPE_COLOR = new Color(237, 242, 249);
067: private static final String[] COLOR_TITLES = new String[] {
068: "Deep Purple", "Red Crush", "Blue Experience", "Aerith" };
069: private static final Color[] COLORS = new Color[] {
070: new Color(170, 175, 230), new Color(210, 210, 255),
071: Color.BLUE, new Color(200, 175, 170),
072: new Color(250, 220, 220), Color.RED,
073: new Color(168, 204, 241), new Color(44, 61, 146),
074: Color.BLACK, Color.WHITE, new Color(64, 110, 161),
075: Color.WHITE };
076:
077: private boolean showTableStriping;
078:
079: private final Map<String, Integer> colorMap;
080:
081: public MailTable() {
082: colorMap = new HashMap<String, Integer>();
083: Border emptyBorder = new EmptyBorder(1, 1, 1, 1);
084: UIManager.put("Table.focusSelectedCellHighlightBorder",
085: emptyBorder);
086: UIManager.put("Table.focusCellHighlightBorder", emptyBorder);
087: setDragEnabled(true);
088: setName("mailTable");
089: }
090:
091: public boolean isCellEditable(int row, int column) {
092: return false;
093: }
094:
095: void bound() {
096: // setFont(getFont().deriveFont(Font.BOLD));
097: getTableHeader().setFont(
098: getFont().deriveFont(getFont().getSize() + 2f));
099: setRowHeight(22);
100: TableColumnModel cm = getColumnModel();
101: cm.getColumn(0).setMaxWidth(24);
102: cm.getColumn(1).setPreferredWidth(400);
103: cm.getColumn(1).setMaxWidth(400);
104: cm.getColumn(0).setHeaderValue("");
105: cm.getColumn(1).setHeaderValue("Subject");
106: cm.getColumn(2).setHeaderValue("Sender");
107: cm.getColumn(3).setHeaderValue("Date");
108: }
109:
110: protected void paintComponent(Graphics g) {
111: Rectangle clip = g.getClipBounds();
112: int rh = getRowHeight();
113: int startRow = clip.y / rh;
114: int endRow = (clip.y + clip.height) / rh + 1;
115: int w = getWidth();
116:
117: g.setColor(Color.WHITE);
118: g.fillRect(clip.x, clip.y, clip.width, clip.height);
119:
120: if (showTableStriping) {
121: g.setColor(STRIPE_COLOR);
122: for (int row = startRow / 2 * 2; row < endRow; row += 2) {
123: g.fillRect(0, rh * row, w, rh);
124: }
125: }
126:
127: Graphics uig = g.create();
128: getUI().paint(uig, this );
129: uig.dispose();
130: }
131:
132: protected void processMouseEvent(MouseEvent e) {
133: super .processMouseEvent(e);
134: if (!e.isConsumed() && e.isPopupTrigger()) {
135: showPopup(e);
136: }
137: }
138:
139: private void showPopup(MouseEvent e) {
140: getPopupMenu().show(this , e.getX(), e.getY());
141: }
142:
143: private JPopupMenu getPopupMenu() {
144: JPopupMenu popupMenu = new JPopupMenu();
145: JCheckBoxMenuItem stripeCB = new JCheckBoxMenuItem(
146: "Table Striping");
147: stripeCB.setSelected(getShowTableStriping());
148: stripeCB.addActionListener(new ActionListener() {
149: public void actionPerformed(ActionEvent e) {
150: setShowTableStriping(((JCheckBoxMenuItem) e.getSource())
151: .isSelected());
152: }
153: });
154: popupMenu.add(stripeCB);
155: JMenu colorizeMenu = (JMenu) popupMenu
156: .add(new JMenu("Colorize"));
157: if (getSelectedRowCount() != 0) {
158: ActionListener colorizeAL = new ActionListener() {
159: public void actionPerformed(ActionEvent e) {
160: JMenuItem mi = (JMenuItem) e.getSource();
161: colorize(mi.getParent().getComponentZOrder(mi));
162: }
163: };
164: for (int i = 0; i < COLOR_TITLES.length; i++) {
165: JMenuItem colorMenuItem = new JMenuItem(COLOR_TITLES[i]);
166: colorMenuItem.setIcon(new ColorIcon(i));
167: colorMenuItem.addActionListener(colorizeAL);
168: colorizeMenu.add(colorMenuItem);
169: }
170: colorizeMenu.addSeparator();
171: JMenuItem uncolorMenuItem = new JMenuItem("None");
172: colorizeMenu.add(uncolorMenuItem);
173: uncolorMenuItem.addActionListener(colorizeAL);
174: }
175: return popupMenu;
176: }
177:
178: public void setShowTableStriping(boolean showStriping) {
179: this .showTableStriping = showStriping;
180: repaint();
181: }
182:
183: public boolean getShowTableStriping() {
184: return showTableStriping;
185: }
186:
187: private void colorize(int index) {
188: repaint();
189: }
190:
191: private String getAddress(Message message) {
192: Contact from = message.getFrom();
193:
194: if (from != null) {
195: String address = from.getAddress();
196: if (address != null) {
197: return address.toLowerCase();
198: }
199: }
200: return null;
201: }
202:
203: private static class ColorIcon implements Icon {
204: private final int colorIndex;
205:
206: ColorIcon(int index) {
207: this .colorIndex = index;
208: }
209:
210: public void paintIcon(Component c, Graphics g, int x, int y) {
211: ((Graphics2D) g).setPaint(new GradientPaint(x, y,
212: COLORS[colorIndex * 3], x, y + getIconHeight(),
213: COLORS[colorIndex * 3 + 1]));
214: g.fillRect(x, y, getIconWidth(), getIconHeight());
215: g.setColor(Color.WHITE);
216: }
217:
218: public int getIconWidth() {
219: return 16;
220: }
221:
222: public int getIconHeight() {
223: return 16;
224: }
225: }
226: }
|