OSDN Git Service

fixed: GUIを一新した
[importpicture/importpicture.git] / src / main / java / osm / jp / gpx / matchtime / gui / ImagePreview.java
1 /*
2  * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  *   - Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  *
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.
14  *
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.
18  *
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.
30  */ 
31
32 package osm.jp.gpx.matchtime.gui;
33
34 import javax.swing.*;
35
36 import java.beans.*;
37 import java.awt.*;
38 import java.io.File;
39
40 /* ImagePreview.java by FileChooserDemo2.java. */
41 @SuppressWarnings("serial")
42 public class ImagePreview extends JComponent implements PropertyChangeListener {
43     ImageIcon thumbnail = null;
44     File file = null;
45     static int IMAGE_SIZE_X = 320;
46     static int IMAGE_SIZE_Y = 240;
47
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);
52     }
53
54     public void loadImage() {
55         if (file == null) {
56             thumbnail = null;
57             return;
58         }
59
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
69                 thumbnail = tmpIcon;
70             }
71         }
72     }
73
74     @Override
75     public void propertyChange(PropertyChangeEvent e) {
76         boolean update = false;
77         String prop = e.getPropertyName();
78
79         if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) {
80             file = null;
81             update = true;
82         }
83         else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
84             file = (File) e.getNewValue();
85             update = true;
86         }
87         if (update) {
88             thumbnail = null;
89             if (isShowing()) {
90                 loadImage();
91                 repaint();
92             }
93         }
94     }
95
96     @Override
97     protected void paintComponent(Graphics g) {
98         if (thumbnail == null) {
99             loadImage();
100         }
101         if (thumbnail != null) {
102             int x = getWidth()/2 - thumbnail.getIconWidth()/2;
103             int y = getHeight()/2 - thumbnail.getIconHeight()/2;
104
105             if (y < 0) {
106                 y = 0;
107             }
108
109             if (x < 5) {
110                 x = 5;
111             }
112             thumbnail.paintIcon(this, g, x, y);
113         }
114     }
115 }