]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/correlate/PhotoPreviewTableModel.java
Version 4, January 2008
[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 _list = new ArrayList();
16         /** Distance units */
17         private int _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                 PhotoPreviewTableRow row = (PhotoPreviewTableRow) _list.get(inRowIndex);
71                 return row;
72         }
73
74
75         /**
76          * Get the value of the specified cell
77          * @param inRowIndex row index
78          * @param inColumnIndex column index
79          * @return value of specified cell
80          */
81         public Object getValueAt(int inRowIndex, int inColumnIndex)
82         {
83                 PhotoPreviewTableRow row = (PhotoPreviewTableRow) _list.get(inRowIndex);
84                 if (inColumnIndex == 0) return row.getPhoto().getFile().getName();
85                 else if (inColumnIndex == 1) {
86                         return row.getPhoto().getTimestamp().getText();
87                 }
88                 else if (inColumnIndex == 2) {
89                         if (row.getPointPair().isValid()) {
90                                 return row.getTimeDiff().getDescription();
91                         }
92                         return "";
93                 }
94                 else if (inColumnIndex == 3) {
95                         if (row.getPointPair().isValid()) {
96                                 return FORMAT_ONE_DP.format(row.getDistance(_distanceUnits));
97                         }
98                         return "";
99                 }
100                 return row.getCorrelateFlag();
101         }
102
103
104         /**
105          * @param inUnits the distance units to use
106          */
107         public void setDistanceUnits(int inUnits)
108         {
109                 _distanceUnits = inUnits;
110         }
111
112
113         /**
114          * Clear the list
115          */
116         public void reset()
117         {
118                 _list.clear();
119         }
120
121
122         /**
123          * Add a photo to the list
124          * @param inRow row to add
125          */
126         public void addPhotoRow(PhotoPreviewTableRow inRow)
127         {
128                 _list.add(inRow);
129         }
130
131
132         /**
133          * Get the class of objects in the given column
134          * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
135          */
136         public Class getColumnClass(int inColumnIndex)
137         {
138                 if (inColumnIndex == 4) {return Boolean.class;}
139                 return super.getColumnClass(inColumnIndex);
140         }
141
142
143         /**
144          * Get whether the given cell is editable
145          * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
146          */
147         public boolean isCellEditable(int inRowIndex, int inColumnIndex)
148         {
149                 if (inColumnIndex == 4) {return true;}
150                 return super.isCellEditable(inRowIndex, inColumnIndex);
151         }
152
153
154         /**
155          * @return true if any of the correlate flags are on
156          */
157         public boolean hasPhotosSelected()
158         {
159                 for (int i=0; i<getRowCount(); i++)
160                 {
161                         if (getRow(i).getCorrelateFlag().booleanValue())
162                         {
163                                 return true;
164                         }
165                 }
166                 // None switched on
167                 return false;
168         }
169
170
171         /**
172          * Set the value at the given table cell
173          * @see javax.swing.table.AbstractTableModel#setValueAt(java.lang.Object, int, int)
174          */
175         public void setValueAt(Object inValue, int inRowIndex, int inColumnIndex)
176         {
177                 // can only edit the correlate column
178                 if (inColumnIndex == 4)
179                 {
180                         PhotoPreviewTableRow row = getRow(inRowIndex);
181                         // Don't allow setting of photos which can't be correlated
182                         if (row.getPointPair().isValid())
183                         {
184                                 row.setCorrelateFlag(((Boolean) inValue).booleanValue());
185                         }
186                 }
187         }
188 }