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());
87 * @param inSet Set containing Photo objects
88 * @return array containing number of photos and number of points added
90 public int[] addPhotos(Set<Photo> inSet)
92 // Firstly count number of points and photos to add
93 int numPhotosToAdd = 0;
94 int numPointsToAdd = 0;
95 Iterator<Photo> iterator = null;
96 if (inSet != null && !inSet.isEmpty())
98 iterator = inSet.iterator();
99 while (iterator.hasNext())
103 Photo photo = iterator.next();
104 if (photo != null && !_photoList.contains(photo))
107 if (photo.getDataPoint() != null)
113 catch (ClassCastException ce) {}
116 // If there are any photos to add, add them
117 if (numPhotosToAdd > 0)
119 DataPoint[] dataPoints = new DataPoint[numPointsToAdd];
121 boolean hasAltitude = false;
122 // Add each Photo in turn
123 iterator = inSet.iterator();
124 while (iterator.hasNext())
128 Photo photo = iterator.next();
129 if (photo != null && !_photoList.contains(photo))
132 _photoList.addPhoto(photo);
133 // Add point if there is one
134 if (photo.getDataPoint() != null)
136 dataPoints[pointNum] = photo.getDataPoint();
137 // Check if any points have altitudes
138 hasAltitude |= (photo.getDataPoint().getAltitude() != null);
143 catch (ClassCastException ce) {}
145 if (numPointsToAdd > 0)
147 // add points to track
148 _track.appendPoints(dataPoints);
149 // modify track field list
150 _track.getFieldList().extendList(Field.LATITUDE);
151 _track.getFieldList().extendList(Field.LONGITUDE);
152 if (hasAltitude) {_track.getFieldList().extendList(Field.ALTITUDE);}
155 int[] result = {numPhotosToAdd, numPointsToAdd};
161 * Delete the currently selected range of points
162 * @return true if successful
164 public boolean deleteRange()
166 int startSel = _selection.getStart();
167 int endSel = _selection.getEnd();
168 boolean answer = _track.deleteRange(startSel, endSel);
169 // clear range selection
170 _selection.modifyRangeDeleted();
176 * Delete the currently selected point
177 * @return true if point deleted
179 public boolean deletePoint()
181 if (_track.deletePoint(_selection.getCurrentPointIndex()))
183 _selection.modifyPointDeleted();
184 UpdateMessageBroker.informSubscribers();
192 * Delete the currently selected photo and optionally its point too
193 * @param inPointToo true to also delete associated point
194 * @return true if delete successful
196 public boolean deleteCurrentPhoto(boolean inPointToo)
198 // delete currently selected photo
199 int photoIndex = _selection.getCurrentPhotoIndex();
202 Photo photo = _photoList.getPhoto(photoIndex);
203 _photoList.deletePhoto(photoIndex);
204 // has it got a point?
205 if (photo.getDataPoint() != null)
210 int pointIndex = _track.getPointIndex(photo.getDataPoint());
211 _track.deletePoint(pointIndex);
215 // disconnect point from photo
216 photo.getDataPoint().setPhoto(null);
217 photo.setDataPoint(null);
220 // update subscribers
221 _selection.modifyPointDeleted();
222 UpdateMessageBroker.informSubscribers();
229 * Delete all the points which have been marked for deletion
230 * @return number of points deleted
232 public int deleteMarkedPoints()
234 int numDeleted = _track.deleteMarkedPoints();
235 if (numDeleted > 0) {
236 _selection.clearAll();
237 UpdateMessageBroker.informSubscribers();
244 * Clone the selected range of data points
245 * @return shallow copy of DataPoint objects
247 public DataPoint[] cloneSelectedRange()
249 return _track.cloneRange(_selection.getStart(), _selection.getEnd());
254 * Interpolate extra points between two selected ones
255 * @param inNumPoints num points to insert
256 * @return true if successful
258 public boolean interpolate(int inNumPoints)
260 boolean success = _track.interpolate(_selection.getStart(), inNumPoints);
262 _selection.selectRangeEnd(_selection.getEnd() + inNumPoints);
269 * Average selected points to create a new one
270 * @return true if successful
272 public boolean average()
274 boolean success = _track.average(_selection.getStart(), _selection.getEnd());
276 _selection.selectPoint(_selection.getEnd()+1);
283 * Select the given DataPoint
284 * @param inPoint DataPoint object to select
286 public void selectPoint(DataPoint inPoint)
288 // get the index of the given Point
289 int index = _track.getPointIndex(inPoint);
291 _selection.selectPoint(index);
295 * Select the given Photo and its point if any
296 * @param inPhotoIndex index of photo to select
298 public void selectPhoto(int inPhotoIndex)
301 Photo photo = _photoList.getPhoto(inPhotoIndex);
304 // Find point object and its index
305 int pointIndex = _track.getPointIndex(photo.getDataPoint());
306 // give to selection object
307 _selection.selectPhotoAndPoint(inPhotoIndex, pointIndex);
311 // no photo, just reset selection
312 _selection.selectPhotoAndPoint(-1, -1);