File: WaypointNameMatcher.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (98 lines) | stat: -rw-r--r-- 2,242 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package tim.prune.gui;

import java.util.ArrayList;
import javax.swing.AbstractListModel;

import tim.prune.data.DataPoint;
import tim.prune.data.Track;

/**
 * Class to deal with the matching of waypoint names
 * and the representation in a list
 */
public class WaypointNameMatcher extends AbstractListModel<String>
{
	private ArrayList<DataPoint> _waypoints = null;
	private int _numPoints = 0;
	private String[] _waypointNames = null;
	private ArrayList<DataPoint> _matches = null;


	/**
	 * Initialisation giving Track object
	 * @param inTrack Track object
	 */
	public void init(Track inTrack)
	{
		// Get list of waypoints from track
		_waypoints = new ArrayList<DataPoint>();
		inTrack.getWaypoints(_waypoints);
		// Initialise match flags and waypoint names
		_numPoints = _waypoints.size();
		_waypointNames = new String[_numPoints];
		for (int i=0; i<_numPoints; i++) {
			_waypointNames[i] = _waypoints.get(i).getWaypointName().toLowerCase();
		}
		_matches = new ArrayList<DataPoint>();
		findMatches(null);
	}

	/**
	 * Search for the given term and collect the matches
	 * @param inSearch string to search for
	 */
	public void findMatches(String inSearch)
	{
		// Reset array
		_matches.clear();
		// Convert search to lower case to match name array
		String search = null;
		if (inSearch != null && !inSearch.equals("")) {
			search = inSearch.toLowerCase();
		}
		// Loop through waypoint names
		for (int i=0; i<_numPoints; i++)
		{
			if (search == null || _waypointNames[i].indexOf(search) >= 0)
			{
				_matches.add(_waypoints.get(i));
			}
		}
		fireChanged();
	}

	/**
	 * @see javax.swing.ListModel#getSize()
	 */
	public int getSize()
	{
		if (_numPoints == 0) return 0;
		return _matches.size();
	}

	/**
	 * @see javax.swing.ListModel#getElementAt(int)
	 */
	public String getElementAt(int inIndex)
	{
		return _matches.get(inIndex).getWaypointName();
	}

	/**
	 * Get the waypoint at the given index
	 * @param inIndex index number, starting at 0
	 * @return DataPoint object
	 */
	public DataPoint getWaypoint(int inIndex)
	{
		return _matches.get(inIndex);
	}

	/**
	 * Fire event to notify that contents have changed
	 */
	public void fireChanged()
	{
		this.fireContentsChanged(this, 0, getSize()-1);
	}
}