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