]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/distance/DistanceTableModel.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / function / distance / DistanceTableModel.java
1 package tim.prune.function.distance;
2
3 import tim.prune.I18nManager;
4 import tim.prune.config.Config;
5 import tim.prune.data.DataPoint;
6 import tim.prune.data.Distance;
7 import tim.prune.data.Unit;
8
9 /**
10  * Class to hold the table model for the distances table
11  */
12 public class DistanceTableModel extends GenericTableModel
13 {
14         /** Distances */
15         private double[] _distances = null;
16         /** Column heading */
17         private static final String _toColLabel = I18nManager.getText("dialog.distances.column.to");
18         /** Column heading (depends on metric/imperial settings) */
19         private String _distanceLabel = null;
20
21         /**
22          * @return column count
23          */
24         public int getColumnCount()
25         {
26                 return 2;
27         }
28
29         /**
30          * @param inRowIndex row index
31          * @param inColumnIndex column index
32          * @return cell value
33          */
34         public Object getValueAt(int inRowIndex, int inColumnIndex)
35         {
36                 if (inColumnIndex == 0) {return getPointName(inRowIndex);}
37                 if (_distances == null) {return 0.0;}
38                 return new Double(_distances[inRowIndex]);
39         }
40
41         /**
42          * @param inColumnIndex column index
43          * @return column name
44          */
45         public String getColumnName(int inColumnIndex)
46         {
47                 if (inColumnIndex == 0) {return _toColLabel;}
48                 return _distanceLabel;
49         }
50
51         /**
52          * Get the column class (required for sorting)
53          * @param inColumnIndex column index
54          * @return Class of specified column
55          */
56         public Class<?> getColumnClass(int inColumnIndex)
57         {
58                 if (inColumnIndex == 0) return String.class;
59                 return Double.class;
60         }
61
62         /**
63          * Recalculate the distances
64          * @param inIndex index of selected 'from' point
65          */
66         public void recalculate(int inIndex)
67         {
68                 // Which units to use?
69                 Unit distUnit = Config.getUnitSet().getDistanceUnit();
70                 _distanceLabel = I18nManager.getText("fieldname.distance") + " (" +
71                         I18nManager.getText(distUnit.getShortnameKey()) + ")";
72                 // Initialize array of distances
73                 int numRows = getRowCount();
74                 if (_distances == null || _distances.length != numRows) {
75                         _distances = new double[numRows];
76                 }
77                 DataPoint fromPoint = _pointList.get(inIndex);
78                 for (int i=0; i<numRows; i++) {
79                         if (i == inIndex) {
80                                 _distances[i] = 0.0;
81                         }
82                         else {
83                                 double rads = DataPoint.calculateRadiansBetween(fromPoint, _pointList.get(i));
84                                 _distances[i] = Distance.convertRadiansToDistance(rads);
85                         }
86                 }
87                 // Let table know that it has to refresh data (and might as well refresh column headings too)
88                 fireTableStructureChanged();
89         }
90 }