1 package tim.prune.correlate;
3 import java.text.NumberFormat;
4 import java.util.ArrayList;
5 import javax.swing.table.AbstractTableModel;
6 import tim.prune.I18nManager;
7 import tim.prune.data.Distance;
10 * Class to act as table model for the photo preview table
12 public class PhotoPreviewTableModel extends AbstractTableModel
14 /** ArrayList containing TableRow objects */
15 private ArrayList<PhotoPreviewTableRow> _list = new ArrayList<PhotoPreviewTableRow>();
17 private Distance.Units _distanceUnits = Distance.Units.KILOMETRES;
18 /** Number formatter */
19 private static final NumberFormat FORMAT_ONE_DP = NumberFormat.getNumberInstance();
22 /** Static block to initialise the one d.p. formatter */
25 FORMAT_ONE_DP.setMaximumFractionDigits(1);
26 FORMAT_ONE_DP.setMinimumFractionDigits(1);
31 * @return the column count, always 5
33 public int getColumnCount()
40 * Get the name of the column
41 * @param inColNum column number
44 public String getColumnName(int inColNum)
46 if (inColNum == 0) return I18nManager.getText("dialog.correlate.photoselect.photoname");
47 else if (inColNum == 1) return I18nManager.getText("fieldname.timestamp");
48 else if (inColNum == 2) return I18nManager.getText("dialog.correlate.photoselect.timediff");
49 else if (inColNum == 3) return I18nManager.getText("fieldname.distance");
50 return I18nManager.getText("dialog.correlate.options.correlate");
55 * @return the row count
57 public int getRowCount()
64 * Get the selected row from the table
65 * @param inRowIndex row index
66 * @return table row object
68 public PhotoPreviewTableRow getRow(int inRowIndex)
70 return _list.get(inRowIndex);
75 * Get the value of the specified cell
76 * @param inRowIndex row index
77 * @param inColumnIndex column index
78 * @return value of specified cell
80 public Object getValueAt(int inRowIndex, int inColumnIndex)
82 PhotoPreviewTableRow row = _list.get(inRowIndex);
83 if (inColumnIndex == 0) return row.getPhoto().getFile().getName();
84 else if (inColumnIndex == 1) {
85 return row.getPhoto().getTimestamp().getText();
87 else if (inColumnIndex == 2) {
88 if (row.getPointPair().isValid()) {
89 return row.getTimeDiff().getDescription();
93 else if (inColumnIndex == 3) {
94 if (row.getPointPair().isValid()) {
95 return FORMAT_ONE_DP.format(row.getDistance(_distanceUnits));
99 return row.getCorrelateFlag();
104 * @param inUnits the distance units to use
106 public void setDistanceUnits(Distance.Units inUnits)
108 _distanceUnits = inUnits;
122 * Add a photo to the list
123 * @param inRow row to add
125 public void addPhotoRow(PhotoPreviewTableRow inRow)
132 * Get the class of objects in the given column
133 * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
135 public Class<?> getColumnClass(int inColumnIndex)
137 if (inColumnIndex == 4) {return Boolean.class;}
138 return super.getColumnClass(inColumnIndex);
143 * Get whether the given cell is editable
144 * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
146 public boolean isCellEditable(int inRowIndex, int inColumnIndex)
148 if (inColumnIndex == 4) {return true;}
149 return super.isCellEditable(inRowIndex, inColumnIndex);
154 * @return true if any of the correlate flags are on
156 public boolean hasPhotosSelected()
158 for (int i=0; i<getRowCount(); i++)
160 if (getRow(i).getCorrelateFlag().booleanValue())
171 * Set the value at the given table cell
172 * @see javax.swing.table.AbstractTableModel#setValueAt(java.lang.Object, int, int)
174 public void setValueAt(Object inValue, int inRowIndex, int inColumnIndex)
176 // can only edit the correlate column
177 if (inColumnIndex == 4)
179 PhotoPreviewTableRow row = getRow(inRowIndex);
180 // Don't allow setting of photos which can't be correlated
181 if (row.getPointPair().isValid())
183 row.setCorrelateFlag(((Boolean) inValue).booleanValue());