01: /*
02: * Copyright (C) 2004 Giuseppe MANNA
03: *
04: * This file is part of FreeReportBuilder
05: *
06: * FreeReportBuilder is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU General Public License
08: * as published by the Free Software Foundation; either version 2
09: * of the License, or (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU General Public License for more details.
15: *
16: * You should have received a copy of the GNU General Public License
17: * along with this program; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19: *
20: */
21:
22: package it.frb;
23:
24: public class FilePreviewer extends javax.swing.JComponent implements
25: java.beans.PropertyChangeListener {
26: javax.swing.ImageIcon thumbnail = null;
27: java.io.File f = null;
28:
29: public FilePreviewer(javax.swing.JFileChooser fc) {
30: setPreferredSize(new java.awt.Dimension(100, 50));
31: fc.addPropertyChangeListener(this );
32: setBorder(new javax.swing.border.BevelBorder(
33: javax.swing.border.BevelBorder.LOWERED));
34: loadImage();
35: }
36:
37: public void loadImage() {
38: if (f != null) {
39: javax.swing.ImageIcon tmpIcon = new javax.swing.ImageIcon(f
40: .getPath());
41: if (tmpIcon.getIconWidth() > 90) {
42: thumbnail = new javax.swing.ImageIcon(tmpIcon
43: .getImage().getScaledInstance(90, -1,
44: java.awt.Image.SCALE_DEFAULT));
45: } else {
46: thumbnail = tmpIcon;
47: }
48: }
49: }
50:
51: public void propertyChange(java.beans.PropertyChangeEvent e) {
52: String prop = e.getPropertyName();
53: if (prop == javax.swing.JFileChooser.SELECTED_FILE_CHANGED_PROPERTY) {
54: f = (java.io.File) e.getNewValue();
55: if (isShowing()) {
56: loadImage();
57: repaint();
58: }
59: }
60: }
61:
62: public void paint(java.awt.Graphics g) {
63: super .paint(g);
64: if (thumbnail == null) {
65: loadImage();
66: }
67:
68: if (thumbnail != null) {
69: int x = getWidth() / 2 - thumbnail.getIconWidth() / 2;
70: int y = getHeight() / 2 - thumbnail.getIconHeight() / 2;
71:
72: if (y < 0) {
73: y = 0;
74: }
75:
76: if (x < 5) {
77: x = 5;
78: }
79:
80: thumbnail.paintIcon(this, g, x, y);
81: }
82: }
83: }
|