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.Color;
25: import java.awt.Graphics2D;
26: import java.awt.Image;
27: import java.awt.geom.AffineTransform;
28: import java.awt.image.BufferedImage;
29:
30: import javax.swing.ImageIcon;
31: import javax.swing.JLabel;
32:
33: import org.beryl.gui.GUIException;
34: import org.beryl.gui.Widget;
35: import org.beryl.gui.model.TableRow;
36: import org.beryl.gui.widgets.Label;
37: import org.beryl.gui.widgets.Table;
38:
39: public class ImageRenderer implements TableRenderer {
40: private int maxImageSize;
41:
42: public ImageRenderer() {
43: this (10);
44: }
45:
46: public ImageRenderer(int maxImageSize) {
47: this .maxImageSize = maxImageSize;
48: }
49:
50: public Widget getRenderer(Table table, Object value,
51: boolean isSelected, boolean hasFocus, TableRow row,
52: String key) throws GUIException {
53:
54: Label label = new Label(null, null);
55: ImageIcon icon = (ImageIcon) value;
56: Image inImage = icon.getImage();
57:
58: /* Resize the image */
59: double scale = (double) maxImageSize
60: / (double) inImage.getHeight(null);
61:
62: if (inImage.getWidth(null) > inImage.getHeight(null)) {
63: scale = (double) maxImageSize
64: / (double) inImage.getWidth(null);
65: }
66: int scaledW = (int) (scale * inImage.getWidth(null));
67: int scaledH = (int) (scale * inImage.getHeight(null));
68: BufferedImage outImage = new BufferedImage(scaledW, scaledH,
69: BufferedImage.TYPE_INT_RGB);
70: AffineTransform tx = new AffineTransform();
71:
72: if (scale < 1.0d) {
73: tx.scale(scale, scale);
74: }
75:
76: Color bgColor = null;
77: Graphics2D g2d = outImage.createGraphics();
78: g2d.setColor(bgColor);
79: g2d.fillRect(0, 0, scaledW, scaledH);
80: g2d.drawImage(inImage, tx, null);
81: g2d.dispose();
82:
83: label.setIcon(new ImageIcon(outImage));
84: label.setProperty("horizontalAlignment", new Integer(
85: JLabel.CENTER));
86: label.setProperty("opaque", Boolean.TRUE);
87: return label;
88: }
89: }
|