]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/TrackInfo.java
Version 7, February 2009
[GpsPrune.git] / tim / prune / data / TrackInfo.java
1 package tim.prune.data;
2
3 import java.util.Iterator;
4 import java.util.Set;
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 Track _track = null;
14         private Selection _selection = null;
15         private FileInfo _fileInfo = null;
16         private PhotoList _photoList = null;
17
18
19         /**
20          * Constructor
21          * @param inTrack Track object
22          */
23         public TrackInfo(Track inTrack)
24         {
25                 _track = inTrack;
26                 _selection = new Selection(_track);
27                 _fileInfo = new FileInfo();
28                 _photoList = new PhotoList();
29         }
30
31
32         /**
33          * @return the Track object
34          */
35         public Track getTrack()
36         {
37                 return _track;
38         }
39
40
41         /**
42          * @return the Selection object
43          */
44         public Selection getSelection()
45         {
46                 return _selection;
47         }
48
49
50         /**
51          * @return the FileInfo object
52          */
53         public FileInfo getFileInfo()
54         {
55                 return _fileInfo;
56         }
57
58         /**
59          * @return the PhotoList object
60          */
61         public PhotoList getPhotoList()
62         {
63                 return _photoList;
64         }
65
66         /**
67          * Get the currently selected point, if any
68          * @return DataPoint if single point selected, otherwise null
69          */
70         public DataPoint getCurrentPoint()
71         {
72                 return _track.getPoint(_selection.getCurrentPointIndex());
73         }
74
75         /**
76          * Get the currently selected photo, if any
77          * @return Photo if selected, otherwise null
78          */
79         public Photo getCurrentPhoto()
80         {
81                 return _photoList.getPhoto(_selection.getCurrentPhotoIndex());
82         }
83
84
85         /**
86          * Add a Set of Photos
87          * @param inSet Set containing Photo objects
88          * @return array containing number of photos and number of points added
89          */
90         public int[] addPhotos(Set<Photo> inSet)
91         {
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())
97                 {
98                         iterator = inSet.iterator();
99                         while (iterator.hasNext())
100                         {
101                                 try
102                                 {
103                                         Photo photo = iterator.next();
104                                         if (photo != null && !_photoList.contains(photo))
105                                         {
106                                                 numPhotosToAdd++;
107                                                 if (photo.getDataPoint() != null)
108                                                 {
109                                                         numPointsToAdd++;
110                                                 }
111                                         }
112                                 }
113                                 catch (ClassCastException ce) {}
114                         }
115                 }
116                 // If there are any photos to add, add them
117                 if (numPhotosToAdd > 0)
118                 {
119                         DataPoint[] dataPoints = new DataPoint[numPointsToAdd];
120                         int pointNum = 0;
121                         boolean hasAltitude = false;
122                         // Add each Photo in turn
123                         iterator = inSet.iterator();
124                         while (iterator.hasNext())
125                         {
126                                 try
127                                 {
128                                         Photo photo = iterator.next();
129                                         if (photo != null && !_photoList.contains(photo))
130                                         {
131                                                 // Add photo
132                                                 _photoList.addPhoto(photo);
133                                                 // Add point if there is one
134                                                 if (photo.getDataPoint() != null)
135                                                 {
136                                                         dataPoints[pointNum] = photo.getDataPoint();
137                                                         // Check if any points have altitudes
138                                                         hasAltitude |= (photo.getDataPoint().getAltitude() != null);
139                                                         pointNum++;
140                                                 }
141                                         }
142                                 }
143                                 catch (ClassCastException ce) {}
144                         }
145                         if (numPointsToAdd > 0)
146                         {
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);}
153                         }
154                 }
155                 int[] result = {numPhotosToAdd, numPointsToAdd};
156                 return result;
157         }
158
159
160         /**
161          * Delete the currently selected range of points
162          * @return true if successful
163          */
164         public boolean deleteRange()
165         {
166                 int startSel = _selection.getStart();
167                 int endSel = _selection.getEnd();
168                 boolean answer = _track.deleteRange(startSel, endSel);
169                 // clear range selection
170                 _selection.modifyRangeDeleted();
171                 return answer;
172         }
173
174
175         /**
176          * Delete the currently selected point
177          * @return true if point deleted
178          */
179         public boolean deletePoint()
180         {
181                 if (_track.deletePoint(_selection.getCurrentPointIndex()))
182                 {
183                         _selection.modifyPointDeleted();
184                         UpdateMessageBroker.informSubscribers();
185                         return true;
186                 }
187                 return false;
188         }
189
190
191         /**
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
195          */
196         public boolean deleteCurrentPhoto(boolean inPointToo)
197         {
198                 // delete currently selected photo
199                 int photoIndex = _selection.getCurrentPhotoIndex();
200                 if (photoIndex >= 0)
201                 {
202                         Photo photo = _photoList.getPhoto(photoIndex);
203                         _photoList.deletePhoto(photoIndex);
204                         // has it got a point?
205                         if (photo.getDataPoint() != null)
206                         {
207                                 if (inPointToo)
208                                 {
209                                         // delete point
210                                         int pointIndex = _track.getPointIndex(photo.getDataPoint());
211                                         _track.deletePoint(pointIndex);
212                                 }
213                                 else
214                                 {
215                                         // disconnect point from photo
216                                         photo.getDataPoint().setPhoto(null);
217                                         photo.setDataPoint(null);
218                                 }
219                         }
220                         // update subscribers
221                         _selection.modifyPointDeleted();
222                         UpdateMessageBroker.informSubscribers();
223                 }
224                 return true;
225         }
226
227
228         /**
229          * Delete all the points which have been marked for deletion
230          * @return number of points deleted
231          */
232         public int deleteMarkedPoints()
233         {
234                 int numDeleted = _track.deleteMarkedPoints();
235                 if (numDeleted > 0) {
236                         _selection.clearAll();
237                         UpdateMessageBroker.informSubscribers();
238                 }
239                 return numDeleted;
240         }
241
242
243         /**
244          * Clone the selected range of data points
245          * @return shallow copy of DataPoint objects
246          */
247         public DataPoint[] cloneSelectedRange()
248         {
249                 return _track.cloneRange(_selection.getStart(), _selection.getEnd());
250         }
251
252
253         /**
254          * Interpolate extra points between two selected ones
255          * @param inNumPoints num points to insert
256          * @return true if successful
257          */
258         public boolean interpolate(int inNumPoints)
259         {
260                 boolean success = _track.interpolate(_selection.getStart(), inNumPoints);
261                 if (success) {
262                         _selection.selectRangeEnd(_selection.getEnd() + inNumPoints);
263                 }
264                 return success;
265         }
266
267
268         /**
269          * Average selected points to create a new one
270          * @return true if successful
271          */
272         public boolean average()
273         {
274                 boolean success = _track.average(_selection.getStart(), _selection.getEnd());
275                 if (success) {
276                         _selection.selectPoint(_selection.getEnd()+1);
277                 }
278                 return success;
279         }
280
281
282         /**
283          * Select the given DataPoint
284          * @param inPoint DataPoint object to select
285          */
286         public void selectPoint(DataPoint inPoint)
287         {
288                 // get the index of the given Point
289                 int index = _track.getPointIndex(inPoint);
290                 // give to selection
291                 _selection.selectPoint(index);
292         }
293
294         /**
295          * Select the given Photo and its point if any
296          * @param inPhotoIndex index of photo to select
297          */
298         public void selectPhoto(int inPhotoIndex)
299         {
300                 // Find Photo object
301                 Photo photo = _photoList.getPhoto(inPhotoIndex);
302                 if (photo != null)
303                 {
304                         // Find point object and its index
305                         int pointIndex = _track.getPointIndex(photo.getDataPoint());
306                         // give to selection object
307                         _selection.selectPhotoAndPoint(inPhotoIndex, pointIndex);
308                 }
309                 else
310                 {
311                         // no photo, just reset selection
312                         _selection.selectPhotoAndPoint(-1, -1);
313                 }
314         }
315 }