]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/correlate/PhotoPreviewTableModel.java
Version 7, February 2009
[GpsPrune.git] / tim / prune / correlate / PhotoPreviewTableModel.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 table model for the photo preview table
11  */
12 public class PhotoPreviewTableModel extends AbstractTableModel
13 {
14         /** ArrayList containing TableRow objects */
15         private ArrayList<PhotoPreviewTableRow> _list = new ArrayList<PhotoPreviewTableRow>();
16         /** Distance units */
17         private Distance.Units _distanceUnits = Distance.Units.KILOMETRES;
18         /** Number formatter */
19         private static final NumberFormat FORMAT_ONE_DP = NumberFormat.getNumberInstance();
20
21
22         /** Static block to initialise the one d.p. formatter */
23         static
24         {
25                 FORMAT_ONE_DP.setMaximumFractionDigits(1);
26                 FORMAT_ONE_DP.setMinimumFractionDigits(1);
27         }
28
29
30         /**
31          * @return the column count, always 5
32          */
33         public int getColumnCount()
34         {
35                 return 5;
36         }
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 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");
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 PhotoPreviewTableRow 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                 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();
86                 }
87                 else if (inColumnIndex == 2) {
88                         if (row.getPointPair().isValid()) {
89                                 return row.getTimeDiff().getDescription();
90                         }
91                         return "";
92                 }
93                 else if (inColumnIndex == 3) {
94                         if (row.getPointPair().isValid()) {
95                                 return FORMAT_ONE_DP.format(row.getDistance(_distanceUnits));
96                         }
97                         return "";
98                 }
99                 return row.getCorrelateFlag();
100         }
101
102
103         /**
104          * @param inUnits the distance units to use
105          */
106         public void setDistanceUnits(Distance.Units inUnits)
107         {
108                 _distanceUnits = inUnits;
109         }
110
111
112         /**
113          * Clear the list
114          */
115         public void reset()
116         {
117                 _list.clear();
118         }
119
120
121         /**
122          * Add a photo to the list
123          * @param inRow row to add
124          */
125         public void addPhotoRow(PhotoPreviewTableRow inRow)
126         {
127                 _list.add(inRow);
128         }
129
130
131         /**
132          * Get the class of objects in the given column
133          * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
134          */
135         public Class<?> getColumnClass(int inColumnIndex)
136         {
137                 if (inColumnIndex == 4) {return Boolean.class;}
138                 return super.getColumnClass(inColumnIndex);
139         }
140
141
142         /**
143          * Get whether the given cell is editable
144          * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
145          */
146         public boolean isCellEditable(int inRowIndex, int inColumnIndex)
147         {
148                 if (inColumnIndex == 4) {return true;}
149                 return super.isCellEditable(inRowIndex, inColumnIndex);
150         }
151
152
153         /**
154          * @return true if any of the correlate flags are on
155          */
156         public boolean hasPhotosSelected()
157         {
158                 for (int i=0; i<getRowCount(); i++)
159                 {
160                         if (getRow(i).getCorrelateFlag().booleanValue())
161                         {
162                                 return true;
163                         }
164                 }
165                 // None switched on
166                 return false;
167         }
168
169
170         /**
171          * Set the value at the given table cell
172          * @see javax.swing.table.AbstractTableModel#setValueAt(java.lang.Object, int, int)
173          */
174         public void setValueAt(Object inValue, int inRowIndex, int inColumnIndex)
175         {
176                 // can only edit the correlate column
177                 if (inColumnIndex == 4)
178                 {
179                         PhotoPreviewTableRow row = getRow(inRowIndex);
180                         // Don't allow setting of photos which can't be correlated
181                         if (row.getPointPair().isValid())
182                         {
183                                 row.setCorrelateFlag(((Boolean) inValue).booleanValue());
184                         }
185                 }
186         }
187 }