]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/gpsies/TrackListModel.java
Update build scripts
[GpsPrune.git] / src / tim / prune / function / gpsies / TrackListModel.java
1 package tim.prune.function.gpsies;
2
3 import java.text.NumberFormat;
4 import java.util.ArrayList;
5 import java.util.Collections;
6
7 import javax.swing.table.AbstractTableModel;
8
9 import tim.prune.I18nManager;
10 import tim.prune.config.Config;
11 import tim.prune.data.Unit;
12 import tim.prune.function.search.SearchResult;
13
14 /**
15  * Model for list of tracks from a search result (eg gpsies.com, geonames, overpass)
16  */
17 public class TrackListModel extends AbstractTableModel
18 {
19         /** List of tracks */
20         private ArrayList<SearchResult> _trackList = null;
21         /** Column heading for track name */
22         private String _nameColLabel = null;
23         /** Column heading for length */
24         private String _lengthColLabel = null;
25         /** Number of columns */
26         private int _numColumns = 2;
27         /** Normally this model shows distances / lengths, except when this flag is true */
28         private boolean _showPointTypes = false;
29         /** Formatter for distances */
30         private NumberFormat _distanceFormatter = NumberFormat.getInstance();
31
32         /**
33          * Constructor
34          * @param inColumn1Key key for first column
35          * @param inColumn2Key key for second column
36          */
37         public TrackListModel(String inColumn1Key, String inColumn2Key)
38         {
39                 _nameColLabel = I18nManager.getText(inColumn1Key);
40                 if (inColumn2Key != null) {
41                         _lengthColLabel = I18nManager.getText(inColumn2Key);
42                 }
43                 _numColumns = (_lengthColLabel != null?2:1);
44                 _distanceFormatter.setMaximumFractionDigits(1);
45         }
46
47         /**
48          * @return column count
49          */
50         public int getColumnCount()
51         {
52                 return _numColumns;
53         }
54
55         /**
56          * @return number of rows
57          */
58         public int getRowCount()
59         {
60                 if (_trackList == null) return 0;
61                 return _trackList.size();
62         }
63
64         /** @return true if there are no rows */
65         public boolean isEmpty()
66         {
67                 return getRowCount() == 0;
68         }
69
70         /**
71          * @param inColNum column number
72          * @return column label for given column
73          */
74         public String getColumnName(int inColNum)
75         {
76                 if (inColNum == 0) {return _nameColLabel;}
77                 return _lengthColLabel;
78         }
79
80         /**
81          * @param inShowTypes true to show point types, false for distances
82          */
83         public void setShowPointTypes(boolean inShowTypes)
84         {
85                 _showPointTypes = inShowTypes;
86         }
87
88         /**
89          * @param inRowNum row number
90          * @param inColNum column number
91          * @return cell entry at given row and column
92          */
93         public Object getValueAt(int inRowNum, int inColNum)
94         {
95                 SearchResult track = _trackList.get(inRowNum);
96                 if (inColNum == 0) {
97                         return track.getTrackName();
98                 }
99                 if (_showPointTypes)
100                 {
101                         return track.getPointType();
102                 }
103                 double lengthM = track.getLength();
104                 // convert to current distance units
105                 Unit distUnit = Config.getUnitSet().getDistanceUnit();
106                 double length = lengthM * distUnit.getMultFactorFromStd();
107                 // Make text
108                 return _distanceFormatter.format(length) + " " + I18nManager.getText(distUnit.getShortnameKey());
109         }
110
111         /**
112          * Add a list of tracks to this model
113          * @param inList list of tracks to add
114          */
115         public void addTracks(ArrayList<SearchResult> inList)
116         {
117                 addTracks(inList, false);
118         }
119
120         /**
121          * Add a list of tracks to this model and optionally sort them
122          * @param inList list of tracks to add
123          * @param inSort true to sort results after adding
124          */
125         public void addTracks(ArrayList<SearchResult> inList, boolean inSort)
126         {
127                 if (_trackList == null) {_trackList = new ArrayList<SearchResult>();}
128                 final int prevCount = _trackList.size();
129                 if (inList != null && inList.size() > 0)
130                 {
131                         _trackList.addAll(inList);
132                         if (inSort) {
133                                 Collections.sort(_trackList);
134                         }
135                 }
136                 final int updatedCount = _trackList.size();
137                 if (prevCount <= 0)
138                         fireTableDataChanged();
139                 else
140                         fireTableRowsInserted(prevCount, updatedCount-1);
141         }
142
143         /**
144          * @param inRowNum row number from 0
145          * @return track object for this row
146          */
147         public SearchResult getTrack(int inRowNum)
148         {
149                 return _trackList.get(inRowNum);
150         }
151
152         /**
153          * Clear the list of tracks
154          */
155         public void clear()
156         {
157                 _trackList = null;
158         }
159 }