]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/gpsies/TrackListModel.java
Version 18.1, September 2015
[GpsPrune.git] / 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 gpsies.com
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         /** Formatter for distances */
28         private NumberFormat _distanceFormatter = NumberFormat.getInstance();
29
30         /**
31          * Constructor
32          * @param inColumn1Key key for first column
33          * @param inColumn2Key key for second column
34          */
35         public TrackListModel(String inColumn1Key, String inColumn2Key)
36         {
37                 _nameColLabel = I18nManager.getText(inColumn1Key);
38                 if (inColumn2Key != null) {
39                         _lengthColLabel = I18nManager.getText(inColumn2Key);
40                 }
41                 _numColumns = (_lengthColLabel != null?2:1);
42                 _distanceFormatter.setMaximumFractionDigits(1);
43         }
44
45         /**
46          * @return column count
47          */
48         public int getColumnCount()
49         {
50                 return _numColumns;
51         }
52
53         /**
54          * @return number of rows
55          */
56         public int getRowCount()
57         {
58                 if (_trackList == null) return 0;
59                 return _trackList.size();
60         }
61
62         /** @return true if there are no rows */
63         public boolean isEmpty()
64         {
65                 return getRowCount() == 0;
66         }
67
68         /**
69          * @param inColNum column number
70          * @return column label for given column
71          */
72         public String getColumnName(int inColNum)
73         {
74                 if (inColNum == 0) {return _nameColLabel;}
75                 return _lengthColLabel;
76         }
77
78         /**
79          * @param inRowNum row number
80          * @param inColNum column number
81          * @return cell entry at given row and column
82          */
83         public Object getValueAt(int inRowNum, int inColNum)
84         {
85                 SearchResult track = _trackList.get(inRowNum);
86                 if (inColNum == 0) {return track.getTrackName();}
87                 double lengthM = track.getLength();
88                 // convert to current distance units
89                 Unit distUnit = Config.getUnitSet().getDistanceUnit();
90                 double length = lengthM * distUnit.getMultFactorFromStd();
91                 // Make text
92                 return _distanceFormatter.format(length) + " " + I18nManager.getText(distUnit.getShortnameKey());
93         }
94
95         /**
96          * Add a list of tracks to this model
97          * @param inList list of tracks to add
98          */
99         public void addTracks(ArrayList<SearchResult> inList)
100         {
101                 addTracks(inList, false);
102         }
103
104         /**
105          * Add a list of tracks to this model and optionally sort them
106          * @param inList list of tracks to add
107          * @param inSort true to sort results after adding
108          */
109         public void addTracks(ArrayList<SearchResult> inList, boolean inSort)
110         {
111                 if (_trackList == null) {_trackList = new ArrayList<SearchResult>();}
112                 final int prevCount = _trackList.size();
113                 if (inList != null && inList.size() > 0)
114                 {
115                         _trackList.addAll(inList);
116                         if (inSort) {
117                                 Collections.sort(_trackList);
118                         }
119                 }
120                 final int updatedCount = _trackList.size();
121                 if (prevCount <= 0)
122                         fireTableDataChanged();
123                 else
124                         fireTableRowsInserted(prevCount, updatedCount-1);
125         }
126
127         /**
128          * @param inRowNum row number from 0
129          * @return track object for this row
130          */
131         public SearchResult getTrack(int inRowNum)
132         {
133                 return _trackList.get(inRowNum);
134         }
135
136         /**
137          * Clear the list of tracks
138          */
139         public void clear()
140         {
141                 _trackList = null;
142         }
143 }