]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/MediaObject.java
Version 14, October 2012
[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 the full path to the media, either filename or url
110          */
111         public String getFullPath()
112         {
113                 if (_file != null) return _file.getAbsolutePath();
114                 return getUrl();
115         }
116
117         /**
118          * @return true if details are valid (might not have timestamp)
119          */
120         public boolean isValid()
121         {
122                 return ((_file != null && _file.exists() && _file.canRead())
123                         || (_data != null && _data.length > 0));
124         }
125
126         /**
127          * @return true if file has timestamp
128          */
129         public boolean hasTimestamp() {
130                  return _timestamp != null && _timestamp.isValid();
131         }
132
133         /**
134          * Check if this object refers to the same object as another
135          * @param inOther other MediaObject
136          * @return true if the objects are the same
137          */
138         public boolean equals(MediaObject inOther)
139         {
140                 if (_file != null)
141                 {
142                         // compare file objects
143                         return (inOther != null && inOther.getFile() != null && getFile() != null
144                                 && inOther.getFile().equals(getFile()));
145                 }
146                 // compare data arrays
147                 return (inOther != null && _name != null && inOther._name != null && _name.equals(inOther._name)
148                         && _data != null && inOther._data != null && _data.length == inOther._data.length);
149         }
150
151         /**
152          * Set the data point associated with the photo
153          * @param inPoint DataPoint with coordinates etc
154          */
155         public void setDataPoint(DataPoint inPoint)
156         {
157                 _dataPoint = inPoint;
158                 // set status according to point
159                 if (inPoint == null) {
160                         setCurrentStatus(Status.NOT_CONNECTED);
161                 }
162                 else {
163                         setCurrentStatus(Status.CONNECTED);
164                 }
165         }
166
167         /**
168          * @return the DataPoint object
169          */
170         public DataPoint getDataPoint() {
171                 return _dataPoint;
172         }
173
174         /**
175          * @param inStatus status of file when loaded
176          */
177         public void setOriginalStatus(Status inStatus)
178         {
179                 _originalStatus = inStatus;
180                 _currentStatus = inStatus;
181         }
182
183         /**
184          * @return status of file when it was loaded
185          */
186         public Status getOriginalStatus()
187         {
188                 return _originalStatus;
189         }
190
191         /**
192          * @return current status
193          */
194         public Status getCurrentStatus()
195         {
196                 return _currentStatus;
197         }
198         /**
199          * @param inStatus current status
200          */
201         public void setCurrentStatus(Status inStatus)
202         {
203                 _currentStatus = inStatus;
204         }
205
206         /**
207          * @return true if this object is connected to a point
208          */
209         public boolean isConnected()
210         {
211                 return _currentStatus != Status.NOT_CONNECTED;
212         }
213
214         /**
215          * Reset any cached data (eg thumbnail)
216          */
217         public void resetCachedData() {}
218 }