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