]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/TrackInfo.java
Version 4, January 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 UpdateMessageBroker _broker = null;
14         private Track _track = null;
15         private Selection _selection = null;
16         private FileInfo _fileInfo = null;
17         private PhotoList _photoList = null;
18
19
20         /**
21          * Constructor
22          * @param inTrack Track object
23          * @param inBroker broker object
24          */
25         public TrackInfo(Track inTrack, UpdateMessageBroker inBroker)
26         {
27                 _broker = inBroker;
28                 _track = inTrack;
29                 _selection = new Selection(_track, inBroker);
30                 _fileInfo = new FileInfo();
31                 _photoList = new PhotoList();
32         }
33
34
35         /**
36          * @return the Track object
37          */
38         public Track getTrack()
39         {
40                 return _track;
41         }
42
43
44         /**
45          * @return the Selection object
46          */
47         public Selection getSelection()
48         {
49                 return _selection;
50         }
51
52
53         /**
54          * @return the FileInfo object
55          */
56         public FileInfo getFileInfo()
57         {
58                 return _fileInfo;
59         }
60
61         /**
62          * @return the PhotoList object
63          */
64         public PhotoList getPhotoList()
65         {
66                 return _photoList;
67         }
68
69         /**
70          * Get the currently selected point, if any
71          * @return DataPoint if single point selected, otherwise null
72          */
73         public DataPoint getCurrentPoint()
74         {
75                 return _track.getPoint(_selection.getCurrentPointIndex());
76         }
77
78         /**
79          * Get the currently selected photo, if any
80          * @return Photo if selected, otherwise null
81          */
82         public Photo getCurrentPhoto()
83         {
84                 return _photoList.getPhoto(_selection.getCurrentPhotoIndex());
85         }
86
87
88         /**
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
93          */
94         public void loadTrack(Field[] inFieldArray, Object[][] inPointArray, int inAltFormat)
95         {
96                 _track.cropTo(0);
97                 _track.load(inFieldArray, inPointArray, inAltFormat);
98                 _selection.clearAll();
99         }
100
101
102         /**
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
106          */
107         public int[] addPhotos(Set inSet)
108         {
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())
114                 {
115                         iterator = inSet.iterator();
116                         while (iterator.hasNext())
117                         {
118                                 try
119                                 {
120                                         Photo photo = (Photo) iterator.next();
121                                         if (photo != null && !_photoList.contains(photo))
122                                         {
123                                                 numPhotosToAdd++;
124                                                 if (photo.getDataPoint() != null)
125                                                 {
126                                                         numPointsToAdd++;
127                                                 }
128                                         }
129                                 }
130                                 catch (ClassCastException ce) {}
131                         }
132                 }
133                 // If there are any photos to add, add them
134                 if (numPhotosToAdd > 0)
135                 {
136                         DataPoint[] dataPoints = new DataPoint[numPointsToAdd];
137                         int pointNum = 0;
138                         boolean hasAltitude = false;
139                         // Add each Photo in turn
140                         iterator = inSet.iterator();
141                         while (iterator.hasNext())
142                         {
143                                 try
144                                 {
145                                         Photo photo = (Photo) iterator.next();
146                                         if (photo != null && !_photoList.contains(photo))
147                                         {
148                                                 // Add photo
149                                                 _photoList.addPhoto(photo);
150                                                 // Add point if there is one
151                                                 if (photo.getDataPoint() != null)
152                                                 {
153                                                         dataPoints[pointNum] = photo.getDataPoint();
154                                                         // Check if any points have altitudes
155                                                         hasAltitude |= (photo.getDataPoint().getAltitude() != null);
156                                                         pointNum++;
157                                                 }
158                                         }
159                                 }
160                                 catch (ClassCastException ce) {}
161                         }
162                         if (numPointsToAdd > 0)
163                         {
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);}
170                         }
171                 }
172                 int[] result = {numPhotosToAdd, numPointsToAdd};
173                 return result;
174         }
175
176
177         /**
178          * Delete the currently selected range of points
179          * @return true if successful
180          */
181         public boolean deleteRange()
182         {
183                 int startSel = _selection.getStart();
184                 int endSel = _selection.getEnd();
185                 boolean answer = _track.deleteRange(startSel, endSel);
186                 // clear range selection
187                 _selection.modifyRangeDeleted();
188                 return answer;
189         }
190
191
192         /**
193          * Delete the currently selected point
194          * @return true if point deleted
195          */
196         public boolean deletePoint()
197         {
198                 if (_track.deletePoint(_selection.getCurrentPointIndex()))
199                 {
200                         _selection.modifyPointDeleted();
201                         _broker.informSubscribers();
202                         return true;
203                 }
204                 return false;
205         }
206
207
208         /**
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
212          */
213         public boolean deleteCurrentPhoto(boolean inPointToo)
214         {
215                 // delete currently selected photo
216                 int photoIndex = _selection.getCurrentPhotoIndex();
217                 if (photoIndex >= 0)
218                 {
219                         Photo photo = _photoList.getPhoto(photoIndex);
220                         _photoList.deletePhoto(photoIndex);
221                         // has it got a point?
222                         if (photo.getDataPoint() != null)
223                         {
224                                 if (inPointToo)
225                                 {
226                                         // delete point
227                                         int pointIndex = _track.getPointIndex(photo.getDataPoint());
228                                         _track.deletePoint(pointIndex);
229                                 }
230                                 else
231                                 {
232                                         // disconnect point from photo
233                                         photo.getDataPoint().setPhoto(null);
234                                         photo.setDataPoint(null);
235                                 }
236                         }
237                         // update subscribers
238                         _selection.modifyPointDeleted();
239                         _broker.informSubscribers();
240                 }
241                 return true;
242         }
243
244
245         /**
246          * Compress the track to the given resolution
247          * @param inResolution resolution
248          * @return number of points deleted
249          */
250         public int compress(int inResolution)
251         {
252                 int numDeleted = _track.compress(inResolution);
253                 if (numDeleted > 0) {
254                         _selection.clearAll();
255                         _broker.informSubscribers();
256                 }
257                 return numDeleted;
258         }
259
260
261         /**
262          * Delete all the duplicate points in the track
263          * @return number of points deleted
264          */
265         public int deleteDuplicates()
266         {
267                 int numDeleted = _track.deleteDuplicates();
268                 if (numDeleted > 0) {
269                         _selection.clearAll();
270                         _broker.informSubscribers();
271                 }
272                 return numDeleted;
273         }
274
275
276         /**
277          * Clone the selected range of data points
278          * @return shallow copy of DataPoint objects
279          */
280         public DataPoint[] cloneSelectedRange()
281         {
282                 return _track.cloneRange(_selection.getStart(), _selection.getEnd());
283         }
284
285
286         /**
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
291          */
292         public boolean interpolate(int inNumPoints)
293         {
294                 boolean success = _track.interpolate(_selection.getStart(), inNumPoints);
295                 if (success)
296                         _selection.selectRangeEnd(_selection.getEnd() + inNumPoints);
297                 return success;
298         }
299
300
301         /**
302          * Select the given DataPoint
303          * @param inPoint DataPoint object to select
304          */
305         public void selectPoint(DataPoint inPoint)
306         {
307                 // get the index of the given Point
308                 int index = _track.getPointIndex(inPoint);
309                 // give to selection
310                 _selection.selectPoint(index);
311         }
312
313         /**
314          * Select the given Photo and its point if any
315          * @param inPhotoIndex index of photo to select
316          */
317         public void selectPhoto(int inPhotoIndex)
318         {
319                 // Find Photo object
320                 Photo photo = _photoList.getPhoto(inPhotoIndex);
321                 if (photo != null)
322                 {
323                         // Find point object and its index
324                         int pointIndex = _track.getPointIndex(photo.getDataPoint());
325                         // give to selection object
326                         _selection.selectPhotoAndPoint(inPhotoIndex, pointIndex);
327                 }
328                 else
329                 {
330                         // no photo, just reset selection
331                         _selection.selectPhotoAndPoint(-1, -1);
332                 }
333         }
334
335
336         /**
337          * Fire a trigger to all data subscribers
338          */
339         public void triggerUpdate()
340         {
341                 _broker.informSubscribers();
342         }
343 }