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