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