]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/Photo.java
Version 7, February 2009
[GpsPrune.git] / tim / prune / data / Photo.java
1 package tim.prune.data;
2
3 import java.awt.Dimension;
4 import java.io.File;
5
6 import javax.swing.ImageIcon;
7
8 /**
9  * Class to represent a photo and link to DataPoint
10  */
11 public class Photo
12 {
13         /** File where photo is stored */
14         private File _file = null;
15         /** Timestamp, if any */
16         private Timestamp _timestamp = null;
17         /** Associated DataPoint if correlated */
18         private DataPoint _dataPoint = null;
19         /** Size of original image */
20         private Dimension _size = null;
21         /** Status of photo when loaded */
22         private Status _originalStatus = Status.NOT_CONNECTED;
23         /** Current photo status */
24         private Status _currentStatus = Status.NOT_CONNECTED;
25         // TODO: Need to store caption for image?
26         // thumbnail for image (from exif)
27         private byte[] _exifThumbnail = null;
28
29         /** Photo status */
30         public enum Status {
31                 /** Photo is not connected to any point */
32                 NOT_CONNECTED,
33                 /** Photo has been connected to a point since it was loaded */
34                 TAGGED,
35                 /** Photo is connected to a point */
36                 CONNECTED
37         };
38
39
40         /**
41          * Constructor
42          * @param inFile File object for photo
43          */
44         public Photo(File inFile)
45         {
46                 _file = inFile;
47         }
48
49
50         /**
51          * @return File object where photo resides
52          */
53         public File getFile()
54         {
55                 return _file;
56         }
57
58
59         /**
60          * Set the data point associated with the photo
61          * @param inPoint DataPoint with coordinates etc
62          */
63         public void setDataPoint(DataPoint inPoint)
64         {
65                 _dataPoint = inPoint;
66                 // set status according to point
67                 if (inPoint == null)
68                 {
69                         setCurrentStatus(Status.NOT_CONNECTED);
70                 }
71                 else
72                 {
73                         setCurrentStatus(Status.CONNECTED);
74                 }
75         }
76
77         /**
78          * @return the DataPoint object
79          */
80         public DataPoint getDataPoint()
81         {
82                 return _dataPoint;
83         }
84
85         /**
86          * @param inTimestamp Timestamp of photo
87          */
88         public void setTimestamp(Timestamp inTimestamp)
89         {
90                 _timestamp = inTimestamp;
91         }
92
93         /**
94          * @return timestamp of photo
95          */
96         public Timestamp getTimestamp()
97         {
98                 return _timestamp;
99         }
100
101         /**
102          * Calculate the size of the image (slow)
103          */
104         private void calculateSize()
105         {
106                 ImageIcon icon = new ImageIcon(_file.getAbsolutePath());
107                 int width = icon.getIconWidth();
108                 int height = icon.getIconHeight();
109                 if (width > 0 && height > 0)
110                 {
111                         _size = new Dimension(width, height);
112                 }
113         }
114
115         /**
116          * @return size of image as Dimension object
117          */
118         public Dimension getSize()
119         {
120                 if (_size == null)
121                 {
122                         calculateSize();
123                 }
124                 return _size;
125         }
126
127         /**
128          * @return width of the image, if known
129          */
130         public int getWidth()
131         {
132                 if (_size == null)
133                 {
134                         calculateSize();
135                         if (_size == null) {return -1;}
136                 }
137                 return _size.width;
138         }
139
140         /**
141          * @return height of the image, if known
142          */
143         public int getHeight()
144         {
145                 if (_size == null)
146                 {
147                         calculateSize();
148                         if (_size == null) {return -1;}
149                 }
150                 return _size.height;
151         }
152
153         /**
154          * @param inStatus status of photo when loaded
155          */
156         public void setOriginalStatus(Status inStatus)
157         {
158                 _originalStatus = inStatus;
159                 _currentStatus = inStatus;
160         }
161
162         /**
163          * @return status of photo when it was loaded
164          */
165         public Status getOriginalStatus()
166         {
167                 return _originalStatus;
168         }
169
170         /**
171          * @return current status of photo
172          */
173         public Status getCurrentStatus()
174         {
175                 return _currentStatus;
176         }
177         /**
178          * @param inStatus current status of photo
179          */
180         public void setCurrentStatus(Status inStatus)
181         {
182                 _currentStatus = inStatus;
183         }
184
185         /**
186          * @return byte array of thumbnail data
187          */
188         public byte[] getExifThumbnail()
189         {
190                 return _exifThumbnail;
191         }
192
193         /**
194          * @param inBytes byte array from exif
195          */
196         public void setExifThumbnail(byte[] inBytes)
197         {
198                 _exifThumbnail = inBytes;
199         }
200
201         /**
202          * Delete the cached data when the Photo is no longer needed
203          */
204         public void resetCachedData()
205         {
206                 _size = null;
207                 // remove thumbnail too
208         }
209
210         /**
211          * Check if a Photo object refers to the same File as another
212          * @param inOther other Photo object
213          * @return true if the Files are the same
214          */
215         public boolean equals(Photo inOther)
216         {
217                 return (inOther != null && inOther.getFile() != null && getFile() != null
218                         && inOther.getFile().equals(getFile()));
219         }
220 }