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