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 Track _track = null;
14 private Selection _selection = null;
15 private FileInfo _fileInfo = null;
16 private PhotoList _photoList = null;
21 * @param inTrack Track object
23 public TrackInfo(Track inTrack)
26 _selection = new Selection(_track);
27 _fileInfo = new FileInfo();
28 _photoList = new PhotoList();
33 * @return the Track object
35 public Track getTrack()
42 * @return the Selection object
44 public Selection getSelection()
51 * @return the FileInfo object
53 public FileInfo getFileInfo()
59 * @return the PhotoList object
61 public PhotoList getPhotoList()
67 * Get the currently selected point, if any
68 * @return DataPoint if single point selected, otherwise null
70 public DataPoint getCurrentPoint()
72 return _track.getPoint(_selection.getCurrentPointIndex());
76 * Get the currently selected photo, if any
77 * @return Photo if selected, otherwise null
79 public Photo getCurrentPhoto()
81 return _photoList.getPhoto(_selection.getCurrentPhotoIndex());
86 * Load the specified data into the Track
87 * @param inFieldArray array of Field objects describing fields
88 * @param inPointArray 2d object array containing data
89 * @param inAltFormat altitude format
91 public void loadTrack(Field[] inFieldArray, Object[][] inPointArray, int inAltFormat)
94 _track.load(inFieldArray, inPointArray, inAltFormat);
95 _selection.clearAll();
100 * Add a Set of Photos
101 * @param inSet Set containing Photo objects
102 * @return array containing number of photos and number of points added
104 public int[] addPhotos(Set inSet)
106 // Firstly count number of points and photos to add
107 int numPhotosToAdd = 0;
108 int numPointsToAdd = 0;
109 Iterator iterator = null;
110 if (inSet != null && !inSet.isEmpty())
112 iterator = inSet.iterator();
113 while (iterator.hasNext())
117 Photo photo = (Photo) iterator.next();
118 if (photo != null && !_photoList.contains(photo))
121 if (photo.getDataPoint() != null)
127 catch (ClassCastException ce) {}
130 // If there are any photos to add, add them
131 if (numPhotosToAdd > 0)
133 DataPoint[] dataPoints = new DataPoint[numPointsToAdd];
135 boolean hasAltitude = false;
136 // Add each Photo in turn
137 iterator = inSet.iterator();
138 while (iterator.hasNext())
142 Photo photo = (Photo) iterator.next();
143 if (photo != null && !_photoList.contains(photo))
146 _photoList.addPhoto(photo);
147 // Add point if there is one
148 if (photo.getDataPoint() != null)
150 dataPoints[pointNum] = photo.getDataPoint();
151 // Check if any points have altitudes
152 hasAltitude |= (photo.getDataPoint().getAltitude() != null);
157 catch (ClassCastException ce) {}
159 if (numPointsToAdd > 0)
161 // add points to track
162 _track.appendPoints(dataPoints);
163 // modify track field list
164 _track.getFieldList().extendList(Field.LATITUDE);
165 _track.getFieldList().extendList(Field.LONGITUDE);
166 if (hasAltitude) {_track.getFieldList().extendList(Field.ALTITUDE);}
169 int[] result = {numPhotosToAdd, numPointsToAdd};
175 * Delete the currently selected range of points
176 * @return true if successful
178 public boolean deleteRange()
180 int startSel = _selection.getStart();
181 int endSel = _selection.getEnd();
182 boolean answer = _track.deleteRange(startSel, endSel);
183 // clear range selection
184 _selection.modifyRangeDeleted();
190 * Delete the currently selected point
191 * @return true if point deleted
193 public boolean deletePoint()
195 if (_track.deletePoint(_selection.getCurrentPointIndex()))
197 _selection.modifyPointDeleted();
198 UpdateMessageBroker.informSubscribers();
206 * Delete the currently selected photo and optionally its point too
207 * @param inPointToo true to also delete associated point
208 * @return true if delete successful
210 public boolean deleteCurrentPhoto(boolean inPointToo)
212 // delete currently selected photo
213 int photoIndex = _selection.getCurrentPhotoIndex();
216 Photo photo = _photoList.getPhoto(photoIndex);
217 _photoList.deletePhoto(photoIndex);
218 // has it got a point?
219 if (photo.getDataPoint() != null)
224 int pointIndex = _track.getPointIndex(photo.getDataPoint());
225 _track.deletePoint(pointIndex);
229 // disconnect point from photo
230 photo.getDataPoint().setPhoto(null);
231 photo.setDataPoint(null);
234 // update subscribers
235 _selection.modifyPointDeleted();
236 UpdateMessageBroker.informSubscribers();
243 * Compress the track to the given resolution
244 * @param inResolution resolution
245 * @return number of points deleted
247 public int compress(int inResolution)
249 int numDeleted = _track.compress(inResolution);
250 if (numDeleted > 0) {
251 _selection.clearAll();
252 UpdateMessageBroker.informSubscribers();
259 * Delete all the duplicate points in the track
260 * @return number of points deleted
262 public int deleteDuplicates()
264 int numDeleted = _track.deleteDuplicates();
265 if (numDeleted > 0) {
266 _selection.clearAll();
267 UpdateMessageBroker.informSubscribers();
274 * Clone the selected range of data points
275 * @return shallow copy of DataPoint objects
277 public DataPoint[] cloneSelectedRange()
279 return _track.cloneRange(_selection.getStart(), _selection.getEnd());
284 * Interpolate extra points between two selected ones
285 * @param inStartIndex start index of interpolation
286 * @param inNumPoints num points to insert
287 * @return true if successful
289 public boolean interpolate(int inNumPoints)
291 boolean success = _track.interpolate(_selection.getStart(), inNumPoints);
293 _selection.selectRangeEnd(_selection.getEnd() + inNumPoints);
299 * Select the given DataPoint
300 * @param inPoint DataPoint object to select
302 public void selectPoint(DataPoint inPoint)
304 // get the index of the given Point
305 int index = _track.getPointIndex(inPoint);
307 _selection.selectPoint(index);
311 * Select the given Photo and its point if any
312 * @param inPhotoIndex index of photo to select
314 public void selectPhoto(int inPhotoIndex)
317 Photo photo = _photoList.getPhoto(inPhotoIndex);
320 // Find point object and its index
321 int pointIndex = _track.getPointIndex(photo.getDataPoint());
322 // give to selection object
323 _selection.selectPhotoAndPoint(inPhotoIndex, pointIndex);
327 // no photo, just reset selection
328 _selection.selectPhotoAndPoint(-1, -1);