]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/PhotoThumbnail.java
Version 9, February 2010
[GpsPrune.git] / tim / prune / gui / PhotoThumbnail.java
1 package tim.prune.gui;
2
3 import java.awt.Color;
4 import java.awt.Dimension;
5 import java.awt.Graphics;
6 import java.awt.Image;
7 import java.awt.image.BufferedImage;
8 import javax.swing.ImageIcon;
9 import javax.swing.JPanel;
10
11 import tim.prune.I18nManager;
12 import tim.prune.data.Photo;
13
14 /**
15  * GUI component for showing photo thumbnail
16  */
17 public class PhotoThumbnail extends JPanel implements Runnable
18 {
19         private Photo _photo = null;
20         private BufferedImage _thumbnail = null;
21         private boolean _loadingImage = false;
22         /** String to show before photo is loaded */
23         private static final String LOADING_STRING = I18nManager.getText("details.photo.loading") + " ...";
24
25
26         /**
27          * Constructor
28          */
29         public PhotoThumbnail()
30         {
31                 setOpaque(true);
32         }
33
34
35         /**
36          * Set the Photo
37          * @param inPhoto Photo object to show thumbnail for
38          */
39         public void setPhoto(Photo inPhoto)
40         {
41                 // Check whether the photo has changed
42                 if (_photo != inPhoto) {
43                         _photo = inPhoto;
44                         _thumbnail = null;
45                 }
46                 repaint();
47         }
48
49         /**
50          * Force a refresh / reload
51          */
52         public void refresh() {
53                 _thumbnail = null;
54         }
55
56         /**
57          * Override paint method
58          * @see javax.swing.JComponent#paint(java.awt.Graphics)
59          */
60         public void paint(Graphics inG)
61         {
62                 super.paint(inG);
63                 if (_photo != null)
64                 {
65                         // read thumbnail in separate thread
66                         if (_thumbnail == null && !_loadingImage)
67                         {
68                                 _loadingImage = true;
69                                 new Thread(this).start();
70                         }
71                         // if loading, display message
72                         if (_loadingImage)
73                         {
74                                 inG.setColor(Color.BLACK);
75                                 inG.drawString(LOADING_STRING, 10, 30);
76                         }
77                         else
78                         {
79                                 // Copy scaled, smoothed (and rotated) image into scaled
80                                 int usableWidth = getParent().getWidth()-10;
81                                 Image scaled = ImageUtils.rotateImage(_thumbnail, usableWidth, usableWidth, _photo.getRotationDegrees());
82                                 int scaleWidth = scaled.getWidth(null);
83                                 int scaleHeight = scaled.getHeight(null);
84                                 // Draw scaled / rotated image to component
85                                 int horizOffset = (getWidth() - scaleWidth) / 2;
86                                 int vertOffset = (getHeight() - scaleHeight) / 2;
87                                 inG.drawImage(scaled, horizOffset, vertOffset, scaleWidth, scaleHeight, null);
88                                 if (getHeight() < getWidth())
89                                 {
90                                         setPreferredSize(new Dimension(usableWidth, usableWidth));
91                                         invalidate();
92                                 }
93                         }
94                 }
95         }
96
97
98         /**
99          * Run method, for loading image in separate thread
100          * @see java.lang.Runnable#run()
101          */
102         public void run()
103         {
104                 // Use exif thumbnail?
105                 if (_photo.getExifThumbnail() != null) {
106                         Image image = new ImageIcon(_photo.getExifThumbnail()).getImage();
107                         _thumbnail = ImageUtils.createScaledImage(image, image.getWidth(null), image.getHeight(null));
108                         image = null;
109                 }
110                 else
111                 {
112                         // no exif thumbnail available, going to have to read whole thing
113                         int picWidth = _photo.getWidth();
114                         int picHeight = _photo.getHeight();
115                         if (picWidth > -1 && picHeight > -1)
116                         {
117                                 // Just set a "reasonable" thumbnail size for now
118                                 final int DEFAULT_THUMB_SIZE = 400;
119                                 // calculate maximum thumbnail size
120                                 Dimension thumbSize = ImageUtils.getThumbnailSize(picWidth, picHeight, DEFAULT_THUMB_SIZE, DEFAULT_THUMB_SIZE);
121                                 // Make icon to load image into
122                                 Image image = new ImageIcon(_photo.getFile().getAbsolutePath()).getImage();
123                                 // save scaled, smoothed thumbnail for reuse
124                                 _thumbnail = ImageUtils.createScaledImage(image, thumbSize.width, thumbSize.height);
125                                 image = null;
126                         }
127                 }
128                 _loadingImage = false;
129                 repaint();
130         }
131 }