您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TableTableModel.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing.components;
  18. import com.google.common.collect.HashBasedTable;
  19. import com.google.common.collect.Lists;
  20. import com.google.common.collect.Table;
  21. import java.util.List;
  22. import java.util.function.BiFunction;
  23. import javax.swing.table.AbstractTableModel;
  24. import javax.swing.table.TableModel;
  25. /**
  26. * A Swing {@link TableModel} that uses a Guava {@link Table} as a backing store.
  27. */
  28. public class TableTableModel extends AbstractTableModel {
  29. private static final long serialVersionUID = 1L;
  30. private final List<String> headers;
  31. private final Table<Integer, Integer, String> table;
  32. private final BiFunction<Integer, Integer, Boolean> editableFunction;
  33. /**
  34. * Creates a new table model.
  35. *
  36. * @param headers The headers to use for the table.
  37. * @param table The table backing this table.
  38. * @param editableFunction The function to decide if a cell will be editable
  39. */
  40. public TableTableModel(final Iterable<String> headers,
  41. final Table<Integer, Integer, String> table,
  42. final BiFunction<Integer, Integer, Boolean> editableFunction) {
  43. this.headers = Lists.newArrayList(headers);
  44. this.table = HashBasedTable.create(table);
  45. this.editableFunction = editableFunction;
  46. }
  47. @Override
  48. public int getRowCount() {
  49. return table.rowKeySet().size();
  50. }
  51. @Override
  52. public int getColumnCount() {
  53. return table.columnKeySet().size();
  54. }
  55. @Override
  56. public Object getValueAt(final int rowIndex, final int columnIndex) {
  57. return table.get(rowIndex, columnIndex);
  58. }
  59. @Override
  60. public String getColumnName(final int columnIndex) {
  61. return headers.get(columnIndex);
  62. }
  63. @Override
  64. public Class<?> getColumnClass(final int columnIndex) {
  65. return String.class;
  66. }
  67. @Override
  68. public boolean isCellEditable(final int rowIndex, final int columnIndex) {
  69. return editableFunction.apply(rowIndex, columnIndex);
  70. }
  71. @Override
  72. public void setValueAt(final Object aValue, final int rowIndex, final int columnIndex) {
  73. table.put(rowIndex, columnIndex, (String) aValue);
  74. fireTableCellUpdated(rowIndex, columnIndex);
  75. }
  76. }