3 import java.util.ArrayList;
4 import javax.swing.AbstractListModel;
6 import tim.prune.data.DataPoint;
7 import tim.prune.data.Track;
10 * Class to deal with the matching of waypoint names
11 * and the representation in a list
13 public class WaypointNameMatcher extends AbstractListModel<String>
15 private ArrayList<DataPoint> _waypoints = null;
16 private int _numPoints = 0;
17 private String[] _waypointNames = null;
18 private ArrayList<DataPoint> _matches = null;
22 * Initialisation giving Track object
23 * @param inTrack Track object
25 public void init(Track inTrack)
27 // Get list of waypoints from track
28 _waypoints = new ArrayList<DataPoint>();
29 inTrack.getWaypoints(_waypoints);
30 // Initialise match flags and waypoint names
31 _numPoints = _waypoints.size();
32 _waypointNames = new String[_numPoints];
33 for (int i=0; i<_numPoints; i++) {
34 _waypointNames[i] = _waypoints.get(i).getWaypointName().toLowerCase();
36 _matches = new ArrayList<DataPoint>();
41 * Search for the given term and collect the matches
42 * @param inSearch string to search for
44 public void findMatches(String inSearch)
48 // Convert search to lower case to match name array
50 if (inSearch != null && !inSearch.equals("")) {
51 search = inSearch.toLowerCase();
53 // Loop through waypoint names
54 for (int i=0; i<_numPoints; i++)
56 if (search == null || _waypointNames[i].indexOf(search) >= 0)
58 _matches.add(_waypoints.get(i));
65 * @see javax.swing.ListModel#getSize()
69 if (_numPoints == 0) return 0;
70 return _matches.size();
74 * @see javax.swing.ListModel#getElementAt(int)
76 public String getElementAt(int inIndex)
78 return _matches.get(inIndex).getWaypointName();
82 * Get the waypoint at the given index
83 * @param inIndex index number, starting at 0
84 * @return DataPoint object
86 public DataPoint getWaypoint(int inIndex)
88 return _matches.get(inIndex);
92 * Fire event to notify that contents have changed
94 public void fireChanged()
96 this.fireContentsChanged(this, 0, getSize()-1);