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