]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/TrackInfo.java
Version 5, May 2008
[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          * 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
90          */
91         public void loadTrack(Field[] inFieldArray, Object[][] inPointArray, int inAltFormat)
92         {
93                 _track.cropTo(0);
94                 _track.load(inFieldArray, inPointArray, inAltFormat);
95                 _selection.clearAll();
96         }
97
98
99         /**
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
103          */
104         public int[] addPhotos(Set inSet)
105         {
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())
111                 {
112                         iterator = inSet.iterator();
113                         while (iterator.hasNext())
114                         {
115                                 try
116                                 {
117                                         Photo photo = (Photo) iterator.next();
118                                         if (photo != null && !_photoList.contains(photo))
119                                         {
120                                                 numPhotosToAdd++;
121                                                 if (photo.getDataPoint() != null)
122                                                 {
123                                                         numPointsToAdd++;
124                                                 }
125                                         }
126                                 }
127                                 catch (ClassCastException ce) {}
128                         }
129                 }
130                 // If there are any photos to add, add them
131                 if (numPhotosToAdd > 0)
132                 {
133                         DataPoint[] dataPoints = new DataPoint[numPointsToAdd];
134                         int pointNum = 0;
135                         boolean hasAltitude = false;
136                         // Add each Photo in turn
137                         iterator = inSet.iterator();
138                         while (iterator.hasNext())
139                         {
140                                 try
141                                 {
142                                         Photo photo = (Photo) iterator.next();
143                                         if (photo != null && !_photoList.contains(photo))
144                                         {
145                                                 // Add photo
146                                                 _photoList.addPhoto(photo);
147                                                 // Add point if there is one
148                                                 if (photo.getDataPoint() != null)
149                                                 {
150                                                         dataPoints[pointNum] = photo.getDataPoint();
151                                                         // Check if any points have altitudes
152                                                         hasAltitude |= (photo.getDataPoint().getAltitude() != null);
153                                                         pointNum++;
154                                                 }
155                                         }
156                                 }
157                                 catch (ClassCastException ce) {}
158                         }
159                         if (numPointsToAdd > 0)
160                         {
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);}
167                         }
168                 }
169                 int[] result = {numPhotosToAdd, numPointsToAdd};
170                 return result;
171         }
172
173
174         /**
175          * Delete the currently selected range of points
176          * @return true if successful
177          */
178         public boolean deleteRange()
179         {
180                 int startSel = _selection.getStart();
181                 int endSel = _selection.getEnd();
182                 boolean answer = _track.deleteRange(startSel, endSel);
183                 // clear range selection
184                 _selection.modifyRangeDeleted();
185                 return answer;
186         }
187
188
189         /**
190          * Delete the currently selected point
191          * @return true if point deleted
192          */
193         public boolean deletePoint()
194         {
195                 if (_track.deletePoint(_selection.getCurrentPointIndex()))
196                 {
197                         _selection.modifyPointDeleted();
198                         UpdateMessageBroker.informSubscribers();
199                         return true;
200                 }
201                 return false;
202         }
203
204
205         /**
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
209          */
210         public boolean deleteCurrentPhoto(boolean inPointToo)
211         {
212                 // delete currently selected photo
213                 int photoIndex = _selection.getCurrentPhotoIndex();
214                 if (photoIndex >= 0)
215                 {
216                         Photo photo = _photoList.getPhoto(photoIndex);
217                         _photoList.deletePhoto(photoIndex);
218                         // has it got a point?
219                         if (photo.getDataPoint() != null)
220                         {
221                                 if (inPointToo)
222                                 {
223                                         // delete point
224                                         int pointIndex = _track.getPointIndex(photo.getDataPoint());
225                                         _track.deletePoint(pointIndex);
226                                 }
227                                 else
228                                 {
229                                         // disconnect point from photo
230                                         photo.getDataPoint().setPhoto(null);
231                                         photo.setDataPoint(null);
232                                 }
233                         }
234                         // update subscribers
235                         _selection.modifyPointDeleted();
236                         UpdateMessageBroker.informSubscribers();
237                 }
238                 return true;
239         }
240
241
242         /**
243          * Compress the track to the given resolution
244          * @param inResolution resolution
245          * @return number of points deleted
246          */
247         public int compress(int inResolution)
248         {
249                 int numDeleted = _track.compress(inResolution);
250                 if (numDeleted > 0) {
251                         _selection.clearAll();
252                         UpdateMessageBroker.informSubscribers();
253                 }
254                 return numDeleted;
255         }
256
257
258         /**
259          * Delete all the duplicate points in the track
260          * @return number of points deleted
261          */
262         public int deleteDuplicates()
263         {
264                 int numDeleted = _track.deleteDuplicates();
265                 if (numDeleted > 0) {
266                         _selection.clearAll();
267                         UpdateMessageBroker.informSubscribers();
268                 }
269                 return numDeleted;
270         }
271
272
273         /**
274          * Clone the selected range of data points
275          * @return shallow copy of DataPoint objects
276          */
277         public DataPoint[] cloneSelectedRange()
278         {
279                 return _track.cloneRange(_selection.getStart(), _selection.getEnd());
280         }
281
282
283         /**
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
288          */
289         public boolean interpolate(int inNumPoints)
290         {
291                 boolean success = _track.interpolate(_selection.getStart(), inNumPoints);
292                 if (success)
293                         _selection.selectRangeEnd(_selection.getEnd() + inNumPoints);
294                 return success;
295         }
296
297
298         /**
299          * Select the given DataPoint
300          * @param inPoint DataPoint object to select
301          */
302         public void selectPoint(DataPoint inPoint)
303         {
304                 // get the index of the given Point
305                 int index = _track.getPointIndex(inPoint);
306                 // give to selection
307                 _selection.selectPoint(index);
308         }
309
310         /**
311          * Select the given Photo and its point if any
312          * @param inPhotoIndex index of photo to select
313          */
314         public void selectPhoto(int inPhotoIndex)
315         {
316                 // Find Photo object
317                 Photo photo = _photoList.getPhoto(inPhotoIndex);
318                 if (photo != null)
319                 {
320                         // Find point object and its index
321                         int pointIndex = _track.getPointIndex(photo.getDataPoint());
322                         // give to selection object
323                         _selection.selectPhotoAndPoint(inPhotoIndex, pointIndex);
324                 }
325                 else
326                 {
327                         // no photo, just reset selection
328                         _selection.selectPhotoAndPoint(-1, -1);
329                 }
330         }
331 }