]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/correlate/MediaPreviewTableModel.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / correlate / MediaPreviewTableModel.java
1 package tim.prune.correlate;
2
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.Unit;
8 import tim.prune.data.UnitSetLibrary;
9
10 /**
11  * Class to act as the table model for the correlation preview table
12  */
13 public class MediaPreviewTableModel extends AbstractTableModel
14 {
15         /** Text for first column heading */
16         private String _firstColumnHeading = null;
17         /** ArrayList containing TableRow objects */
18         private ArrayList<MediaPreviewTableRow> _list = new ArrayList<MediaPreviewTableRow>();
19         /** Distance units */
20         private Unit _distanceUnits = UnitSetLibrary.UNITS_KILOMETRES;
21         /** Number formatter */
22         private static final NumberFormat FORMAT_ONE_DP = NumberFormat.getNumberInstance();
23
24
25         /** Static block to initialise the one d.p. formatter */
26         static
27         {
28                 FORMAT_ONE_DP.setMaximumFractionDigits(1);
29                 FORMAT_ONE_DP.setMinimumFractionDigits(1);
30         }
31
32         /**
33          * Constructor
34          * @param inFirstColumnKey key for first column heading
35          */
36         public MediaPreviewTableModel(String inFirstColumnKey) {
37                 _firstColumnHeading = I18nManager.getText(inFirstColumnKey);
38         }
39
40         /**
41          * @return the column count, always 5
42          */
43         public int getColumnCount() {
44                 return 5;
45         }
46
47         /**
48          * Get the name of the column
49          * @param inColNum column number
50          * @return column name
51          */
52         public String getColumnName(int inColNum)
53         {
54                 if (inColNum == 0) return _firstColumnHeading;
55                 else if (inColNum == 1) return I18nManager.getText("fieldname.timestamp");
56                 else if (inColNum == 2) return I18nManager.getText("dialog.correlate.select.timediff");
57                 else if (inColNum == 3) return I18nManager.getText("fieldname.distance");
58                 return I18nManager.getText("dialog.correlate.options.correlate");
59         }
60
61
62         /**
63          * @return the row count
64          */
65         public int getRowCount()
66         {
67                 return _list.size();
68         }
69
70
71         /**
72          * Get the selected row from the table
73          * @param inRowIndex row index
74          * @return table row object
75          */
76         public MediaPreviewTableRow getRow(int inRowIndex)
77         {
78                 return _list.get(inRowIndex);
79         }
80
81
82         /**
83          * Get the value of the specified cell
84          * @param inRowIndex row index
85          * @param inColumnIndex column index
86          * @return value of specified cell
87          */
88         public Object getValueAt(int inRowIndex, int inColumnIndex)
89         {
90                 MediaPreviewTableRow row = _list.get(inRowIndex);
91                 if (inColumnIndex == 0) return row.getMedia().getName();
92                 else if (inColumnIndex == 1) {
93                         if (row.getMedia().hasTimestamp()) {
94                                 return row.getMedia().getTimestamp().getText();
95                         }
96                         return ""; // media doesn't have a timestamp
97                 }
98                 else if (inColumnIndex == 2) {
99                         if (row.getPointPair().isValid()) {
100                                 return row.getTimeDiff().getDescription();
101                         }
102                         return "";
103                 }
104                 else if (inColumnIndex == 3) {
105                         if (row.getPointPair().isValid()) {
106                                 return FORMAT_ONE_DP.format(row.getDistance(_distanceUnits));
107                         }
108                         return "";
109                 }
110                 return row.getCorrelateFlag();
111         }
112
113
114         /**
115          * @param inUnits the distance units to use
116          */
117         public void setDistanceUnits(Unit inUnits)
118         {
119                 _distanceUnits = inUnits;
120         }
121
122
123         /**
124          * Clear the list
125          */
126         public void reset()
127         {
128                 _list.clear();
129         }
130
131
132         /**
133          * Add a row to the list
134          * @param inRow row to add
135          */
136         public void addRow(MediaPreviewTableRow inRow)
137         {
138                 _list.add(inRow);
139         }
140
141
142         /**
143          * Get the class of objects in the given column
144          * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
145          */
146         public Class<?> getColumnClass(int inColumnIndex)
147         {
148                 if (inColumnIndex == 4) {return Boolean.class;}
149                 return super.getColumnClass(inColumnIndex);
150         }
151
152
153         /**
154          * Get whether the given cell is editable
155          * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
156          */
157         public boolean isCellEditable(int inRowIndex, int inColumnIndex)
158         {
159                 if (inColumnIndex == 4) {return true;}
160                 return super.isCellEditable(inRowIndex, inColumnIndex);
161         }
162
163
164         /**
165          * @return true if any of the correlate flags are on
166          */
167         public boolean hasAnySelected()
168         {
169                 for (int i=0; i<getRowCount(); i++)
170                 {
171                         if (getRow(i).getCorrelateFlag().booleanValue()) {
172                                 return true;
173                         }
174                 }
175                 // None switched on
176                 return false;
177         }
178
179
180         /**
181          * Set the value at the given table cell
182          * @see javax.swing.table.AbstractTableModel#setValueAt(java.lang.Object, int, int)
183          */
184         public void setValueAt(Object inValue, int inRowIndex, int inColumnIndex)
185         {
186                 // can only edit the correlate column
187                 if (inColumnIndex == 4)
188                 {
189                         MediaPreviewTableRow row = getRow(inRowIndex);
190                         // Don't allow setting of items which can't be correlated
191                         if (row.getPointPair().isValid()) {
192                                 row.setCorrelateFlag(((Boolean) inValue).booleanValue());
193                         }
194                 }
195         }
196 }