]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/gpsies/TrackListModel.java
Version 14, October 2012
[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
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                 // convert to current distance units
81                 Unit distUnit = Config.getUnitSet().getDistanceUnit();
82                 double length = lengthM * distUnit.getMultFactorFromStd();
83                 // Make text
84                 return _distanceFormatter.format(length) + " " + I18nManager.getText(distUnit.getShortnameKey());
85         }
86
87         /**
88          * Add a list of tracks to this model
89          * @param inList list of tracks to add
90          */
91         public void addTracks(ArrayList<GpsiesTrack> inList)
92         {
93                 if (_trackList == null) {_trackList = new ArrayList<GpsiesTrack>();}
94                 final int prevCount = _trackList.size();
95                 if (inList != null && inList.size() > 0) {
96                         _trackList.addAll(inList);
97                 }
98                 final int updatedCount = _trackList.size();
99                 if (prevCount <= 0)
100                         fireTableDataChanged();
101                 else
102                         fireTableRowsInserted(prevCount, updatedCount-1);
103         }
104
105         /**
106          * @param inRowNum row number from 0
107          * @return track object for this row
108          */
109         public GpsiesTrack getTrack(int inRowNum)
110         {
111                 return _trackList.get(inRowNum);
112         }
113
114         /**
115          * Clear the list of tracks
116          */
117         public void clear()
118         {
119                 _trackList = null;
120         }
121 }