2 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * - Neither the name of Oracle or the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 package osm.jp.gpx.matchtime.gui;
40 /* ImagePreview.java by FileChooserDemo2.java. */
41 @SuppressWarnings("serial")
42 public class ImagePreview extends JComponent implements PropertyChangeListener {
43 ImageIcon thumbnail = null;
45 static int IMAGE_SIZE_X = 320;
46 static int IMAGE_SIZE_Y = 240;
48 @SuppressWarnings({"LeakingThisInConstructor", "OverridableMethodCallInConstructor"})
49 public ImagePreview(JFileChooser fc) {
50 setPreferredSize(new Dimension(IMAGE_SIZE_X + 10, IMAGE_SIZE_Y + 10));
51 fc.addPropertyChangeListener(this);
54 public void loadImage() {
60 //Don't use createImageIcon (which is a wrapper for getResource)
61 //because the image we're trying to load is probably not one
62 //of this program's own resources.
63 ImageIcon tmpIcon = new ImageIcon(file.getPath());
64 if (tmpIcon != null) {
65 if (tmpIcon.getIconWidth() > IMAGE_SIZE_X) {
66 thumbnail = new ImageIcon(tmpIcon.getImage().
67 getScaledInstance(IMAGE_SIZE_X, -1, Image.SCALE_DEFAULT));
68 } else { //no need to miniaturize
75 public void propertyChange(PropertyChangeEvent e) {
76 boolean update = false;
77 String prop = e.getPropertyName();
79 if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) {
83 else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
84 file = (File) e.getNewValue();
97 protected void paintComponent(Graphics g) {
98 if (thumbnail == null) {
101 if (thumbnail != null) {
102 int x = getWidth()/2 - thumbnail.getIconWidth()/2;
103 int y = getHeight()/2 - thumbnail.getIconHeight()/2;
112 thumbnail.paintIcon(this, g, x, y);