1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.correction;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.Arrays;
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | import javax.swing.table.AbstractTableModel;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.correction.Correction;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Abstract correction table model.
|
---|
15 | * @param <C> type of correction
|
---|
16 | */
|
---|
17 | public abstract class CorrectionTableModel<C extends Correction> extends AbstractTableModel {
|
---|
18 |
|
---|
19 | private final transient List<C> corrections;
|
---|
20 | private boolean[] apply;
|
---|
21 | private final int applyColumn;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Constructs a new {@code CorrectionTableModel}.
|
---|
25 | * @param corrections list of corrections
|
---|
26 | */
|
---|
27 | public CorrectionTableModel(List<C> corrections) {
|
---|
28 | this.corrections = corrections;
|
---|
29 | apply = new boolean[this.corrections.size()];
|
---|
30 | Arrays.fill(apply, true);
|
---|
31 | applyColumn = getColumnCount() - 1;
|
---|
32 | }
|
---|
33 |
|
---|
34 | protected abstract boolean isBoldCell(int row, int column);
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Returns the column name for columns other than "Apply".
|
---|
38 | * @param colIndex column index
|
---|
39 | * @return the translated column name for given index
|
---|
40 | * @see #getApplyColumn
|
---|
41 | */
|
---|
42 | public abstract String getCorrectionColumnName(int colIndex);
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Returns the correction value at given position.
|
---|
46 | * @param rowIndex row index
|
---|
47 | * @param colIndex column index
|
---|
48 | * @return the correction value at given position
|
---|
49 | */
|
---|
50 | public abstract Object getCorrectionValueAt(int rowIndex, int colIndex);
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Returns the list of corrections.
|
---|
54 | * @return the list of corrections
|
---|
55 | */
|
---|
56 | public List<C> getCorrections() {
|
---|
57 | return corrections;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Returns the index of the "Apply" column.
|
---|
62 | * @return the index of the "Apply" column
|
---|
63 | */
|
---|
64 | public int getApplyColumn() {
|
---|
65 | return applyColumn;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Returns the "Apply" flag for given index.
|
---|
70 | * @param i index
|
---|
71 | * @return the "Apply" flag for given index
|
---|
72 | */
|
---|
73 | public boolean getApply(int i) {
|
---|
74 | return apply[i];
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override
|
---|
78 | public int getRowCount() {
|
---|
79 | return corrections.size();
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public Class<?> getColumnClass(int columnIndex) {
|
---|
84 | if (columnIndex == applyColumn)
|
---|
85 | return Boolean.class;
|
---|
86 | return String.class;
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Override
|
---|
90 | public String getColumnName(int columnIndex) {
|
---|
91 | if (columnIndex == applyColumn)
|
---|
92 | return tr("Apply?");
|
---|
93 |
|
---|
94 | return getCorrectionColumnName(columnIndex);
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override
|
---|
98 | public boolean isCellEditable(int rowIndex, int columnIndex) {
|
---|
99 | return columnIndex == applyColumn;
|
---|
100 | }
|
---|
101 |
|
---|
102 | @Override
|
---|
103 | public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
|
---|
104 | if (columnIndex == applyColumn && aValue instanceof Boolean)
|
---|
105 | apply[rowIndex] = (Boolean) aValue;
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | public Object getValueAt(int rowIndex, int colIndex) {
|
---|
110 | if (colIndex == applyColumn)
|
---|
111 | return apply[rowIndex];
|
---|
112 |
|
---|
113 | return getCorrectionValueAt(rowIndex, colIndex);
|
---|
114 | }
|
---|
115 | }
|
---|