]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/PhotoThumbnail.java
Version 4, January 2008
[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 int _lastWidth = -1;
22         private int _lastHeight = -1;
23         private boolean _loadingImage = false;
24         private static String _loadingString = null;
25
26
27         /**
28          * Constructor
29          */
30         public PhotoThumbnail()
31         {
32                 // TODO: Make size of thumbnail dynamic, as big as it can be
33                 setOpaque(true);
34                 _loadingString = I18nManager.getText("details.photo.loading") + " ...";
35         }
36
37
38         /**
39          * Set the Photo
40          * @param inPhoto Photo object to show thumbnail for
41          */
42         public void setPhoto(Photo inPhoto)
43         {
44                 // Check whether the photo has changed
45                 if (_photo == inPhoto) {return;}
46                 _photo = inPhoto;
47                 _thumbnail = null;
48         }
49
50
51         /**
52          * Override paint method
53          * @see javax.swing.JComponent#paint(java.awt.Graphics)
54          */
55         public void paint(Graphics inG)
56         {
57                 super.paint(inG);
58                 if (_photo != null)
59                 {
60                         // recalculate thumbnail if photo has changed
61                         if (_thumbnail == null || getWidth() != _lastWidth || getHeight() != _lastHeight)
62                         {
63                                 // initiate load if not already started
64                                 if (!_loadingImage)
65                                 {
66                                         _loadingImage = true;
67                                         new Thread(this).start();
68                                 }
69                         }
70                         // Set width and height
71                         _lastWidth = getWidth();
72                         _lastHeight = getHeight();
73                         // if loading, display image
74                         if (_loadingImage)
75                         {
76                                 inG.setColor(Color.BLACK);
77                                 inG.drawString(_loadingString, 10, 30);
78                         }
79                         else
80                         {
81                                 // Copy scaled, smoothed image onto the screen
82                                 inG.drawImage(_thumbnail, 0, 0, _thumbnail.getWidth(), _thumbnail.getHeight(), null);
83                         }
84                 }
85         }
86
87
88         /**
89          * Run method, for loading image in separate thread
90          * @see java.lang.Runnable#run()
91          */
92         public void run()
93         {
94                 // Use exif thumbnail?
95                 if (_photo.getExifThumbnail() != null) {
96                         Image image = new ImageIcon(_photo.getExifThumbnail()).getImage();
97                         _thumbnail = ImageUtils.createScaledImage(image, image.getWidth(null), image.getHeight(null));
98                         image = null;
99                 }
100                 else
101                 {
102                         // no exif thumbnail available, going to have to read whole thing
103                         int picWidth = _photo.getWidth();
104                         int picHeight = _photo.getHeight();
105                         if (picWidth > -1 && picHeight > -1)
106                         {
107                                 int displayWidth = Math.min(getWidth(), getParent().getWidth());
108                                 // System.out.println("width = " + getWidth() + ", " + getParent().getWidth() + " = " + displayWidth);
109                                 int displayHeight = Math.min(getHeight(), getParent().getHeight());
110                                 // System.out.println("height = " + getHeight() + ", " + getParent().getHeight() + " = " + displayHeight);
111         
112                                 // calculate maximum thumbnail size
113                                 Dimension thumbSize = ImageUtils.getThumbnailSize(picWidth, picHeight, displayWidth, displayHeight);
114                                 // Work out if need to remake image
115                                 boolean needToRemake = (_thumbnail == null)
116                                  || _thumbnail.getWidth() != thumbSize.width || _thumbnail.getHeight() != thumbSize.height;
117                                 if (thumbSize.width > 0 && thumbSize.height > 0 && needToRemake)
118                                 {
119                                         // Make icon to load image into
120                                         Image image = new ImageIcon(_photo.getFile().getAbsolutePath()).getImage();
121                                         // save scaled, smoothed thumbnail for reuse
122                                         _thumbnail = ImageUtils.createScaledImage(image, thumbSize.width, thumbSize.height);
123                                         image = null;
124                                         // TODO: Calculate and set size of thumbnail here
125                                         // setPreferredSize(new Dimension(200, 200));
126                                 }
127                         }
128                 }
129                 _loadingImage = false;
130                 repaint();
131         }
132 }