1 package tim.prune.function.distance;
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;
10 * Class to hold the table model for the distances table
12 public class DistanceTableModel extends GenericTableModel
15 private double[] _distances = null;
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;
24 * @return column count
26 public int getColumnCount()
32 * @param inRowIndex row index
33 * @param inColumnIndex column index
36 public Object getValueAt(int inRowIndex, int inColumnIndex)
38 if (inColumnIndex == 0) {return getPointName(inRowIndex);}
39 if (_distances == null) {return 0.0;}
40 return Double.valueOf(_distances[inRowIndex]);
44 * @param inColumnIndex column index
47 public String getColumnName(int inColumnIndex)
49 if (inColumnIndex == 0) {return _toColLabel;}
50 return _distanceLabel;
54 * Get the column class (required for sorting)
55 * @param inColumnIndex column index
56 * @return Class of specified column
58 public Class<?> getColumnClass(int inColumnIndex)
60 if (inColumnIndex == 0) return String.class;
65 * Recalculate the distances
66 * @param inIndex index of selected 'from' point
68 public void recalculate(int inIndex)
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;
77 // Initialize array of distances
78 int numRows = getRowCount();
79 if (_distances == null || _distances.length != numRows) {
80 _distances = new double[numRows];
82 DataPoint fromPoint = _pointList.get(inIndex);
83 for (int i=0; i<numRows; i++) {
88 double rads = DataPoint.calculateRadiansBetween(fromPoint, _pointList.get(i));
89 _distances[i] = Distance.convertRadiansToDistance(rads);
92 // Let table know that it has to refresh data, and maybe the whole table too
93 if (distUnitsChanged) {
94 fireTableStructureChanged();
97 fireTableDataChanged();