]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/TrackInfo.java
b987ea9449d6dff66fd52f2af60031752f415413
[GpsPrune.git] / tim / prune / data / TrackInfo.java
1 package tim.prune.data;
2
3 import java.util.List;
4
5 import tim.prune.UpdateMessageBroker;
6
7 /**
8  * Class to hold all track information, including data
9  * and the selection information
10  */
11 public class TrackInfo
12 {
13         private UpdateMessageBroker _broker = null;
14         private Track _track = null;
15         private Selection _selection = null;
16         private FileInfo _fileInfo = null;
17         private PhotoList _photoList = null;
18
19
20         /**
21          * Constructor
22          * @param inTrack Track object
23          * @param inBroker broker object
24          */
25         public TrackInfo(Track inTrack, UpdateMessageBroker inBroker)
26         {
27                 _broker = inBroker;
28                 _track = inTrack;
29                 _selection = new Selection(_track, inBroker);
30                 _fileInfo = new FileInfo();
31                 _photoList = new PhotoList();
32         }
33
34
35         /**
36          * @return the Track object
37          */
38         public Track getTrack()
39         {
40                 return _track;
41         }
42
43
44         /**
45          * @return the Selection object
46          */
47         public Selection getSelection()
48         {
49                 return _selection;
50         }
51
52
53         /**
54          * @return the FileInfo object
55          */
56         public FileInfo getFileInfo()
57         {
58                 return _fileInfo;
59         }
60
61         /**
62          * @return the PhotoList object
63          */
64         public PhotoList getPhotoList()
65         {
66                 return _photoList;
67         }
68
69         /**
70          * Get the currently selected point, if any
71          * @return DataPoint if single point selected, otherwise null
72          */
73         public DataPoint getCurrentPoint()
74         {
75                 return _track.getPoint(_selection.getCurrentPointIndex());
76         }
77
78         /**
79          * Get the currently selected photo, if any
80          * @return Photo if selected, otherwise null
81          */
82         public Photo getCurrentPhoto()
83         {
84                 return _photoList.getPhoto(_selection.getCurrentPhotoIndex());
85         }
86
87
88         /**
89          * Load the specified data into the Track
90          * @param inFieldArray array of Field objects describing fields
91          * @param inPointArray 2d object array containing data
92          * @param inAltFormat altitude format
93          */
94         public void loadTrack(Field[] inFieldArray, Object[][] inPointArray, int inAltFormat)
95         {
96                 _track.cropTo(0);
97                 _track.load(inFieldArray, inPointArray, inAltFormat);
98                 _selection.clearAll();
99         }
100
101
102         /**
103          * Add a List of Photos
104          * @param inList List containing Photo objects
105          * @return array containing number of photos and number of points added
106          */
107         public int[] addPhotos(List inList)
108         {
109                 // TODO: Should photos be sorted at load-time, either by filename or date?
110                 // Firstly count number of points and photos to add
111                 int numPhotosToAdd = 0;
112                 int numPointsToAdd = 0;
113                 if (inList != null && !inList.isEmpty())
114                 {
115                         for (int i=0; i<inList.size(); i++)
116                         {
117                                 try
118                                 {
119                                         Photo photo = (Photo) inList.get(i);
120                                         if (photo != null && !_photoList.contains(photo))
121                                         {
122                                                 numPhotosToAdd++;
123                                                 if (photo.getDataPoint() != null)
124                                                 {
125                                                         numPointsToAdd++;
126                                                 }
127                                         }
128                                 }
129                                 catch (ClassCastException ce) {}
130                         }
131                 }
132                 // If there are any photos to add, add them
133                 if (numPhotosToAdd > 0)
134                 {
135                         DataPoint[] dataPoints = new DataPoint[numPointsToAdd];
136                         int pointNum = 0;
137                         boolean hasAltitude = false;
138                         // Add each Photo in turn
139                         for (int i=0; i<inList.size(); i++)
140                         {
141                                 try
142                                 {
143                                         Photo photo = (Photo) inList.get(i);
144                                         if (photo != null && !_photoList.contains(photo))
145                                         {
146                                                 // Add photo
147                                                 _photoList.addPhoto(photo);
148                                                 // Add point if there is one
149                                                 if (photo.getDataPoint() != null)
150                                                 {
151                                                         dataPoints[pointNum] = photo.getDataPoint();
152                                                         // Check if any points have altitudes
153                                                         hasAltitude |= (photo.getDataPoint().getAltitude() != null);
154                                                         pointNum++;
155                                                 }
156                                         }
157                                 }
158                                 catch (ClassCastException ce) {}
159                         }
160                         if (numPointsToAdd > 0)
161                         {
162                                 // add points to track
163                                 _track.appendPoints(dataPoints);
164                                 // modify track field list
165                                 _track.getFieldList().extendList(Field.LATITUDE);
166                                 _track.getFieldList().extendList(Field.LONGITUDE);
167                                 if (hasAltitude) {_track.getFieldList().extendList(Field.ALTITUDE);}
168                         }
169                 }
170                 int[] result = {numPhotosToAdd, numPointsToAdd};
171                 return result;
172         }
173
174
175         /**
176          * Delete the currently selected range of points
177          * @return true if successful
178          */
179         public boolean deleteRange()
180         {
181                 int startSel = _selection.getStart();
182                 int endSel = _selection.getEnd();
183                 boolean answer = _track.deleteRange(startSel, endSel);
184                 // clear range selection
185                 _selection.modifyRangeDeleted();
186                 return answer;
187         }
188
189
190         /**
191          * Delete the currently selected point
192          * @return true if point deleted
193          */
194         public boolean deletePoint()
195         {
196                 if (_track.deletePoint(_selection.getCurrentPointIndex()))
197                 {
198                         _selection.modifyPointDeleted();
199                         _broker.informSubscribers();
200                         return true;
201                 }
202                 return false;
203         }
204
205
206         /**
207          * Delete the currently selected photo and optionally its point too
208          * @param inPointToo true to also delete associated point
209          * @return true if delete successful
210          */
211         public boolean deleteCurrentPhoto(boolean inPointToo)
212         {
213                 // delete currently selected photo
214                 int photoIndex = _selection.getCurrentPhotoIndex();
215                 if (photoIndex >= 0)
216                 {
217                         Photo photo = _photoList.getPhoto(photoIndex);
218                         _photoList.deletePhoto(photoIndex);
219                         // has it got a point?
220                         if (photo.getDataPoint() != null)
221                         {
222                                 if (inPointToo)
223                                 {
224                                         // delete point
225                                         int pointIndex = _track.getPointIndex(photo.getDataPoint());
226                                         _track.deletePoint(pointIndex);
227                                 }
228                                 else
229                                 {
230                                         // disconnect point from photo
231                                         photo.getDataPoint().setPhoto(null);
232                                         photo.setDataPoint(null);
233                                 }
234                         }
235                         // update subscribers
236                         _selection.modifyPointDeleted();
237                         _broker.informSubscribers();
238                 }
239                 return true;
240         }
241
242
243         /**
244          * Compress the track to the given resolution
245          * @param inResolution resolution
246          * @return number of points deleted
247          */
248         public int compress(int inResolution)
249         {
250                 int numDeleted = _track.compress(inResolution);
251                 if (numDeleted > 0)
252                         _selection.clearAll();
253                 return numDeleted;
254         }
255
256
257         /**
258          * Delete all the duplicate points in the track
259          * @return number of points deleted
260          */
261         public int deleteDuplicates()
262         {
263                 int numDeleted = _track.deleteDuplicates();
264                 if (numDeleted > 0)
265                         _selection.clearAll();
266                 return numDeleted;
267         }
268
269
270         /**
271          * Clone the selected range of data points
272          * @return shallow copy of DataPoint objects
273          */
274         public DataPoint[] cloneSelectedRange()
275         {
276                 return _track.cloneRange(_selection.getStart(), _selection.getEnd());
277         }
278
279
280         /**
281          * Interpolate extra points between two selected ones
282          * @param inStartIndex start index of interpolation
283          * @param inNumPoints num points to insert
284          * @return true if successful
285          */
286         public boolean interpolate(int inNumPoints)
287         {
288                 boolean success = _track.interpolate(_selection.getStart(), inNumPoints);
289                 if (success)
290                         _selection.selectRangeEnd(_selection.getEnd() + inNumPoints);
291                 return success;
292         }
293
294
295         /**
296          * Select the given DataPoint
297          * @param inPoint DataPoint object to select
298          */
299         public void selectPoint(DataPoint inPoint)
300         {
301                 // get the index of the given Point
302                 int index = _track.getPointIndex(inPoint);
303                 // give to selection
304                 _selection.selectPoint(index);
305         }
306
307         /**
308          * Select the given Photo and its point if any
309          * @param inPhotoIndex index of photo to select
310          */
311         public void selectPhoto(int inPhotoIndex)
312         {
313                 // Find Photo object
314                 Photo photo = _photoList.getPhoto(inPhotoIndex);
315                 if (photo != null)
316                 {
317                         // Find point object and its index
318                         int pointIndex = _track.getPointIndex(photo.getDataPoint());
319                         // give to selection object
320                         _selection.selectPhotoAndPoint(inPhotoIndex, pointIndex);
321                 }
322                 else
323                 {
324                         // no photo, just reset selection
325                         _selection.selectPhotoAndPoint(-1, -1);
326                 }
327         }
328
329
330         /**
331          * Fire a trigger to all data subscribers
332          */
333         public void triggerUpdate()
334         {
335                 _broker.informSubscribers();
336         }
337 }