01: /*
02: * ImagePanel.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.gui.dialogs;
13:
14: import java.awt.BorderLayout;
15: import java.awt.Image;
16: import java.io.BufferedInputStream;
17: import java.io.ByteArrayInputStream;
18: import java.io.File;
19: import java.io.FileInputStream;
20: import java.io.IOException;
21: import java.io.InputStream;
22: import java.sql.Blob;
23: import java.sql.SQLException;
24: import javax.imageio.ImageIO;
25: import javax.swing.ImageIcon;
26: import javax.swing.JLabel;
27: import javax.swing.JPanel;
28: import javax.swing.SwingConstants;
29: import javax.swing.border.EtchedBorder;
30: import workbench.log.LogMgr;
31: import workbench.resource.ResourceMgr;
32: import workbench.util.FileUtil;
33:
34: /**
35: * @author support@sql-workbench.net
36: */
37: public class ImagePanel extends JPanel {
38: private Image displayImage;
39: private JLabel label = new JLabel();
40: private int imageWidth;
41: private int imageHeight;
42:
43: public ImagePanel() {
44: this .setLayout(new BorderLayout());
45: this .add(label, BorderLayout.CENTER);
46: label.setHorizontalAlignment(SwingConstants.CENTER);
47: label.setBorder(new EtchedBorder());
48: }
49:
50: public void setImage(File imageData) throws IOException,
51: SQLException {
52: InputStream in = new BufferedInputStream(new FileInputStream(
53: imageData));
54: this .readImageData(in);
55: }
56:
57: public void setImage(Blob imageData) throws IOException,
58: SQLException {
59: byte[] data = imageData.getBytes(1, (int) imageData.length());
60: setImage(data);
61: }
62:
63: public void setImage(byte[] imageData) throws IOException {
64: if (imageData == null)
65: return;
66: if (imageData.length < 4)
67: return;
68: InputStream in = new ByteArrayInputStream(imageData);
69: this .readImageData(in);
70: }
71:
72: public boolean hasImage() {
73: return this .displayImage != null;
74: }
75:
76: private void readImageData(InputStream in) throws IOException {
77: if (displayImage != null) {
78: displayImage.flush();
79: }
80:
81: try {
82: displayImage = ImageIO.read(in);
83: } catch (Exception e) {
84: displayImage = null;
85: LogMgr.logError("ImagePanel.readImageData",
86: "Error reading image", e);
87: } finally {
88: FileUtil.closeQuitely(in);
89: }
90:
91: if (displayImage == null) {
92: label.setText(ResourceMgr.getString("ErrImgNotSupp"));
93: } else {
94: label.setIcon(new ImageIcon(this.displayImage));
95: }
96: }
97:
98: }
|