]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/WaypointNameMatcher.java
db0f827947475e692586c1f9ca01053f7ddff389
[GpsPrune.git] / tim / prune / gui / WaypointNameMatcher.java
1 package tim.prune.gui;
2
3 import java.util.ArrayList;
4 import javax.swing.AbstractListModel;
5
6 import tim.prune.data.DataPoint;
7 import tim.prune.data.Track;
8
9 /**
10  * Class to deal with the matching of waypoint names
11  * and the representation in a list
12  */
13 public class WaypointNameMatcher extends AbstractListModel
14 {
15         private ArrayList<DataPoint> _waypoints = null;
16         private int _numPoints = 0;
17         private String[] _waypointNames = null;
18         private ArrayList<DataPoint> _matches = null;
19
20
21         /**
22          * Initialisation giving Track object
23          * @param inTrack Track object
24          */
25         public void init(Track inTrack)
26         {
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();
35                 }
36                 _matches = new ArrayList<DataPoint>();
37                 findMatches(null);
38         }
39
40         /**
41          * Search for the given term and collect the matches
42          * @param inSearch string to search for
43          */
44         public void findMatches(String inSearch)
45         {
46                 // Reset array
47                 _matches.clear();
48                 // Convert search to lower case to match name array
49                 String search = null;
50                 if (inSearch != null && !inSearch.equals("")) {
51                         search = inSearch.toLowerCase();
52                 }
53                 // Loop through waypoint names
54                 for (int i=0; i<_numPoints; i++)
55                 {
56                         if (search == null || _waypointNames[i].indexOf(search) >= 0)
57                         {
58                                 _matches.add(_waypoints.get(i));
59                         }
60                 }
61                 fireChanged();
62         }
63
64         /**
65          * @see javax.swing.ListModel#getSize()
66          */
67         public int getSize()
68         {
69                 if (_numPoints == 0) return 0;
70                 return _matches.size();
71         }
72
73         /**
74          * @see javax.swing.ListModel#getElementAt(int)
75          */
76         public Object getElementAt(int inIndex)
77         {
78                 return _matches.get(inIndex).getWaypointName();
79         }
80
81         /**
82          * Get the waypoint at the given index
83          * @param inIndex index number, starting at 0
84          * @return DataPoint object
85          */
86         public DataPoint getWaypoint(int inIndex)
87         {
88                 return _matches.get(inIndex);
89         }
90
91         /**
92          * Fire event to notify that contents have changed
93          */
94         public void fireChanged()
95         {
96                 this.fireContentsChanged(this, 0, getSize()-1);
97         }
98 }