]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/gpsies/TrackListModel.java
274c0b882a3bf51f1ee7c7dfe278646f756ff235
[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         /** @return true if there are no rows */
61         public boolean isEmpty()
62         {
63                 return getRowCount() == 0;
64         }
65
66         /**
67          * @param inColNum column number
68          * @return column label for given column
69          */
70         public String getColumnName(int inColNum)
71         {
72                 if (inColNum == 0) {return _nameColLabel;}
73                 return _lengthColLabel;
74         }
75
76         /**
77          * @param inRowNum row number
78          * @param inColNum column number
79          * @return cell entry at given row and column
80          */
81         public Object getValueAt(int inRowNum, int inColNum)
82         {
83                 GpsiesTrack track = _trackList.get(inRowNum);
84                 if (inColNum == 0) {return track.getTrackName();}
85                 double lengthM = track.getLength();
86                 // convert to current distance units
87                 Unit distUnit = Config.getUnitSet().getDistanceUnit();
88                 double length = lengthM * distUnit.getMultFactorFromStd();
89                 // Make text
90                 return _distanceFormatter.format(length) + " " + I18nManager.getText(distUnit.getShortnameKey());
91         }
92
93         /**
94          * Add a list of tracks to this model
95          * @param inList list of tracks to add
96          */
97         public void addTracks(ArrayList<GpsiesTrack> inList)
98         {
99                 if (_trackList == null) {_trackList = new ArrayList<GpsiesTrack>();}
100                 final int prevCount = _trackList.size();
101                 if (inList != null && inList.size() > 0) {
102                         _trackList.addAll(inList);
103                 }
104                 final int updatedCount = _trackList.size();
105                 if (prevCount <= 0)
106                         fireTableDataChanged();
107                 else
108                         fireTableRowsInserted(prevCount, updatedCount-1);
109         }
110
111         /**
112          * @param inRowNum row number from 0
113          * @return track object for this row
114          */
115         public GpsiesTrack getTrack(int inRowNum)
116         {
117                 return _trackList.get(inRowNum);
118         }
119
120         /**
121          * Clear the list of tracks
122          */
123         public void clear()
124         {
125                 _trackList = null;
126         }
127 }