2 GNU Lesser General Public License
4 ImageFileChooserPreview
5 Copyright (C) 2000-2002 Frits Jalvingh & Howard Kistler
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 This library 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.
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 package com
.hexidec
.ekit
.component
;
24 import java
.awt
.Dimension
;
25 import java
.awt
.Graphics
;
26 import java
.awt
.Image
;
28 import javax
.swing
.ImageIcon
;
29 import javax
.swing
.JComponent
;
30 import javax
.swing
.JFileChooser
;
31 import java
.beans
.PropertyChangeEvent
;
32 import java
.beans
.PropertyChangeListener
;
34 /** Class provides a preview window for the selected image file
36 class ImageFileChooserPreview
extends JComponent
implements PropertyChangeListener
38 private static final int previewWidth
= 100;
39 private static final int previewHeight
= 100;
41 private ImageIcon imageThumb
= null
;
42 private File imageFile
= null
;
44 /** This class requires a file chooser to register with so this class will
45 * be notified when a new file is selected in the browser.
46 * @param JFileChooser that this preview window is used in.
48 public ImageFileChooserPreview(JFileChooser parent
)
50 setPreferredSize(new Dimension(previewWidth
, previewHeight
));
51 parent
.addPropertyChangeListener(this);
54 /** Loads a new image into the preview window, and scales it if necessary.
56 public void loadImage()
63 imageThumb
= new ImageIcon(imageFile
.getPath());
65 // Check if thumb requires scaling
66 if(imageThumb
.getIconHeight() < previewHeight
&& imageThumb
.getIconWidth() < previewWidth
)
71 int h
= previewHeight
;
72 if(imageThumb
.getIconHeight() > imageThumb
.getIconWidth())
80 imageThumb
= new ImageIcon(imageThumb
.getImage().getScaledInstance(w
, h
, Image
.SCALE_DEFAULT
));
83 /** Callback (event handler) to indicate that a property of the
84 * JFileChooser has changed. If the selected file has changed cause a new
87 public void propertyChange(PropertyChangeEvent e
)
89 if(e
.getPropertyName().equals(JFileChooser
.SELECTED_FILE_CHANGED_PROPERTY
))
91 imageFile
= (File
)e
.getNewValue();
100 /** Paints the icon of the current image, if one's present..
101 * @param Graphics object to use when painting the component.
103 public void paintComponent(Graphics g
)
105 if(imageThumb
== null
)
109 if(imageThumb
== null
)
113 int x
= (getWidth() - imageThumb
.getIconWidth()) / 2;
114 int y
= (getHeight() - imageThumb
.getIconHeight()) / 2;
123 imageThumb
.paintIcon(this, g
, x
, y
);