001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.correction;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.util.Arrays;
007import java.util.List;
008
009import javax.swing.table.AbstractTableModel;
010
011import org.openstreetmap.josm.data.correction.Correction;
012
013/**
014 * Abstract correction table model.
015 * @param <C> type of correction
016 */
017public abstract class CorrectionTableModel<C extends Correction> extends AbstractTableModel {
018
019    private final transient List<C> corrections;
020    private final boolean[] apply;
021    private final int applyColumn;
022
023    /**
024     * Constructs a new {@code CorrectionTableModel}.
025     * @param corrections list of corrections
026     */
027    protected CorrectionTableModel(List<C> corrections) {
028        this.corrections = corrections;
029        apply = new boolean[this.corrections.size()];
030        Arrays.fill(apply, true);
031        applyColumn = getColumnCount() - 1;
032    }
033
034    protected abstract boolean isBoldCell(int row, int column);
035
036    /**
037     * Returns the column name for columns other than "Apply".
038     * @param colIndex column index
039     * @return the translated column name for given index
040     * @see #getApplyColumn
041     */
042    public abstract String getCorrectionColumnName(int colIndex);
043
044    /**
045     * Returns the correction value at given position.
046     * @param rowIndex row index
047     * @param colIndex column index
048     * @return the correction value at given position
049     */
050    public abstract Object getCorrectionValueAt(int rowIndex, int colIndex);
051
052    /**
053     * Returns the list of corrections.
054     * @return the list of corrections
055     */
056    public List<C> getCorrections() {
057        return corrections;
058    }
059
060    /**
061     * Returns the index of the "Apply" column.
062     * @return the index of the "Apply" column
063     */
064    public int getApplyColumn() {
065        return applyColumn;
066    }
067
068    /**
069     * Returns the "Apply" flag for given index.
070     * @param i index
071     * @return the "Apply" flag for given index
072     */
073    public boolean getApply(int i) {
074        return apply[i];
075    }
076
077    @Override
078    public int getRowCount() {
079        return corrections.size();
080    }
081
082    @Override
083    public Class<?> getColumnClass(int columnIndex) {
084        if (columnIndex == applyColumn)
085            return Boolean.class;
086        return String.class;
087    }
088
089    @Override
090    public String getColumnName(int columnIndex) {
091        if (columnIndex == applyColumn)
092            return tr("Apply?");
093
094        return getCorrectionColumnName(columnIndex);
095    }
096
097    @Override
098    public boolean isCellEditable(int rowIndex, int columnIndex) {
099        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}