1 package tim.prune.data;
3 import java.util.Iterator;
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 Set of Photos
104 * @param inSet Set containing Photo objects
105 * @return array containing number of photos and number of points added
107 public int[] addPhotos(Set inSet)
109 // Firstly count number of points and photos to add
110 int numPhotosToAdd = 0;
111 int numPointsToAdd = 0;
112 Iterator iterator = null;
113 if (inSet != null && !inSet.isEmpty())
115 iterator = inSet.iterator();
116 while (iterator.hasNext())
120 Photo photo = (Photo) iterator.next();
121 if (photo != null && !_photoList.contains(photo))
124 if (photo.getDataPoint() != null)
130 catch (ClassCastException ce) {}
133 // If there are any photos to add, add them
134 if (numPhotosToAdd > 0)
136 DataPoint[] dataPoints = new DataPoint[numPointsToAdd];
138 boolean hasAltitude = false;
139 // Add each Photo in turn
140 iterator = inSet.iterator();
141 while (iterator.hasNext())
145 Photo photo = (Photo) iterator.next();
146 if (photo != null && !_photoList.contains(photo))
149 _photoList.addPhoto(photo);
150 // Add point if there is one
151 if (photo.getDataPoint() != null)
153 dataPoints[pointNum] = photo.getDataPoint();
154 // Check if any points have altitudes
155 hasAltitude |= (photo.getDataPoint().getAltitude() != null);
160 catch (ClassCastException ce) {}
162 if (numPointsToAdd > 0)
164 // add points to track
165 _track.appendPoints(dataPoints);
166 // modify track field list
167 _track.getFieldList().extendList(Field.LATITUDE);
168 _track.getFieldList().extendList(Field.LONGITUDE);
169 if (hasAltitude) {_track.getFieldList().extendList(Field.ALTITUDE);}
172 int[] result = {numPhotosToAdd, numPointsToAdd};
178 * Delete the currently selected range of points
179 * @return true if successful
181 public boolean deleteRange()
183 int startSel = _selection.getStart();
184 int endSel = _selection.getEnd();
185 boolean answer = _track.deleteRange(startSel, endSel);
186 // clear range selection
187 _selection.modifyRangeDeleted();
193 * Delete the currently selected point
194 * @return true if point deleted
196 public boolean deletePoint()
198 if (_track.deletePoint(_selection.getCurrentPointIndex()))
200 _selection.modifyPointDeleted();
201 _broker.informSubscribers();
209 * Delete the currently selected photo and optionally its point too
210 * @param inPointToo true to also delete associated point
211 * @return true if delete successful
213 public boolean deleteCurrentPhoto(boolean inPointToo)
215 // delete currently selected photo
216 int photoIndex = _selection.getCurrentPhotoIndex();
219 Photo photo = _photoList.getPhoto(photoIndex);
220 _photoList.deletePhoto(photoIndex);
221 // has it got a point?
222 if (photo.getDataPoint() != null)
227 int pointIndex = _track.getPointIndex(photo.getDataPoint());
228 _track.deletePoint(pointIndex);
232 // disconnect point from photo
233 photo.getDataPoint().setPhoto(null);
234 photo.setDataPoint(null);
237 // update subscribers
238 _selection.modifyPointDeleted();
239 _broker.informSubscribers();
246 * Compress the track to the given resolution
247 * @param inResolution resolution
248 * @return number of points deleted
250 public int compress(int inResolution)
252 int numDeleted = _track.compress(inResolution);
253 if (numDeleted > 0) {
254 _selection.clearAll();
255 _broker.informSubscribers();
262 * Delete all the duplicate points in the track
263 * @return number of points deleted
265 public int deleteDuplicates()
267 int numDeleted = _track.deleteDuplicates();
268 if (numDeleted > 0) {
269 _selection.clearAll();
270 _broker.informSubscribers();
277 * Clone the selected range of data points
278 * @return shallow copy of DataPoint objects
280 public DataPoint[] cloneSelectedRange()
282 return _track.cloneRange(_selection.getStart(), _selection.getEnd());
287 * Interpolate extra points between two selected ones
288 * @param inStartIndex start index of interpolation
289 * @param inNumPoints num points to insert
290 * @return true if successful
292 public boolean interpolate(int inNumPoints)
294 boolean success = _track.interpolate(_selection.getStart(), inNumPoints);
296 _selection.selectRangeEnd(_selection.getEnd() + inNumPoints);
302 * Select the given DataPoint
303 * @param inPoint DataPoint object to select
305 public void selectPoint(DataPoint inPoint)
307 // get the index of the given Point
308 int index = _track.getPointIndex(inPoint);
310 _selection.selectPoint(index);
314 * Select the given Photo and its point if any
315 * @param inPhotoIndex index of photo to select
317 public void selectPhoto(int inPhotoIndex)
320 Photo photo = _photoList.getPhoto(inPhotoIndex);
323 // Find point object and its index
324 int pointIndex = _track.getPointIndex(photo.getDataPoint());
325 // give to selection object
326 _selection.selectPhotoAndPoint(inPhotoIndex, pointIndex);
330 // no photo, just reset selection
331 _selection.selectPhotoAndPoint(-1, -1);
337 * Fire a trigger to all data subscribers
339 public void triggerUpdate()
341 _broker.informSubscribers();