]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/distance/DistanceTableModel.java
48ba1722be3bedf224ec28ed14fe362129d8fedf
[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
8 /**
9  * Class to hold the table model for the distances table
10  */
11 public class DistanceTableModel extends GenericTableModel
12 {
13         /** Distances */
14         private double[] _distances = null;
15         /** Metric distances? */
16         private boolean _useMetric = true;
17         /** Previous value of metric flag (to spot changes) */
18         private boolean _prevUseMetric = false;
19         /** Column heading */
20         private static final String _toColLabel = I18nManager.getText("dialog.distances.column.to");
21         /** Column heading (depends on metric/imperial settings) */
22         private String _distanceLabel = null;
23
24         /**
25          * @return column count
26          */
27         public int getColumnCount()
28         {
29                 return 2;
30         }
31
32         /**
33          * @param inRowIndex row index
34          * @param inColumnIndex column index
35          * @return cell value
36          */
37         public Object getValueAt(int inRowIndex, int inColumnIndex)
38         {
39                 if (inColumnIndex == 0) {return getPointName(inRowIndex);}
40                 if (_distances == null) {return 0.0;}
41                 return new Double(_distances[inRowIndex]);
42         }
43
44         /**
45          * @param inColumnIndex column index
46          * @return column name
47          */
48         public String getColumnName(int inColumnIndex)
49         {
50                 if (inColumnIndex == 0) {return _toColLabel;}
51                 return _distanceLabel;
52         }
53
54         /**
55          * Get the column class (required for sorting)
56          * @param inColumnIndex column index
57          * @return Class of specified column
58          */
59         public Class<?> getColumnClass(int inColumnIndex)
60         {
61                 if (inColumnIndex == 0) return String.class;
62                 return Double.class;
63         }
64
65         /**
66          * Recalculate the distances
67          * @param inIndex index of selected 'from' point
68          */
69         public void recalculate(int inIndex)
70         {
71                 // Use metric or not?
72                 _useMetric = Config.getConfigBoolean(Config.KEY_METRIC_UNITS);
73                 _distanceLabel = getDistanceLabel(_useMetric);
74                 // Initialize array of distances
75                 int numRows = getRowCount();
76                 if (_distances == null || _distances.length != numRows) {
77                         _distances = new double[numRows];
78                 }
79                 DataPoint fromPoint = _pointList.get(inIndex);
80                 for (int i=0; i<numRows; i++) {
81                         if (i == inIndex) {
82                                 _distances[i] = 0.0;
83                         }
84                         else {
85                                 double rads = DataPoint.calculateRadiansBetween(fromPoint, _pointList.get(i));
86                                 _distances[i] = Distance.convertRadiansToDistance(rads, _useMetric?Distance.Units.KILOMETRES:Distance.Units.MILES);
87                         }
88                 }
89                 // Let table know that it has to refresh data (and maybe refresh column headings too)
90                 if (_useMetric == _prevUseMetric) {
91                         fireTableDataChanged();
92                 }
93                 else {
94                         fireTableStructureChanged();
95                 }
96                 _prevUseMetric = _useMetric;
97         }
98
99         /**
100          * @param inMetric true to use metric distances
101          * @return distance label for column heading
102          */
103         private static String getDistanceLabel(boolean inMetric)
104         {
105                 return I18nManager.getText("fieldname.distance") + " (" +
106                         I18nManager.getText(inMetric?"units.kilometres.short" : "units.miles.short") + ")";
107         }
108 }