001 // License: GPL. For details, see LICENSE file.
002 package org.openstreetmap.josm.corrector;
003
004 import static org.openstreetmap.josm.tools.I18n.tr;
005
006 import java.util.Arrays;
007 import java.util.List;
008
009 import javax.swing.table.AbstractTableModel;
010
011 public abstract class CorrectionTableModel<C extends Correction> extends
012 AbstractTableModel {
013
014 private List<C> corrections;
015 private boolean[] apply;
016 private int applyColumn;
017
018 public CorrectionTableModel(List<C> corrections) {
019 super();
020 this.corrections = corrections;
021 apply = new boolean[this.corrections.size()];
022 Arrays.fill(apply, true);
023 applyColumn = getColumnCount() - 1;
024 }
025
026 abstract public int getColumnCount();
027
028 abstract protected boolean isBoldCell(int row, int column);
029 abstract public String getCorrectionColumnName(int colIndex);
030 abstract public Object getCorrectionValueAt(int rowIndex, int colIndex);
031
032 public List<C> getCorrections() {
033 return corrections;
034 }
035
036 public int getApplyColumn() {
037 return applyColumn;
038 }
039
040 public boolean getApply(int i) {
041 return apply[i];
042 }
043
044 public int getRowCount() {
045 return corrections.size();
046 }
047
048 @Override
049 public Class<?> getColumnClass(int columnIndex) {
050 if (columnIndex == applyColumn)
051 return Boolean.class;
052 return String.class;
053 }
054
055 @Override
056 public String getColumnName(int columnIndex) {
057 if (columnIndex == applyColumn)
058 return tr("Apply?");
059
060 return getCorrectionColumnName(columnIndex);
061 }
062
063 @Override
064 public boolean isCellEditable(int rowIndex, int columnIndex) {
065 return columnIndex == applyColumn;
066 }
067
068 @Override
069 public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
070 if (columnIndex == applyColumn && aValue instanceof Boolean)
071 apply[rowIndex] = (Boolean)aValue;
072 }
073
074 public Object getValueAt(int rowIndex, int colIndex) {
075 if (colIndex == applyColumn)
076 return apply[rowIndex];
077
078 return getCorrectionValueAt(rowIndex, colIndex);
079 }
080 }