]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/distance/DistanceTableModel.java
Version 20.4, May 2021
[GpsPrune.git] / src / 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         /** Previous distance units */
21         private Unit _previousDistUnit = null;
22
23         /**
24          * @return column count
25          */
26         public int getColumnCount()
27         {
28                 return 2;
29         }
30
31         /**
32          * @param inRowIndex row index
33          * @param inColumnIndex column index
34          * @return cell value
35          */
36         public Object getValueAt(int inRowIndex, int inColumnIndex)
37         {
38                 if (inColumnIndex == 0) {return getPointName(inRowIndex);}
39                 if (_distances == null) {return 0.0;}
40                 return Double.valueOf(_distances[inRowIndex]);
41         }
42
43         /**
44          * @param inColumnIndex column index
45          * @return column name
46          */
47         public String getColumnName(int inColumnIndex)
48         {
49                 if (inColumnIndex == 0) {return _toColLabel;}
50                 return _distanceLabel;
51         }
52
53         /**
54          * Get the column class (required for sorting)
55          * @param inColumnIndex column index
56          * @return Class of specified column
57          */
58         public Class<?> getColumnClass(int inColumnIndex)
59         {
60                 if (inColumnIndex == 0) return String.class;
61                 return Double.class;
62         }
63
64         /**
65          * Recalculate the distances
66          * @param inIndex index of selected 'from' point
67          */
68         public void recalculate(int inIndex)
69         {
70                 // Which units to use?
71                 Unit distUnit = Config.getUnitSet().getDistanceUnit();
72                 _distanceLabel = I18nManager.getText("fieldname.distance") + " (" +
73                         I18nManager.getText(distUnit.getShortnameKey()) + ")";
74                 final boolean distUnitsChanged = (distUnit != _previousDistUnit);
75                 _previousDistUnit = distUnit;
76
77                 // Initialize array of distances
78                 int numRows = getRowCount();
79                 if (_distances == null || _distances.length != numRows) {
80                         _distances = new double[numRows];
81                 }
82                 DataPoint fromPoint = _pointList.get(inIndex);
83                 for (int i=0; i<numRows; i++) {
84                         if (i == inIndex) {
85                                 _distances[i] = 0.0;
86                         }
87                         else {
88                                 double rads = DataPoint.calculateRadiansBetween(fromPoint, _pointList.get(i));
89                                 _distances[i] = Distance.convertRadiansToDistance(rads);
90                         }
91                 }
92                 // Let table know that it has to refresh data, and maybe the whole table too
93                 if (distUnitsChanged) {
94                         fireTableStructureChanged();
95                 }
96                 else {
97                         fireTableDataChanged();
98                 }
99         }
100 }