File: TrackListModel.java

package info (click to toggle)
gpsprune 26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,824 kB
  • sloc: java: 52,154; sh: 25; makefile: 21; python: 15
file content (156 lines) | stat: -rw-r--r-- 3,907 bytes parent folder | download | duplicates (4)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package tim.prune.function.search;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.table.AbstractTableModel;

import tim.prune.I18nManager;
import tim.prune.data.Unit;


/**
 * Model for list of tracks from a search result (eg geonames, overpass)
 */
public class TrackListModel extends AbstractTableModel
{
	/** Unit to use for distances */
	private final Unit _distanceUnit;
	/** List of tracks */
	private ArrayList<SearchResult> _trackList = null;
	/** Column heading for track name */
	private final String _nameColLabel;
	/** Column heading for length */
	private String _lengthColLabel = null;
	/** Number of columns */
	private final int _numColumns;
	/** Normally this model shows distances / lengths, except when this flag is true */
	private boolean _showPointTypes = false;
	/** Formatter for distances */
	private final NumberFormat _distanceFormatter = NumberFormat.getInstance();

	/**
	 * Constructor
	 * @param inColumn1Key key for first column
	 * @param inColumn2Key key for second column
	 * @param inDistanceUnit unit to use for distances
	 */
	public TrackListModel(String inColumn1Key, String inColumn2Key, Unit inDistanceUnit)
	{
		_distanceUnit = inDistanceUnit;
		_nameColLabel = I18nManager.getText(inColumn1Key);
		if (inColumn2Key != null) {
			_lengthColLabel = I18nManager.getText(inColumn2Key);
		}
		_numColumns = (_lengthColLabel != null ? 2 : 1);
		_distanceFormatter.setMaximumFractionDigits(1);
	}

	/**
	 * @return column count
	 */
	public int getColumnCount() {
		return _numColumns;
	}

	/**
	 * @return number of rows
	 */
	public int getRowCount()
	{
		if (_trackList == null) return 0;
		return _trackList.size();
	}

	/** @return true if there are no rows */
	public boolean isEmpty() {
		return getRowCount() == 0;
	}

	/**
	 * @param inColNum column number
	 * @return column label for given column
	 */
	public String getColumnName(int inColNum)
	{
		if (inColNum == 0) {return _nameColLabel;}
		return _lengthColLabel;
	}

	/**
	 * @param inShowTypes true to show point types, false for distances
	 */
	public void setShowPointTypes(boolean inShowTypes) {
		_showPointTypes = inShowTypes;
	}

	/**
	 * @param inRowNum row number
	 * @param inColNum column number
	 * @return cell entry at given row and column
	 */
	public Object getValueAt(int inRowNum, int inColNum)
	{
		SearchResult track = _trackList.get(inRowNum);
		if (inColNum == 0) {
			return track.getTrackName();
		}
		if (_showPointTypes) {
			return track.getPointType();
		}
		double lengthM = track.getLength();
		// convert to current distance units
		double length = lengthM * _distanceUnit.getMultFactorFromStd();
		// Make text
		return _distanceFormatter.format(length) + " " + I18nManager.getText(_distanceUnit.getShortnameKey());
	}

	/**
	 * Add a list of tracks to this model
	 * @param inList list of tracks to add
	 */
	public void addTracks(ArrayList<SearchResult> inList) {
		addTracks(inList, false);
	}

	/**
	 * Add a list of tracks to this model and optionally sort them
	 * @param inList list of tracks to add
	 * @param inSort true to sort results after adding
	 */
	public void addTracks(ArrayList<SearchResult> inList, boolean inSort)
	{
		if (_trackList == null) {_trackList = new ArrayList<>();}
		final int prevCount = _trackList.size();
		if (inList != null && inList.size() > 0)
		{
			_trackList.addAll(inList);
			if (inSort) {
				Collections.sort(_trackList);
			}
		}
		final int updatedCount = _trackList.size();
		if (prevCount <= 0) {
			fireTableDataChanged();
		}
		else {
			fireTableRowsInserted(prevCount, updatedCount-1);
		}
	}

	/**
	 * @param inRowNum row number from 0
	 * @return track object for this row
	 */
	public SearchResult getTrack(int inRowNum) {
		return _trackList.get(inRowNum);
	}

	/**
	 * Clear the list of tracks
	 */
	public void clear() {
		_trackList = null;
	}
}