]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/gpsies/TrackListModel.java
3a7326f647409b131d70964ad48279ac086ab275
[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.Distance;
11
12 /**
13  * Model for list of tracks from gpsies.com
14  */
15 public class TrackListModel extends AbstractTableModel
16 {
17         /** List of tracks */
18         private ArrayList<GpsiesTrack> _trackList = null;
19         /** Column heading for track name */
20         private String _nameColLabel = null;
21         /** Column heading for length */
22         private String _lengthColLabel = null;
23         /** Number of columns */
24         private int _numColumns = 2;
25         /** Formatter for distances */
26         private NumberFormat _distanceFormatter = NumberFormat.getInstance();
27
28         /**
29          * Constructor
30          * @param inColumn1Key key for first column
31          * @param inColumn2Key key for second column
32          */
33         public TrackListModel(String inColumn1Key, String inColumn2Key)
34         {
35                 _nameColLabel = I18nManager.getText(inColumn1Key);
36                 if (inColumn2Key != null) {
37                         _lengthColLabel = I18nManager.getText(inColumn2Key);
38                 }
39                 _numColumns = (_lengthColLabel != null?2:1);
40                 _distanceFormatter.setMaximumFractionDigits(1);
41         }
42
43         /**
44          * @return column count
45          */
46         public int getColumnCount()
47         {
48                 return _numColumns;
49         }
50
51         /**
52          * @return number of rows
53          */
54         public int getRowCount()
55         {
56                 if (_trackList == null) return 0;
57                 return _trackList.size();
58         }
59
60         /**
61          * @param inColNum column number
62          * @return column label for given column
63          */
64         public String getColumnName(int inColNum)
65         {
66                 if (inColNum == 0) {return _nameColLabel;}
67                 return _lengthColLabel;
68         }
69
70         /**
71          * @param inRowNum row number
72          * @param inColNum column number
73          * @return cell entry at given row and column
74          */
75         public Object getValueAt(int inRowNum, int inColNum)
76         {
77                 GpsiesTrack track = _trackList.get(inRowNum);
78                 if (inColNum == 0) {return track.getTrackName();}
79                 double lengthM = track.getLength();
80                 if (Config.getConfigBoolean(Config.KEY_METRIC_UNITS)) {
81                         return _distanceFormatter.format(lengthM / 1000.0) + " " + I18nManager.getText("units.kilometres.short");
82                 }
83                 // must be imperial
84                 return _distanceFormatter.format(Distance.convertMetresToMiles(lengthM))
85                         + " " + I18nManager.getText("units.miles.short");
86         }
87
88         /**
89          * Add a list of tracks to this model
90          * @param inList list of tracks to add
91          */
92         public void addTracks(ArrayList<GpsiesTrack> inList)
93         {
94                 if (_trackList == null) {_trackList = new ArrayList<GpsiesTrack>();}
95                 if (inList != null && inList.size() > 0) {
96                         _trackList.addAll(inList);
97                 }
98                 fireTableDataChanged();
99         }
100
101         /**
102          * @param inRowNum row number from 0
103          * @return track object for this row
104          */
105         public GpsiesTrack getTrack(int inRowNum)
106         {
107                 return _trackList.get(inRowNum);
108         }
109
110         /**
111          * Clear the list of tracks
112          */
113         public void clear()
114         {
115                 _trackList = null;
116         }
117 }