]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/TrackInfo.java
4695d351e6c124c398e0df90c911dbe0583c4dbe
[GpsPrune.git] / tim / prune / data / TrackInfo.java
1 package tim.prune.data;
2
3 import java.util.List;
4
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         // TODO: How to store photos? In separate list to be maintained or dynamic? Only store pointless photos?
18         private PhotoList _photoList = null;
19
20
21         /**
22          * Constructor
23          * @param inTrack Track object
24          * @param inBroker broker object
25          */
26         public TrackInfo(Track inTrack, UpdateMessageBroker inBroker)
27         {
28                 _broker = inBroker;
29                 _track = inTrack;
30                 _selection = new Selection(_track, inBroker);
31                 _fileInfo = new FileInfo();
32                 _photoList = new PhotoList();
33         }
34
35
36         /**
37          * @return the Track object
38          */
39         public Track getTrack()
40         {
41                 return _track;
42         }
43
44
45         /**
46          * @return the Selection object
47          */
48         public Selection getSelection()
49         {
50                 return _selection;
51         }
52
53
54         /**
55          * @return the FileInfo object
56          */
57         public FileInfo getFileInfo()
58         {
59                 return _fileInfo;
60         }
61
62         /**
63          * @return the PhotoList object
64          */
65         public PhotoList getPhotoList()
66         {
67                 return _photoList;
68         }
69
70         /**
71          * Get the currently selected point, if any
72          * @return DataPoint if single point selected, otherwise null
73          */
74         public DataPoint getCurrentPoint()
75         {
76                 return _track.getPoint(_selection.getCurrentPointIndex());
77         }
78
79
80         /**
81          * Load the specified data into the Track
82          * @param inFieldArray array of Field objects describing fields
83          * @param inPointArray 2d object array containing data
84          * @param inAltFormat altitude format
85          */
86         public void loadTrack(Field[] inFieldArray, Object[][] inPointArray, int inAltFormat)
87         {
88                 _track.cropTo(0);
89                 _track.load(inFieldArray, inPointArray, inAltFormat);
90                 _selection.clearAll();
91         }
92
93
94         /**
95          * Add a List of Photos
96          * @param inList List containing Photo objects
97          * @return number of photos added
98          */
99         public int addPhotos(List inList)
100         {
101                 // Firstly count number to add to make array
102                 int numPhotosToAdd = 0;
103                 if (inList != null && !inList.isEmpty())
104                 {
105                         for (int i=0; i<inList.size(); i++)
106                         {
107                                 try
108                                 {
109                                         Photo photo = (Photo) inList.get(i);
110                                         if (photo != null && !_photoList.contains(photo))
111                                         {
112                                                 numPhotosToAdd++;
113                                         }
114                                 }
115                                 catch (ClassCastException ce) {}
116                         }
117                 }
118                 // If there are any photos to add, add them
119                 if (numPhotosToAdd > 0)
120                 {
121                         DataPoint[] dataPoints = new DataPoint[numPhotosToAdd];
122                         int pointNum = 0;
123                         // Add each Photo in turn
124                         for (int i=0; i<inList.size(); i++)
125                         {
126                                 try
127                                 {
128                                         Photo photo = (Photo) inList.get(i);
129                                         if (photo != null && !_photoList.contains(photo))
130                                         {
131                                                 _photoList.addPhoto(photo);
132                                                 dataPoints[pointNum] = photo.getDataPoint();
133                                                 pointNum++;
134                                         }
135                                 }
136                                 catch (ClassCastException ce) {}
137                         }
138                         _track.appendPoints(dataPoints);
139                 }
140                 return numPhotosToAdd;
141         }
142
143
144         /**
145          * Delete the currently selected range of points
146          * @return true if successful
147          */
148         public boolean deleteRange()
149         {
150                 // TODO: Check whether to delete photos associated with this range
151                 int currPoint = _selection.getCurrentPointIndex();
152                 int startSel = _selection.getStart();
153                 int endSel = _selection.getEnd();
154                 boolean answer = _track.deleteRange(startSel, endSel);
155                 // clear range selection
156                 _selection.modifyRangeDeleted();
157                 return answer;
158         }
159
160
161         /**
162          * Delete the currently selected point
163          * @return true if point deleted
164          */
165         public boolean deletePoint()
166         {
167                 if (_track.deletePoint(_selection.getCurrentPointIndex()))
168                 {
169                         // TODO: Check whether to delete photo associated with this point
170                         _selection.modifyPointDeleted();
171                         _broker.informSubscribers();
172                         return true;
173                 }
174                 return false;
175         }
176
177
178         /**
179          * Compress the track to the given resolution
180          * @param inResolution resolution
181          * @return number of points deleted
182          */
183         public int compress(int inResolution)
184         {
185                 int numDeleted = _track.compress(inResolution);
186                 if (numDeleted > 0)
187                         _selection.clearAll();
188                 return numDeleted;
189         }
190
191
192         /**
193          * Delete all the duplicate points in the track
194          * @return number of points deleted
195          */
196         public int deleteDuplicates()
197         {
198                 int numDeleted = _track.deleteDuplicates();
199                 if (numDeleted > 0)
200                         _selection.clearAll();
201                 return numDeleted;
202         }
203
204
205         /**
206          * Clone the selected range of data points
207          * @return shallow copy of DataPoint objects
208          */
209         public DataPoint[] cloneSelectedRange()
210         {
211                 return _track.cloneRange(_selection.getStart(), _selection.getEnd());
212         }
213
214
215         /**
216          * Interpolate extra points between two selected ones
217          * @param inStartIndex start index of interpolation
218          * @param inNumPoints num points to insert
219          * @return true if successful
220          */
221         public boolean interpolate(int inNumPoints)
222         {
223                 boolean success = _track.interpolate(_selection.getStart(), inNumPoints);
224                 if (success)
225                         _selection.selectRangeEnd(_selection.getEnd() + inNumPoints);
226                 return success;
227         }
228
229
230         /**
231          * Select the given DataPoint
232          * @param inPoint DataPoint object to select
233          */
234         public void selectPoint(DataPoint inPoint)
235         {
236                 // get the index of the given Point
237                 int index = _track.getPointIndex(inPoint);
238                 // give to selection
239                 _selection.selectPoint(index);
240         }
241 }