/*******************************************************************************
* Copyright (c) 2008 Jerome Negre.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl-3.0-standalone.html
*
* Contributors:
* Jerome Negre - initial API and implementation
******************************************************************************/
package org.jnegre.redpill.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.List;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import org.jnegre.redpill.HttpResponse;
public class ImageResponseViewer implements ResponseViewer {
JLabel iconLabel;
JLabel sizeLabel;
JCheckBox shrinkToFitBox;
public void displayResponses(List<HttpResponse> responses) {
for(HttpResponse r : responses) {
if(r != null) {
byte[] data = r.getEntityBody();
if(data != null && data.length != 0) {
updateDisplay(data);
return;
}
}
}
updateDisplay(null);
}
public Component getComponent() {
JPanel statusPanel = new JPanel(new BorderLayout());
//shrinkToFitBox
shrinkToFitBox = new JCheckBox("Shrink to fit");
shrinkToFitBox.setEnabled(false);//FIXME implement
statusPanel.add(shrinkToFitBox, BorderLayout.WEST);
//size
sizeLabel = new JLabel();
statusPanel.add(sizeLabel, BorderLayout.EAST);
//icon
iconLabel = new JLabel();
iconLabel.setHorizontalAlignment(SwingConstants.CENTER);
JPanel panel = new JPanel(new BorderLayout());
panel.add(statusPanel, BorderLayout.NORTH);
panel.add(new JScrollPane(iconLabel), BorderLayout.CENTER);
return panel;
}
public String getTitle() {
return "Image";
}
private void updateDisplay(byte[] data) {
//FIXME use javax.imageio, need to support BMP and animated gif
if(data != null) {
Icon icon = getIcon(data);
int w = icon.getIconWidth();
int h = icon.getIconHeight();
if(h>=0 && w>=0) {
iconLabel.setIcon(icon);
sizeLabel.setText(w+"x"+h+" pixels");
} else {
iconLabel.setIcon(null);
sizeLabel.setText(null);
}
} else {
iconLabel.setIcon(null);
sizeLabel.setText(null);
}
}
private Icon getIcon(byte[] data) {
final ImageIcon image = new ImageIcon(data);
return new Icon(){
public int getIconHeight() {
return image.getIconHeight();
}
public int getIconWidth() {
return image.getIconWidth();
}
public void paintIcon(Component c, Graphics g, int x, int y) {
// render chessboard
int w = getIconWidth();
int h = getIconHeight();
int maxX = x + w;
int maxY = y + h;
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.GRAY);
g2d.fillRect(x, y, w, h);
g2d.setColor(Color.LIGHT_GRAY);
for(int cx=x;cx<maxX;cx+=16) {
for(int cy=y;cy<maxY;cy+=16) {
g2d.fillRect(cx, cy, Math.min(8, maxX-cx), Math.min(8, maxY-cy));
g2d.fillRect(cx+8, cy+8, Math.min(8, maxX-cx-8), Math.min(8, maxY-cy-8));
}
}
g2d.dispose();
// render image
image.paintIcon(c, g, x, y);
}
};
}
}