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