]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/correlate/MediaPreviewTableModel.java
Version 19, May 2018
[GpsPrune.git] / tim / prune / correlate / MediaPreviewTableModel.java
1 package tim.prune.correlate;
2
3 import java.util.ArrayList;
4 import java.util.TimeZone;
5
6 import javax.swing.table.AbstractTableModel;
7
8 import tim.prune.I18nManager;
9 import tim.prune.config.TimezoneHelper;
10 import tim.prune.data.Unit;
11 import tim.prune.data.UnitSetLibrary;
12 import tim.prune.gui.DisplayUtils;
13
14 /**
15  * Class to act as the table model for the correlation preview table
16  */
17 public class MediaPreviewTableModel extends AbstractTableModel
18 {
19         /** Text for first column heading */
20         private String _firstColumnHeading = null;
21         /** ArrayList containing TableRow objects */
22         private ArrayList<MediaPreviewTableRow> _list = new ArrayList<MediaPreviewTableRow>();
23         /** Distance units */
24         private Unit _distanceUnits = UnitSetLibrary.UNITS_KILOMETRES;
25         /** Current timezone */
26         private TimeZone _timezone = null;
27
28
29         /**
30          * Constructor
31          * @param inFirstColumnKey key for first column heading
32          */
33         public MediaPreviewTableModel(String inFirstColumnKey)
34         {
35                 _firstColumnHeading = I18nManager.getText(inFirstColumnKey);
36                 _timezone = TimezoneHelper.getSelectedTimezone();
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                         if (row.getMedia().hasTimestamp()) {
93                                 return row.getMedia().getTimestamp().getText(_timezone);
94                         }
95                         return ""; // media doesn't have a timestamp
96                 }
97                 else if (inColumnIndex == 2) {
98                         if (row.getPointPair().isValid()) {
99                                 return row.getTimeDiff().getDescription();
100                         }
101                         return "";
102                 }
103                 else if (inColumnIndex == 3) {
104                         if (row.getPointPair().isValid()) {
105                                 return DisplayUtils.formatOneDp(row.getDistance(_distanceUnits));
106                         }
107                         return "";
108                 }
109                 return row.getCorrelateFlag();
110         }
111
112
113         /**
114          * @param inUnits the distance units to use
115          */
116         public void setDistanceUnits(Unit inUnits)
117         {
118                 _distanceUnits = inUnits;
119         }
120
121
122         /**
123          * Clear the list
124          */
125         public void reset()
126         {
127                 _list.clear();
128         }
129
130
131         /**
132          * Add a row to the list
133          * @param inRow row to add
134          */
135         public void addRow(MediaPreviewTableRow inRow)
136         {
137                 _list.add(inRow);
138         }
139
140
141         /**
142          * Get the class of objects in the given column
143          * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
144          */
145         public Class<?> getColumnClass(int inColumnIndex)
146         {
147                 if (inColumnIndex == 4) {return Boolean.class;}
148                 return super.getColumnClass(inColumnIndex);
149         }
150
151
152         /**
153          * Get whether the given cell is editable
154          * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
155          */
156         public boolean isCellEditable(int inRowIndex, int inColumnIndex)
157         {
158                 if (inColumnIndex == 4) {return true;}
159                 return super.isCellEditable(inRowIndex, inColumnIndex);
160         }
161
162
163         /**
164          * @return true if any of the correlate flags are on
165          */
166         public boolean hasAnySelected()
167         {
168                 for (int i=0; i<getRowCount(); i++)
169                 {
170                         if (getRow(i).getCorrelateFlag().booleanValue()) {
171                                 return true;
172                         }
173                 }
174                 // None switched on
175                 return false;
176         }
177
178
179         /**
180          * Set the value at the given table cell
181          * @see javax.swing.table.AbstractTableModel#setValueAt(java.lang.Object, int, int)
182          */
183         public void setValueAt(Object inValue, int inRowIndex, int inColumnIndex)
184         {
185                 // can only edit the correlate column
186                 if (inColumnIndex == 4)
187                 {
188                         MediaPreviewTableRow row = getRow(inRowIndex);
189                         // Don't allow setting of items which can't be correlated
190                         if (row.getPointPair().isValid()) {
191                                 row.setCorrelateFlag(((Boolean) inValue).booleanValue());
192                         }
193                 }
194         }
195 }