]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/MediaObject.java
e028f4ab4ddc7f1050f0c9e2e3792da90c3e6c41
[GpsPrune.git] / tim / prune / data / MediaObject.java
1 package tim.prune.data;
2
3 import java.io.File;
4
5 /**
6  * Class to represent a general media object for correlation.
7  * Subclasses are currently Photo and AudioClip
8  */
9 public abstract class MediaObject
10 {
11         /** File where media is stored (if any) */
12         protected File _file = null;
13         /** Name of file */
14         protected String _name = null;
15         /** Cached data if downloaded */
16         protected byte[] _data = null;
17         /** URL if media came from net */
18         protected String _url = null;
19         /** Timestamp, if any */
20         protected Timestamp _timestamp = null;
21         /** Associated DataPoint if correlated */
22         protected DataPoint _dataPoint = null;
23         /** Status when loaded */
24         private Status _originalStatus = Status.NOT_CONNECTED;
25         /** Current status */
26         private Status _currentStatus = Status.NOT_CONNECTED;
27
28         /** Connection status */
29         public enum Status
30         {
31                 /** Media is not connected to any point */
32                 NOT_CONNECTED,
33                 /** Media has been connected to a point since it was loaded */
34                 TAGGED,
35                 /** Media is connected to a point */
36                 CONNECTED
37         };
38
39
40         /**
41          * Constructor
42          * @param inFile file object
43          * @param inStamp timestamp object
44          */
45         public MediaObject(File inFile, Timestamp inStamp)
46         {
47                 _file = inFile;
48                 _name = inFile.getName();
49                 _data = null;
50                 _timestamp = inStamp;
51         }
52
53         /**
54          * Constructor for byte arrays
55          * @param inData byte array containing data
56          * @param inName name of object
57          * @param inUrl source url of object or null
58          */
59         public MediaObject(byte[] inData, String inName, String inUrl)
60         {
61                 _file = null;
62                 _data = inData;
63                 _name = inName;
64                 _url = inUrl;
65                 _timestamp = null;
66         }
67
68         /**
69          * @return the file object
70          */
71         public File getFile() {
72                 return _file;
73         }
74
75         /** @return media name */
76         public String getName() {
77                 return _name;
78         }
79
80         /**
81          * @return the timestamp object
82          */
83         public Timestamp getTimestamp() {
84                 return _timestamp;
85         }
86
87         /**
88          * @param inTimestamp Timestamp object
89          */
90         public void setTimestamp(Timestamp inTimestamp) {
91                 _timestamp = inTimestamp;
92         }
93
94         /**
95          * @return byte data of media
96          */
97         public byte[] getByteData() {
98                 return _data;
99         }
100
101         /**
102          * @return source Url (or null)
103          */
104         public String getUrl() {
105                 return _url;
106         }
107
108         /**
109          * @return true if details are valid (might not have timestamp)
110          */
111         public boolean isValid()
112         {
113                 return ((_file != null && _file.exists() && _file.canRead())
114                         || (_data != null && _data.length > 0));
115         }
116
117         /**
118          * @return true if file has timestamp
119          */
120         public boolean hasTimestamp() {
121                  return _timestamp != null && _timestamp.isValid();
122         }
123
124         /**
125          * Check if this object refers to the same object as another
126          * @param inOther other MediaObject
127          * @return true if the objects are the same
128          */
129         public boolean equals(MediaObject inOther)
130         {
131                 if (_file != null)
132                 {
133                         // compare file objects
134                         return (inOther != null && inOther.getFile() != null && getFile() != null
135                                 && inOther.getFile().equals(getFile()));
136                 }
137                 // compare data arrays
138                 return (inOther != null && _name != null && inOther._name != null && _name.equals(inOther._name)
139                         && _data != null && inOther._data != null && _data.length == inOther._data.length);
140         }
141
142         /**
143          * Set the data point associated with the photo
144          * @param inPoint DataPoint with coordinates etc
145          */
146         public void setDataPoint(DataPoint inPoint)
147         {
148                 _dataPoint = inPoint;
149                 // set status according to point
150                 if (inPoint == null) {
151                         setCurrentStatus(Status.NOT_CONNECTED);
152                 }
153                 else {
154                         setCurrentStatus(Status.CONNECTED);
155                 }
156         }
157
158         /**
159          * @return the DataPoint object
160          */
161         public DataPoint getDataPoint() {
162                 return _dataPoint;
163         }
164
165         /**
166          * @param inStatus status of file when loaded
167          */
168         public void setOriginalStatus(Status inStatus)
169         {
170                 _originalStatus = inStatus;
171                 _currentStatus = inStatus;
172         }
173
174         /**
175          * @return status of file when it was loaded
176          */
177         public Status getOriginalStatus()
178         {
179                 return _originalStatus;
180         }
181
182         /**
183          * @return current status
184          */
185         public Status getCurrentStatus()
186         {
187                 return _currentStatus;
188         }
189         /**
190          * @param inStatus current status
191          */
192         public void setCurrentStatus(Status inStatus)
193         {
194                 _currentStatus = inStatus;
195         }
196
197         /**
198          * @return true if this object is connected to a point
199          */
200         public boolean isConnected()
201         {
202                 return _currentStatus != Status.NOT_CONNECTED;
203         }
204
205         /**
206          * Reset any cached data (eg thumbnail)
207          */
208         public void resetCachedData() {}
209 }