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