]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/deletebydate/DeletionTableModel.java
Version 17, September 2014
[GpsPrune.git] / tim / prune / function / deletebydate / DeletionTableModel.java
1 package tim.prune.function.deletebydate;
2
3 import java.text.DateFormat;
4 import javax.swing.table.AbstractTableModel;
5 import tim.prune.I18nManager;
6
7 /**
8  * Table model for selecting which dates to delete
9  */
10 public class DeletionTableModel extends AbstractTableModel
11 {
12         /** info list, one for each row of table */
13         private DateInfoList _infoList = null;
14
15         /** Formatter, determining how dates appear in the table */
16         private static final DateFormat DEFAULT_DATE_FORMAT = DateFormat.getDateInstance();
17         /** Column heading for date */
18         private static final String COLUMN_HEADING_DATE = I18nManager.getText("fieldname.date");
19         /** Column heading for number of points */
20         private static final String COLUMN_HEADING_NUMPOINTS = I18nManager.getText("details.track.points");
21         /** Column heading for keep */
22         private static final String COLUMN_HEADING_KEEP = I18nManager.getText("dialog.deletebydate.column.keep");
23         /** Column heading for delete */
24         private static final String COLUMN_HEADING_DELETE = I18nManager.getText("dialog.deletebydate.column.delete");
25
26
27         /**
28          * Constructor
29          * @param inList date info list from function
30          */
31         public DeletionTableModel(DateInfoList inList)
32         {
33                 _infoList = inList;
34         }
35
36         /**
37          * @return column count
38          */
39         public int getColumnCount()
40         {
41                 return 4; // always fixed (date, numpoints, keep, delete)
42         }
43
44         /**
45          * @return row count
46          */
47         public int getRowCount()
48         {
49                 if (_infoList == null) {return 0;} // shouldn't happen
50                 return _infoList.getNumEntries();
51         }
52
53         /**
54          * Get the name of the column
55          * @param inColNum column number
56          * @return column name
57          */
58         public String getColumnName(int inColNum)
59         {
60                 if (inColNum == 0) return COLUMN_HEADING_DATE;
61                 else if (inColNum == 1) return COLUMN_HEADING_NUMPOINTS;
62                 else if (inColNum == 2) return COLUMN_HEADING_KEEP;
63                 else if (inColNum == 3) return COLUMN_HEADING_DELETE;
64                 return "unknown column!";
65         }
66
67         /**
68          * Get the class of objects in the given column
69          * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
70          */
71         public Class<?> getColumnClass(int inColumnIndex)
72         {
73                 if (inColumnIndex == 1) {return Integer.class;}
74                 if (inColumnIndex > 1) {return Boolean.class;}
75                 return super.getColumnClass(inColumnIndex);
76         }
77
78         /**
79          * Get whether the given cell is editable
80          * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
81          */
82         public boolean isCellEditable(int inRowIndex, int inColumnIndex)
83         {
84                 return (inColumnIndex > 1);
85         }
86
87         /**
88          * Set the value at the given table cell
89          * @see javax.swing.table.AbstractTableModel#setValueAt(java.lang.Object, int, int)
90          */
91         public void setValueAt(Object inValue, int inRowIndex, int inColumnIndex)
92         {
93                 // can only edit the keep and delete columns
94                 final boolean isKeep = (inColumnIndex == 2);
95                 final boolean isDelete = (inColumnIndex == 3);
96                 // ignore all events for other columns
97                 if (isKeep || isDelete)
98                 {
99                         try {
100                                 boolean setFlag = ((Boolean) inValue).booleanValue();
101                                 if (setFlag)
102                                 {
103                                         _infoList.getDateInfo(inRowIndex).setDeleteFlag(isDelete);
104                                         // make sure the other cell (keep or delete) on the same row is updated too
105                                         fireTableCellUpdated(inRowIndex, 5 - inColumnIndex);
106                                 }
107                         }
108                         catch (ClassCastException cce) {}
109                 }
110         }
111
112         /**
113          * @return cell contents at the given row, column inded
114          */
115         public Object getValueAt(int inRowIndex, int inColIndex)
116         {
117                 try {
118                         DateInfo info = _infoList.getDateInfo(inRowIndex);
119                         if (info != null)
120                         {
121                                 switch (inColIndex)
122                                 {
123                                         case 0: // date
124                                                 if (info.isDateless()) {
125                                                         return I18nManager.getText("dialog.deletebydate.nodate");
126                                                 }
127                                                 return DEFAULT_DATE_FORMAT.format(info.getDate());
128                                         case 1: // number of points
129                                                 return info.getPointCount();
130                                         case 2: // keep
131                                                 return !info.getDeleteFlag();
132                                         case 3: // delete
133                                                 return info.getDeleteFlag();
134                                 }
135                         }
136                 }
137                 catch (IndexOutOfBoundsException obe) {} // ignore, fallthrough
138                 return null;
139         }
140 }