01: /*
02: * Beryl - A web platform based on XML, XSLT and Java
03: * This file is part of the Beryl XML GUI
04: *
05: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU Lesser General Public
09: * License as published by the Free Software Foundation; either
10: * version 2.1 of the License, or (at your option) any later version.
11:
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
20: */
21:
22: package org.beryl.gui.table;
23:
24: import java.awt.BorderLayout;
25: import java.awt.Color;
26:
27: import javax.swing.BorderFactory;
28: import javax.swing.JLabel;
29: import javax.swing.border.Border;
30:
31: import org.beryl.gui.GUIException;
32: import org.beryl.gui.Widget;
33: import org.beryl.gui.model.TableRow;
34: import org.beryl.gui.widgets.Label;
35: import org.beryl.gui.widgets.Panel;
36: import org.beryl.gui.widgets.Table;
37:
38: public class ColorRenderer implements TableRenderer {
39: private Border selectedBorder = null;
40: private Border unselectedBorder = null;
41:
42: private static String toHex(int num) {
43: String val = Integer.toString(num, 16);
44: if (val.length() == 1)
45: val = "0" + val;
46: return val;
47: }
48:
49: public static String toString(Color color) {
50: return "#" + toHex(color.getRed()) + toHex(color.getGreen())
51: + toHex(color.getBlue());
52: }
53:
54: public Widget getRenderer(Table table, Object value,
55: boolean isSelected, boolean hasFocus, TableRow row,
56: String key) throws GUIException {
57: Color color = (Color) value;
58: Panel panel = new Panel(null, null);
59: Label label = new Label(panel, null);
60:
61: label.setProperty("opaque", Boolean.TRUE);
62: label.setProperty("background", color);
63:
64: int lightness = (color.getRed() + color.getBlue() + color
65: .getGreen()) / 3;
66:
67: if (lightness > 127)
68: label.setProperty("foreground", Color.black);
69: else
70: label.setProperty("foreground", Color.white);
71:
72: label.setProperty("text", toString(color));
73:
74: label.setProperty("horizontalAlignment", new Integer(
75: JLabel.CENTER));
76: label.setProperty("border", BorderFactory.createEmptyBorder(3,
77: 3, 3, 3));
78: panel.setProperty("layout", new BorderLayout());
79: panel.addChild(label, BorderLayout.CENTER);
80: return panel;
81: }
82: }
|