1 package tim.prune.function.distance;
3 import tim.prune.Config;
4 import tim.prune.I18nManager;
5 import tim.prune.data.DataPoint;
6 import tim.prune.data.Distance;
9 * Class to hold the table model for the distances table
11 public class DistanceTableModel extends GenericTableModel
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;
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;
25 * @return column count
27 public int getColumnCount()
33 * @param inRowIndex row index
34 * @param inColumnIndex column index
37 public Object getValueAt(int inRowIndex, int inColumnIndex)
39 if (inColumnIndex == 0) {return getPointName(inRowIndex);}
40 if (_distances == null) {return 0.0;}
41 return new Double(_distances[inRowIndex]);
45 * @param inColumnIndex column index
48 public String getColumnName(int inColumnIndex)
50 if (inColumnIndex == 0) {return _toColLabel;}
51 return _distanceLabel;
55 * Get the column class (required for sorting)
56 * @param inColumnIndex column index
57 * @return Class of specified column
59 public Class<?> getColumnClass(int inColumnIndex)
61 if (inColumnIndex == 0) return String.class;
66 * Recalculate the distances
67 * @param inIndex index of selected 'from' point
69 public void recalculate(int inIndex)
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];
79 DataPoint fromPoint = _pointList.get(inIndex);
80 for (int i=0; i<numRows; i++) {
85 double rads = DataPoint.calculateRadiansBetween(fromPoint, _pointList.get(i));
86 _distances[i] = Distance.convertRadiansToDistance(rads, _useMetric?Distance.Units.KILOMETRES:Distance.Units.MILES);
89 // Let table know that it has to refresh data (and maybe refresh column headings too)
90 if (_useMetric == _prevUseMetric) {
91 fireTableDataChanged();
94 fireTableStructureChanged();
96 _prevUseMetric = _useMetric;
100 * @param inMetric true to use metric distances
101 * @return distance label for column heading
103 private static String getDistanceLabel(boolean inMetric)
105 return I18nManager.getText("fieldname.distance") + " (" +
106 I18nManager.getText(inMetric?"units.kilometres.short" : "units.miles.short") + ")";