1 package tim.prune.data;
5 import tim.prune.UpdateMessageBroker;
8 * Class to hold all track information, including data
9 * and the selection information
11 public class TrackInfo
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;
22 * @param inTrack Track object
23 * @param inBroker broker object
25 public TrackInfo(Track inTrack, UpdateMessageBroker inBroker)
29 _selection = new Selection(_track, inBroker);
30 _fileInfo = new FileInfo();
31 _photoList = new PhotoList();
36 * @return the Track object
38 public Track getTrack()
45 * @return the Selection object
47 public Selection getSelection()
54 * @return the FileInfo object
56 public FileInfo getFileInfo()
62 * @return the PhotoList object
64 public PhotoList getPhotoList()
70 * Get the currently selected point, if any
71 * @return DataPoint if single point selected, otherwise null
73 public DataPoint getCurrentPoint()
75 return _track.getPoint(_selection.getCurrentPointIndex());
79 * Get the currently selected photo, if any
80 * @return Photo if selected, otherwise null
82 public Photo getCurrentPhoto()
84 return _photoList.getPhoto(_selection.getCurrentPhotoIndex());
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
94 public void loadTrack(Field[] inFieldArray, Object[][] inPointArray, int inAltFormat)
97 _track.load(inFieldArray, inPointArray, inAltFormat);
98 _selection.clearAll();
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
107 public int[] addPhotos(List inList)
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())
115 for (int i=0; i<inList.size(); i++)
119 Photo photo = (Photo) inList.get(i);
120 if (photo != null && !_photoList.contains(photo))
123 if (photo.getDataPoint() != null)
129 catch (ClassCastException ce) {}
132 // If there are any photos to add, add them
133 if (numPhotosToAdd > 0)
135 DataPoint[] dataPoints = new DataPoint[numPointsToAdd];
137 boolean hasAltitude = false;
138 // Add each Photo in turn
139 for (int i=0; i<inList.size(); i++)
143 Photo photo = (Photo) inList.get(i);
144 if (photo != null && !_photoList.contains(photo))
147 _photoList.addPhoto(photo);
148 // Add point if there is one
149 if (photo.getDataPoint() != null)
151 dataPoints[pointNum] = photo.getDataPoint();
152 // Check if any points have altitudes
153 hasAltitude |= (photo.getDataPoint().getAltitude() != null);
158 catch (ClassCastException ce) {}
160 if (numPointsToAdd > 0)
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);}
170 int[] result = {numPhotosToAdd, numPointsToAdd};
176 * Delete the currently selected range of points
177 * @return true if successful
179 public boolean deleteRange()
181 int startSel = _selection.getStart();
182 int endSel = _selection.getEnd();
183 boolean answer = _track.deleteRange(startSel, endSel);
184 // clear range selection
185 _selection.modifyRangeDeleted();
191 * Delete the currently selected point
192 * @return true if point deleted
194 public boolean deletePoint()
196 if (_track.deletePoint(_selection.getCurrentPointIndex()))
198 _selection.modifyPointDeleted();
199 _broker.informSubscribers();
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
211 public boolean deleteCurrentPhoto(boolean inPointToo)
213 // delete currently selected photo
214 int photoIndex = _selection.getCurrentPhotoIndex();
217 Photo photo = _photoList.getPhoto(photoIndex);
218 _photoList.deletePhoto(photoIndex);
219 // has it got a point?
220 if (photo.getDataPoint() != null)
225 int pointIndex = _track.getPointIndex(photo.getDataPoint());
226 _track.deletePoint(pointIndex);
230 // disconnect point from photo
231 photo.getDataPoint().setPhoto(null);
232 photo.setDataPoint(null);
235 // update subscribers
236 _selection.modifyPointDeleted();
237 _broker.informSubscribers();
244 * Compress the track to the given resolution
245 * @param inResolution resolution
246 * @return number of points deleted
248 public int compress(int inResolution)
250 int numDeleted = _track.compress(inResolution);
252 _selection.clearAll();
258 * Delete all the duplicate points in the track
259 * @return number of points deleted
261 public int deleteDuplicates()
263 int numDeleted = _track.deleteDuplicates();
265 _selection.clearAll();
271 * Clone the selected range of data points
272 * @return shallow copy of DataPoint objects
274 public DataPoint[] cloneSelectedRange()
276 return _track.cloneRange(_selection.getStart(), _selection.getEnd());
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
286 public boolean interpolate(int inNumPoints)
288 boolean success = _track.interpolate(_selection.getStart(), inNumPoints);
290 _selection.selectRangeEnd(_selection.getEnd() + inNumPoints);
296 * Select the given DataPoint
297 * @param inPoint DataPoint object to select
299 public void selectPoint(DataPoint inPoint)
301 // get the index of the given Point
302 int index = _track.getPointIndex(inPoint);
304 _selection.selectPoint(index);
308 * Select the given Photo and its point if any
309 * @param inPhotoIndex index of photo to select
311 public void selectPhoto(int inPhotoIndex)
314 Photo photo = _photoList.getPhoto(inPhotoIndex);
317 // Find point object and its index
318 int pointIndex = _track.getPointIndex(photo.getDataPoint());
319 // give to selection object
320 _selection.selectPhotoAndPoint(inPhotoIndex, pointIndex);
324 // no photo, just reset selection
325 _selection.selectPhotoAndPoint(-1, -1);
331 * Fire a trigger to all data subscribers
333 public void triggerUpdate()
335 _broker.informSubscribers();