Mehrere TableModels zu einem einzigen zusammenfügen: Unterschied zwischen den Versionen
Aus Byte-Welt Wiki
Zur Navigation springenZur Suche springenK |
K |
||
Zeile 10: | Zeile 10: | ||
/** | /** | ||
+ | * | ||
* This class provides utility methods to create special TableModels | * This class provides utility methods to create special TableModels | ||
* that are composed of other TableModels, or consist of selected | * that are composed of other TableModels, or consist of selected | ||
* subsets of rows and columns of other TableModels | * subsets of rows and columns of other TableModels | ||
*/ | */ | ||
− | class TableModelUtils | + | class TableModelUtils { |
− | + | ||
/** | /** | ||
* Creates a TableModel that composes the given TableModels into | * Creates a TableModel that composes the given TableModels into | ||
Zeile 24: | Zeile 25: | ||
* @return A TableModel combining the given TableModels | * @return A TableModel combining the given TableModels | ||
*/ | */ | ||
− | public static TableModel createCompoundTableModel(TableModel ... tableModels) | + | public static TableModel createCompoundTableModel(TableModel... tableModels) { |
− | |||
return new CompoundTableModel(tableModels); | return new CompoundTableModel(tableModels); | ||
} | } | ||
Zeile 45: | Zeile 45: | ||
* @return A sub-table of the given TableModel | * @return A sub-table of the given TableModel | ||
*/ | */ | ||
− | public static RangeTableModel createSubTableModel(TableModel tableModel, int row0, int col0, int row1, int col1) | + | public static RangeTableModel createSubTableModel(TableModel tableModel, int row0, int col0, int row1, int col1) { |
− | |||
return new RangeTableModel(tableModel, row0, col0, row1, col1); | return new RangeTableModel(tableModel, row0, col0, row1, col1); | ||
} | } | ||
Zeile 60: | Zeile 59: | ||
* @return | * @return | ||
*/ | */ | ||
− | public static TableModel createRowSelectionTableModel(TableModel tableModel, int ... indices) | + | public static TableModel createRowSelectionTableModel(TableModel tableModel, int... indices) { |
− | |||
return new SelectionTableModel(tableModel, indices, null); | return new SelectionTableModel(tableModel, indices, null); | ||
} | } | ||
Zeile 75: | Zeile 73: | ||
* @return | * @return | ||
*/ | */ | ||
− | public static TableModel createColumnSelectionTableModel(TableModel tableModel, int ... indices) | + | public static TableModel createColumnSelectionTableModel(TableModel tableModel, int... indices) { |
− | |||
return new SelectionTableModel(tableModel, null, indices); | return new SelectionTableModel(tableModel, null, indices); | ||
} | } | ||
− | |||
− | |||
/** | /** | ||
* This class is a TableModel that combines several other TableModels | * This class is a TableModel that combines several other TableModels | ||
*/ | */ | ||
− | private static class CompoundTableModel implements TableModel | + | private static class CompoundTableModel implements TableModel { |
− | + | ||
/** | /** | ||
* The TableModels that are combined in this TableModel | * The TableModels that are combined in this TableModel | ||
Zeile 97: | Zeile 92: | ||
* @param tableModels The TableModels to combine | * @param tableModels The TableModels to combine | ||
*/ | */ | ||
− | public CompoundTableModel(TableModel ... tableModels) | + | public CompoundTableModel(TableModel... tableModels) { |
− | |||
this.tableModels = new ArrayList<TableModel>(Arrays.asList(tableModels)); | this.tableModels = new ArrayList<TableModel>(Arrays.asList(tableModels)); | ||
} | } | ||
Zeile 105: | Zeile 99: | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public void addTableModelListener(TableModelListener tableModelListener) | + | public void addTableModelListener(TableModelListener tableModelListener) { |
− | + | for (TableModel tableModel : tableModels) { | |
− | for (TableModel tableModel : tableModels) | ||
− | |||
tableModel.addTableModelListener(tableModelListener); | tableModel.addTableModelListener(tableModelListener); | ||
} | } | ||
Zeile 116: | Zeile 108: | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public void removeTableModelListener(TableModelListener tableModelListener) | + | public void removeTableModelListener(TableModelListener tableModelListener) { |
− | + | for (TableModel tableModel : tableModels) { | |
− | for (TableModel tableModel : tableModels) | ||
− | |||
tableModel.removeTableModelListener(tableModelListener); | tableModel.removeTableModelListener(tableModelListener); | ||
} | } | ||
} | } | ||
− | |||
/** | /** | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public Class getColumnClass(int columnIndex) | + | public Class getColumnClass(int columnIndex) { |
− | + | for (TableModel tableModel : tableModels) { | |
− | for (TableModel tableModel : tableModels) | + | if (tableModel.getColumnCount() > columnIndex) { |
− | |||
− | if (tableModel.getColumnCount() > columnIndex) | ||
− | |||
return tableModel.getColumnClass(columnIndex); | return tableModel.getColumnClass(columnIndex); | ||
} | } | ||
Zeile 144: | Zeile 130: | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public int getColumnCount() | + | public int getColumnCount() { |
− | |||
int columnCount = 0; | int columnCount = 0; | ||
− | for (TableModel tableModel : tableModels) | + | for (TableModel tableModel : tableModels) { |
− | |||
columnCount += tableModel.getColumnCount(); | columnCount += tableModel.getColumnCount(); | ||
} | } | ||
Zeile 157: | Zeile 141: | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public String getColumnName(int columnIndex) | + | public String getColumnName(int columnIndex) { |
− | + | for (TableModel tableModel : tableModels) { | |
− | for (TableModel tableModel : tableModels) | + | if (tableModel.getColumnCount() > columnIndex) { |
− | |||
− | if (tableModel.getColumnCount() > columnIndex) | ||
− | |||
return tableModel.getColumnName(columnIndex); | return tableModel.getColumnName(columnIndex); | ||
} | } | ||
Zeile 169: | Zeile 150: | ||
throw new IllegalArgumentException("No column found for the given index"); | throw new IllegalArgumentException("No column found for the given index"); | ||
} | } | ||
− | |||
/** | /** | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public int getRowCount() | + | public int getRowCount() { |
− | |||
int maxRowCount = -1; | int maxRowCount = -1; | ||
− | for (TableModel tableModel : tableModels) | + | for (TableModel tableModel : tableModels) { |
− | |||
maxRowCount = Math.max(maxRowCount, tableModel.getRowCount()); | maxRowCount = Math.max(maxRowCount, tableModel.getRowCount()); | ||
} | } | ||
Zeile 187: | Zeile 165: | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public Object getValueAt(int rowIndex, int columnIndex) | + | public Object getValueAt(int rowIndex, int columnIndex) { |
− | + | for (TableModel tableModel : tableModels) { | |
− | for (TableModel tableModel : tableModels) | + | if (tableModel.getColumnCount() > columnIndex) { |
− | + | if (rowIndex < tableModel.getRowCount()) { | |
− | if (tableModel.getColumnCount() > columnIndex) | ||
− | |||
− | if (rowIndex < tableModel.getRowCount()) | ||
− | |||
return tableModel.getValueAt(rowIndex, columnIndex); | return tableModel.getValueAt(rowIndex, columnIndex); | ||
− | } | + | } else { |
− | |||
− | |||
return null; | return null; | ||
} | } | ||
Zeile 210: | Zeile 182: | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public void setValueAt(Object aValue, int rowIndex, int columnIndex) | + | public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
− | + | for (TableModel tableModel : tableModels) { | |
− | for (TableModel tableModel : tableModels) | + | if (tableModel.getColumnCount() > columnIndex) { |
− | + | if (rowIndex < tableModel.getRowCount()) { | |
− | if (tableModel.getColumnCount() > columnIndex) | ||
− | |||
− | if (rowIndex < tableModel.getRowCount()) | ||
− | |||
tableModel.setValueAt(aValue, rowIndex, columnIndex); | tableModel.setValueAt(aValue, rowIndex, columnIndex); | ||
− | } | + | return; |
− | + | } else { | |
− | |||
throw new IllegalArgumentException("No cell found for the given row index"); | throw new IllegalArgumentException("No cell found for the given row index"); | ||
} | } | ||
Zeile 229: | Zeile 196: | ||
throw new IllegalArgumentException("No column found for the given index"); | throw new IllegalArgumentException("No column found for the given index"); | ||
} | } | ||
− | |||
/** | /** | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public boolean isCellEditable(int rowIndex, int columnIndex) | + | public boolean isCellEditable(int rowIndex, int columnIndex) { |
− | + | for (TableModel tableModel : tableModels) { | |
− | for (TableModel tableModel : tableModels) | + | if (tableModel.getColumnCount() > columnIndex) { |
− | + | if (rowIndex < tableModel.getRowCount()) { | |
− | if (tableModel.getColumnCount() > columnIndex) | ||
− | |||
− | if (rowIndex < tableModel.getRowCount()) | ||
− | |||
return tableModel.isCellEditable(rowIndex, columnIndex); | return tableModel.isCellEditable(rowIndex, columnIndex); | ||
− | } | + | } else { |
− | |||
− | |||
return false; | return false; | ||
} | } | ||
Zeile 254: | Zeile 214: | ||
} | } | ||
} | } | ||
− | |||
− | |||
/** | /** | ||
Zeile 261: | Zeile 219: | ||
* of rows and columns of another TableModel | * of rows and columns of another TableModel | ||
*/ | */ | ||
− | private static class SelectionTableModel implements TableModel | + | private static class SelectionTableModel implements TableModel { |
− | + | ||
/** | /** | ||
* The TableModel from which the rows and columns are selected | * The TableModel from which the rows and columns are selected | ||
Zeile 283: | Zeile 241: | ||
* Then all rows/columns will be selected, respectively | * Then all rows/columns will be selected, respectively | ||
* | * | ||
− | * @param tableModel The TableModel from which the rows and columns are selected | + | * @param tableModel The TableModel from which the rows and columns are |
+ | * selected | ||
* @param rowSelection The selection of rows | * @param rowSelection The selection of rows | ||
* @param columnSelection The selection of columns | * @param columnSelection The selection of columns | ||
*/ | */ | ||
− | public SelectionTableModel(TableModel tableModel, int rowSelection[], int columnSelection[]) | + | public SelectionTableModel(TableModel tableModel, int rowSelection[], int columnSelection[]) { |
− | |||
this.tableModel = tableModel; | this.tableModel = tableModel; | ||
− | if (rowSelection != null) | + | if (rowSelection != null) { |
− | |||
this.rowSelection = rowSelection.clone(); | this.rowSelection = rowSelection.clone(); | ||
− | } | + | } else { |
− | |||
− | |||
this.rowSelection = createSelectionFromSize(tableModel.getRowCount()); | this.rowSelection = createSelectionFromSize(tableModel.getRowCount()); | ||
} | } | ||
− | if (columnSelection != null) | + | if (columnSelection != null) { |
− | |||
this.columnSelection = columnSelection.clone(); | this.columnSelection = columnSelection.clone(); | ||
− | } | + | } else { |
− | |||
− | |||
this.columnSelection = createSelectionFromSize(tableModel.getColumnCount()); | this.columnSelection = createSelectionFromSize(tableModel.getColumnCount()); | ||
} | } | ||
Zeile 314: | Zeile 266: | ||
* @return The selection array | * @return The selection array | ||
*/ | */ | ||
− | static int[] createSelectionFromSize(int size) | + | static int[] createSelectionFromSize(int size) { |
− | |||
int selected[] = new int[size]; | int selected[] = new int[size]; | ||
− | for (int i=0; i<selected.length; i++) | + | for (int i = 0; i < selected.length; i++) { |
− | |||
selected[i] = i; | selected[i] = i; | ||
} | } | ||
Zeile 331: | Zeile 281: | ||
* @return The selection array | * @return The selection array | ||
*/ | */ | ||
− | static int[] createSelectionFromRange(int min, int max) | + | static int[] createSelectionFromRange(int min, int max) { |
− | + | int selected[] = new int[max - min]; | |
− | int selected[] = new int[max-min]; | + | for (int i = 0; i < selected.length; i++) { |
− | for (int i=0; i<selected.length; i++) | ||
− | |||
selected[i] = i + min; | selected[i] = i + min; | ||
} | } | ||
return selected; | return selected; | ||
} | } | ||
− | |||
/** | /** | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public void addTableModelListener(TableModelListener tableModelListener) | + | public void addTableModelListener(TableModelListener tableModelListener) { |
− | |||
tableModel.addTableModelListener(tableModelListener); | tableModel.addTableModelListener(tableModelListener); | ||
} | } | ||
Zeile 353: | Zeile 299: | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public void removeTableModelListener(TableModelListener tableModelListener) | + | public void removeTableModelListener(TableModelListener tableModelListener) { |
− | |||
tableModel.removeTableModelListener(tableModelListener); | tableModel.removeTableModelListener(tableModelListener); | ||
} | } | ||
Zeile 361: | Zeile 306: | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public Class getColumnClass(int columnIndex) | + | public Class getColumnClass(int columnIndex) { |
− | |||
return tableModel.getColumnClass(columnSelection[columnIndex]); | return tableModel.getColumnClass(columnSelection[columnIndex]); | ||
} | } | ||
Zeile 369: | Zeile 313: | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public int getColumnCount() | + | public int getColumnCount() { |
− | |||
return columnSelection.length; | return columnSelection.length; | ||
} | } | ||
Zeile 377: | Zeile 320: | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public String getColumnName(int columnIndex) | + | public String getColumnName(int columnIndex) { |
− | |||
return tableModel.getColumnName(columnSelection[columnIndex]); | return tableModel.getColumnName(columnSelection[columnIndex]); | ||
} | } | ||
Zeile 385: | Zeile 327: | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public int getRowCount() | + | public int getRowCount() { |
− | |||
return rowSelection.length; | return rowSelection.length; | ||
} | } | ||
Zeile 393: | Zeile 334: | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public Object getValueAt(int rowIndex, int columnIndex) | + | public Object getValueAt(int rowIndex, int columnIndex) { |
− | |||
return tableModel.getValueAt(rowSelection[rowIndex], columnSelection[columnIndex]); | return tableModel.getValueAt(rowSelection[rowIndex], columnSelection[columnIndex]); | ||
} | } | ||
Zeile 401: | Zeile 341: | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public void setValueAt(Object aValue, int rowIndex, int columnIndex) | + | public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
− | |||
tableModel.setValueAt(aValue, rowSelection[rowIndex], columnSelection[columnIndex]); | tableModel.setValueAt(aValue, rowSelection[rowIndex], columnSelection[columnIndex]); | ||
} | } | ||
− | |||
/** | /** | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public boolean isCellEditable(int rowIndex, int columnIndex) | + | public boolean isCellEditable(int rowIndex, int columnIndex) { |
− | |||
return tableModel.isCellEditable(rowSelection[rowIndex], columnSelection[columnIndex]); | return tableModel.isCellEditable(rowSelection[rowIndex], columnSelection[columnIndex]); | ||
} | } | ||
} | } | ||
− | |||
/** | /** | ||
Zeile 422: | Zeile 358: | ||
* of rows and columns of another TableModel | * of rows and columns of another TableModel | ||
*/ | */ | ||
− | public static class RangeTableModel implements TableModel | + | public static class RangeTableModel implements TableModel { |
− | + | ||
/** | /** | ||
* The TableModel from which the rows and columns are selected | * The TableModel from which the rows and columns are selected | ||
*/ | */ | ||
private TableModel tableModel; | private TableModel tableModel; | ||
− | |||
/** | /** | ||
Zeile 459: | Zeile 394: | ||
* and columns from the given TableModel. | * and columns from the given TableModel. | ||
* | * | ||
− | * @param tableModel The TableModel from which the rows and columns are selected | + | * @param tableModel The TableModel from which the rows and columns are |
− | * @param minRow The row of the given model which will be row 0 in this model | + | * selected |
− | * @param minColumn The column of the given model which will be column 0 in this model | + | * @param minRow The row of the given model which will be row 0 in this |
− | * @param maxRow The row of the given model which will be the last row in this model | + | * model |
− | * @param maxColumn The column of the given model which will be the last column in this model | + | * @param minColumn The column of the given model which will be column 0 |
+ | * in this model | ||
+ | * @param maxRow The row of the given model which will be the last row | ||
+ | * in this model | ||
+ | * @param maxColumn The column of the given model which will be the last | ||
+ | * column in this model | ||
*/ | */ | ||
− | public RangeTableModel(TableModel tableModel, int minRow, int minColumn, int maxRow, int maxColumn) | + | public RangeTableModel(TableModel tableModel, int minRow, int minColumn, int maxRow, int maxColumn) { |
− | |||
this.tableModel = tableModel; | this.tableModel = tableModel; | ||
this.minRow = minRow; | this.minRow = minRow; | ||
Zeile 473: | Zeile 412: | ||
this.maxColumn = maxColumn; | this.maxColumn = maxColumn; | ||
} | } | ||
− | |||
/** | /** | ||
* Set the range that is displayed by this RangeTableModel. | * Set the range that is displayed by this RangeTableModel. | ||
* | * | ||
− | * @param minRow The row of the given model which will be row 0 in this model | + | * @param minRow The row of the given model which will be row 0 in this |
− | * @param minColumn The column of the given model which will be column 0 in this model | + | * model |
− | * @param maxRow The row of the given model which will be the last row in this model | + | * @param minColumn The column of the given model which will be column 0 |
− | * @param maxColumn The column of the given model which will be the last column in this model | + | * in this model |
+ | * @param maxRow The row of the given model which will be the last row | ||
+ | * in this model | ||
+ | * @param maxColumn The column of the given model which will be the last | ||
+ | * column in this model | ||
*/ | */ | ||
− | public void setRange(int minRow, int minColumn, int maxRow, int maxColumn) | + | public void setRange(int minRow, int minColumn, int maxRow, int maxColumn) { |
− | |||
this.minRow = minRow; | this.minRow = minRow; | ||
this.maxRow = maxRow; | this.maxRow = maxRow; | ||
this.minColumn = minColumn; | this.minColumn = minColumn; | ||
this.maxColumn = maxColumn; | this.maxColumn = maxColumn; | ||
− | if (tableModelListeners.size() > 0) | + | if (tableModelListeners.size() > 0) { |
− | |||
TableModelEvent tableModelEvent = new TableModelEvent(this, TableModelEvent.HEADER_ROW); | TableModelEvent tableModelEvent = new TableModelEvent(this, TableModelEvent.HEADER_ROW); | ||
− | for (TableModelListener tableModelListener : tableModelListeners) | + | for (TableModelListener tableModelListener : tableModelListeners) { |
− | |||
tableModelListener.tableChanged(tableModelEvent); | tableModelListener.tableChanged(tableModelEvent); | ||
} | } | ||
} | } | ||
} | } | ||
− | |||
− | |||
/** | /** | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public void addTableModelListener(TableModelListener tableModelListener) | + | public void addTableModelListener(TableModelListener tableModelListener) { |
− | |||
tableModel.addTableModelListener(tableModelListener); | tableModel.addTableModelListener(tableModelListener); | ||
tableModelListeners.add(tableModelListener); | tableModelListeners.add(tableModelListener); | ||
Zeile 513: | Zeile 449: | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public void removeTableModelListener(TableModelListener tableModelListener) | + | public void removeTableModelListener(TableModelListener tableModelListener) { |
− | |||
tableModel.removeTableModelListener(tableModelListener); | tableModel.removeTableModelListener(tableModelListener); | ||
tableModelListeners.remove(tableModelListener); | tableModelListeners.remove(tableModelListener); | ||
Zeile 522: | Zeile 457: | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public Class getColumnClass(int columnIndex) | + | public Class getColumnClass(int columnIndex) { |
− | + | return tableModel.getColumnClass(columnIndex + minColumn); | |
− | return tableModel.getColumnClass(columnIndex+minColumn); | ||
} | } | ||
Zeile 530: | Zeile 464: | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public int getColumnCount() | + | public int getColumnCount() { |
− | |||
return maxColumn - minColumn + 1; | return maxColumn - minColumn + 1; | ||
} | } | ||
Zeile 538: | Zeile 471: | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public String getColumnName(int columnIndex) | + | public String getColumnName(int columnIndex) { |
− | + | return tableModel.getColumnName(columnIndex + minColumn); | |
− | return tableModel.getColumnName(columnIndex+minColumn); | ||
} | } | ||
Zeile 546: | Zeile 478: | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public int getRowCount() | + | public int getRowCount() { |
− | |||
return maxRow - minRow + 1; | return maxRow - minRow + 1; | ||
} | } | ||
Zeile 554: | Zeile 485: | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public Object getValueAt(int rowIndex, int columnIndex) | + | public Object getValueAt(int rowIndex, int columnIndex) { |
− | |||
return tableModel.getValueAt(rowIndex + minRow, columnIndex + minColumn); | return tableModel.getValueAt(rowIndex + minRow, columnIndex + minColumn); | ||
} | } | ||
Zeile 562: | Zeile 492: | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public void setValueAt(Object aValue, int rowIndex, int columnIndex) | + | public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
− | |||
tableModel.setValueAt(aValue, rowIndex + minRow, columnIndex + minColumn); | tableModel.setValueAt(aValue, rowIndex + minRow, columnIndex + minColumn); | ||
} | } | ||
− | |||
/** | /** | ||
* {@inheritDoc} | * {@inheritDoc} | ||
*/ | */ | ||
− | public boolean isCellEditable(int rowIndex, int columnIndex) | + | public boolean isCellEditable(int rowIndex, int columnIndex) { |
− | |||
return tableModel.isCellEditable(rowIndex + minRow, columnIndex + minColumn); | return tableModel.isCellEditable(rowIndex + minRow, columnIndex + minColumn); | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
} | } |
Version vom 26. Mai 2014, 16:16 Uhr
RangeTableModel
Mit folgendem Code kann man mehrere TableModels zu einem zusammenfügen, oder bestimmte Zeilen/Spaltenmengen aus einem bestehenden TableModel als ein neues, eigenständiges TableModel anbieten.
Trivialkram, aber immerhin braucht jemand, der das braucht, diesen Trivialkram dann nicht mehr selbst zu schreiben.
<code=java> import java.util.*; import javax.swing.event.*; import javax.swing.table.*;
/**
* * This class provides utility methods to create special TableModels * that are composed of other TableModels, or consist of selected * subsets of rows and columns of other TableModels */
class TableModelUtils {
/** * Creates a TableModel that composes the given TableModels into * one. The number of rows of the resulting TableModel will be * the maximum of all row counts of the given TableModels * * @param tableModels The TableModels to combine * @return A TableModel combining the given TableModels */ public static TableModel createCompoundTableModel(TableModel... tableModels) { return new CompoundTableModel(tableModels); }
/** * Creates a new TableModel which represents the "sub-table" that is * described by the given row- and column indices * * @param tableModel The TableModel from which the given sub-table * should be taken * @param col0 The first column of the sub-table, i.e. index of the * column in the given TableModel that should be the column with * index 0 in the returned model * @param row0 The first row of the sub-table, i.e. index of the * row in the given TableModel that should be the row with * index 0 in the returned model * @param col1 The column index where the sub-table should end * @param row1 The row index where the sub-table should end * @return A sub-table of the given TableModel */ public static RangeTableModel createSubTableModel(TableModel tableModel, int row0, int col0, int row1, int col1) { return new RangeTableModel(tableModel, row0, col0, row1, col1); }
/** * Creates a TableModel that consists of the rows with the * given indices of the given TableModel * * @param tableModel The TableModel from which the rows * will be taken * @param indices The indices of the rows that should be * contained in the resulting TableModel * @return */ public static TableModel createRowSelectionTableModel(TableModel tableModel, int... indices) { return new SelectionTableModel(tableModel, indices, null); }
/** * Creates a TableModel that consists of the columns with the * given indices of the given TableModel * * @param tableModel The TableModel from which the columns * will be taken * @param indices The indices of the columns that should be * contained in the resulting TableModel * @return */ public static TableModel createColumnSelectionTableModel(TableModel tableModel, int... indices) { return new SelectionTableModel(tableModel, null, indices); }
/** * This class is a TableModel that combines several other TableModels */ private static class CompoundTableModel implements TableModel {
/** * The TableModels that are combined in this TableModel */ private List<TableModel> tableModels;
/** * Create a new CompoundTableModel, combining the given TableModels * * @param tableModels The TableModels to combine */ public CompoundTableModel(TableModel... tableModels) { this.tableModels = new ArrayList<TableModel>(Arrays.asList(tableModels)); }
/** * {@inheritDoc} */ public void addTableModelListener(TableModelListener tableModelListener) { for (TableModel tableModel : tableModels) { tableModel.addTableModelListener(tableModelListener); } }
/** * {@inheritDoc} */ public void removeTableModelListener(TableModelListener tableModelListener) { for (TableModel tableModel : tableModels) { tableModel.removeTableModelListener(tableModelListener); } }
/** * {@inheritDoc} */ public Class getColumnClass(int columnIndex) { for (TableModel tableModel : tableModels) { if (tableModel.getColumnCount() > columnIndex) { return tableModel.getColumnClass(columnIndex); } columnIndex -= tableModel.getColumnCount(); } throw new IllegalArgumentException("No column found for the given index"); }
/** * {@inheritDoc} */ public int getColumnCount() { int columnCount = 0; for (TableModel tableModel : tableModels) { columnCount += tableModel.getColumnCount(); } return columnCount; }
/** * {@inheritDoc} */ public String getColumnName(int columnIndex) { for (TableModel tableModel : tableModels) { if (tableModel.getColumnCount() > columnIndex) { return tableModel.getColumnName(columnIndex); } columnIndex -= tableModel.getColumnCount(); } throw new IllegalArgumentException("No column found for the given index"); }
/** * {@inheritDoc} */ public int getRowCount() { int maxRowCount = -1; for (TableModel tableModel : tableModels) { maxRowCount = Math.max(maxRowCount, tableModel.getRowCount()); } return maxRowCount; }
/** * {@inheritDoc} */ public Object getValueAt(int rowIndex, int columnIndex) { for (TableModel tableModel : tableModels) { if (tableModel.getColumnCount() > columnIndex) { if (rowIndex < tableModel.getRowCount()) { return tableModel.getValueAt(rowIndex, columnIndex); } else { return null; } } columnIndex -= tableModel.getColumnCount(); } throw new IllegalArgumentException("No column found for the given index"); }
/** * {@inheritDoc} */ public void setValueAt(Object aValue, int rowIndex, int columnIndex) { for (TableModel tableModel : tableModels) { if (tableModel.getColumnCount() > columnIndex) { if (rowIndex < tableModel.getRowCount()) { tableModel.setValueAt(aValue, rowIndex, columnIndex); return; } else { throw new IllegalArgumentException("No cell found for the given row index"); } } columnIndex -= tableModel.getColumnCount(); } throw new IllegalArgumentException("No column found for the given index"); }
/** * {@inheritDoc} */ public boolean isCellEditable(int rowIndex, int columnIndex) { for (TableModel tableModel : tableModels) { if (tableModel.getColumnCount() > columnIndex) { if (rowIndex < tableModel.getRowCount()) { return tableModel.isCellEditable(rowIndex, columnIndex); } else { return false; } } columnIndex -= tableModel.getColumnCount(); } throw new IllegalArgumentException("No column found for the given index"); } }
/** * This class describes a TableModel which represents a selection * of rows and columns of another TableModel */ private static class SelectionTableModel implements TableModel {
/** * The TableModel from which the rows and columns are selected */ private TableModel tableModel;
/** * The selection of rows */ private int rowSelection[];
/** * The selection of columns */ private int columnSelection[];
/** * Creates a new SelectionTableModel, which selects the specified rows * and columns from the given TableModel. The selections may be null. * Then all rows/columns will be selected, respectively * * @param tableModel The TableModel from which the rows and columns are * selected * @param rowSelection The selection of rows * @param columnSelection The selection of columns */ public SelectionTableModel(TableModel tableModel, int rowSelection[], int columnSelection[]) { this.tableModel = tableModel; if (rowSelection != null) { this.rowSelection = rowSelection.clone(); } else { this.rowSelection = createSelectionFromSize(tableModel.getRowCount()); } if (columnSelection != null) { this.columnSelection = columnSelection.clone(); } else { this.columnSelection = createSelectionFromSize(tableModel.getColumnCount()); } }
/** * Creates a selection containing all indices from 0 to size-1 * * @param size The size of the selection * @return The selection array */ static int[] createSelectionFromSize(int size) { int selected[] = new int[size]; for (int i = 0; i < selected.length; i++) { selected[i] = i; } return selected; }
/** * Creates a selection containing all indices from min to max-1 * * @param min The minimum value to be selected * @param max The maximum value to be selected * @return The selection array */ static int[] createSelectionFromRange(int min, int max) { int selected[] = new int[max - min]; for (int i = 0; i < selected.length; i++) { selected[i] = i + min; } return selected; }
/** * {@inheritDoc} */ public void addTableModelListener(TableModelListener tableModelListener) { tableModel.addTableModelListener(tableModelListener); }
/** * {@inheritDoc} */ public void removeTableModelListener(TableModelListener tableModelListener) { tableModel.removeTableModelListener(tableModelListener); }
/** * {@inheritDoc} */ public Class getColumnClass(int columnIndex) { return tableModel.getColumnClass(columnSelection[columnIndex]); }
/** * {@inheritDoc} */ public int getColumnCount() { return columnSelection.length; }
/** * {@inheritDoc} */ public String getColumnName(int columnIndex) { return tableModel.getColumnName(columnSelection[columnIndex]); }
/** * {@inheritDoc} */ public int getRowCount() { return rowSelection.length; }
/** * {@inheritDoc} */ public Object getValueAt(int rowIndex, int columnIndex) { return tableModel.getValueAt(rowSelection[rowIndex], columnSelection[columnIndex]); }
/** * {@inheritDoc} */ public void setValueAt(Object aValue, int rowIndex, int columnIndex) { tableModel.setValueAt(aValue, rowSelection[rowIndex], columnSelection[columnIndex]); }
/** * {@inheritDoc} */ public boolean isCellEditable(int rowIndex, int columnIndex) { return tableModel.isCellEditable(rowSelection[rowIndex], columnSelection[columnIndex]); }
}
/** * This class describes a TableModel which represents a range * of rows and columns of another TableModel */ public static class RangeTableModel implements TableModel {
/** * The TableModel from which the rows and columns are selected */ private TableModel tableModel;
/** * The TableModelListeners that have been added to this TableModel */ private List<TableModelListener> tableModelListeners = new ArrayList<TableModelListener>();
/** * The minimum row */ private int minRow;
/** * The maximum row */ private int maxRow;
/** * The minimum column */ private int minColumn;
/** * The maximum column */ private int maxColumn;
/** * Creates a new SelectionTableModel, which selects the specified rows * and columns from the given TableModel. * * @param tableModel The TableModel from which the rows and columns are * selected * @param minRow The row of the given model which will be row 0 in this * model * @param minColumn The column of the given model which will be column 0 * in this model * @param maxRow The row of the given model which will be the last row * in this model * @param maxColumn The column of the given model which will be the last * column in this model */ public RangeTableModel(TableModel tableModel, int minRow, int minColumn, int maxRow, int maxColumn) { this.tableModel = tableModel; this.minRow = minRow; this.maxRow = maxRow; this.minColumn = minColumn; this.maxColumn = maxColumn; }
/** * Set the range that is displayed by this RangeTableModel. * * @param minRow The row of the given model which will be row 0 in this * model * @param minColumn The column of the given model which will be column 0 * in this model * @param maxRow The row of the given model which will be the last row * in this model * @param maxColumn The column of the given model which will be the last * column in this model */ public void setRange(int minRow, int minColumn, int maxRow, int maxColumn) { this.minRow = minRow; this.maxRow = maxRow; this.minColumn = minColumn; this.maxColumn = maxColumn; if (tableModelListeners.size() > 0) { TableModelEvent tableModelEvent = new TableModelEvent(this, TableModelEvent.HEADER_ROW); for (TableModelListener tableModelListener : tableModelListeners) { tableModelListener.tableChanged(tableModelEvent); } } }
/** * {@inheritDoc} */ public void addTableModelListener(TableModelListener tableModelListener) { tableModel.addTableModelListener(tableModelListener); tableModelListeners.add(tableModelListener); }
/** * {@inheritDoc} */ public void removeTableModelListener(TableModelListener tableModelListener) { tableModel.removeTableModelListener(tableModelListener); tableModelListeners.remove(tableModelListener); }
/** * {@inheritDoc} */ public Class getColumnClass(int columnIndex) { return tableModel.getColumnClass(columnIndex + minColumn); }
/** * {@inheritDoc} */ public int getColumnCount() { return maxColumn - minColumn + 1; }
/** * {@inheritDoc} */ public String getColumnName(int columnIndex) { return tableModel.getColumnName(columnIndex + minColumn); }
/** * {@inheritDoc} */ public int getRowCount() { return maxRow - minRow + 1; }
/** * {@inheritDoc} */ public Object getValueAt(int rowIndex, int columnIndex) { return tableModel.getValueAt(rowIndex + minRow, columnIndex + minColumn); }
/** * {@inheritDoc} */ public void setValueAt(Object aValue, int rowIndex, int columnIndex) { tableModel.setValueAt(aValue, rowIndex + minRow, columnIndex + minColumn); }
/** * {@inheritDoc} */ public boolean isCellEditable(int rowIndex, int columnIndex) { return tableModel.isCellEditable(rowIndex + minRow, columnIndex + minColumn); } }
} </code=java>
Nachbetrachtung
Eigentlich wär's ja noch schick, das ganze in dem Sinne "mächtiger" zu machen, dass man sowas wie die hintereinanderschaltbaren RowFilter
(und entsprechende ColumnFilter
) einbaut, aber ... da das ja keiner braucht....
--Marco13 27.08.2008, 19:39