Class BeanListMultimapTableModel<G,T>
- java.lang.Object
-
- javax.swing.table.AbstractTableModel
-
- io.github.jonestimd.swing.table.model.BeanListMultimapTableModel<G,T>
-
- Type Parameters:
G
- the type of the group keysT
- the class representing a row in the table
- All Implemented Interfaces:
BeanTableModel<T>
,ColumnIdentifier
,SectionTableModel<T>
,java.io.Serializable
,javax.swing.table.TableModel
public class BeanListMultimapTableModel<G,T> extends javax.swing.table.AbstractTableModel implements ColumnIdentifier, SectionTableModel<T>, BeanTableModel<T>
ASectionTableModel
that stores the bean groups in aListMultimap
. Groups are displayed in ascending order based on the group name.- See Also:
SectionTableRowSorter
, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description BeanListMultimapTableModel(java.util.List<? extends ColumnAdapter<? super T,?>> columnAdapters, java.lang.Iterable<? extends TableDataProvider<T>> tableDataProviders, java.util.function.Function<T,G> groupingFunction, java.util.function.Function<G,java.lang.String> groupNameFunction)
Create a new model.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addBean(T bean)
T
getBean(int rowIndex)
Get the bean at the specified index.int
getBeanCount()
Get the number of beans in the model.java.util.List<T>
getBeans()
java.util.List<T>
getBeans(G group)
java.lang.Class<?>
getColumnClass(int columnIndex)
int
getColumnCount()
ColumnAdapter<? super T,?>
getColumnIdentifier(int modelIndex)
java.lang.String
getColumnName(int modelIndex)
java.awt.Cursor
getCursor(java.awt.event.MouseEvent event, javax.swing.JTable table, int rowIndex, int columnIndex)
Get the mouse cursor to display for a cell.java.util.List<T>
getGroup(int groupNumber)
int
getGroupNumber(int rowIndex)
int
getRowCount()
java.lang.String
getSectionName(int rowIndex)
int
getSectionRow(int rowIndex)
java.util.List<G>
getSections()
java.lang.Object
getValue(T bean, int columnIndex)
Get a column cell value for a bean.java.lang.Object
getValueAt(int rowIndex, int columnIndex)
void
handleClick(java.awt.event.MouseEvent event, javax.swing.JTable table, int rowIndex, int columnIndex)
Handle a mouse click on a cell.int
indexOf(java.util.function.Predicate<T> predicate)
Find the index of a bean matching a predicate.boolean
isCellEditable(int rowIndex, int columnIndex)
boolean
isSectionRow(int rowIndex)
protected void
notifyDataProviders(T row, java.lang.String columnId, java.lang.Object oldValue)
void
put(G group, T bean)
Add a bean to a group.void
putAll(G group, java.util.Collection<? extends T> beans)
Add beans to a group.void
remove(int rowIndex)
Remove a bean from a group.java.util.List<T>
removeAll(G group)
Remove an entire group.protected void
setBean(int rowIndex, T bean)
void
setBeans(com.google.common.collect.Multimap<G,T> beans)
void
setBeans(java.util.Collection<T> beans)
Replace all rows of the table model.void
setValueAt(java.lang.Object aValue, int rowIndex, int columnIndex)
void
updateBeans(java.util.Collection<T> beans, java.util.function.BiPredicate<T,T> isEqual)
Update existing rows and add missing rows to the table model.-
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getListeners, getTableModelListeners, removeTableModelListener
-
-
-
-
Constructor Detail
-
BeanListMultimapTableModel
public BeanListMultimapTableModel(java.util.List<? extends ColumnAdapter<? super T,?>> columnAdapters, java.lang.Iterable<? extends TableDataProvider<T>> tableDataProviders, java.util.function.Function<T,G> groupingFunction, java.util.function.Function<G,java.lang.String> groupNameFunction)
Create a new model.- Parameters:
columnAdapters
- provides access to column values on the row beanstableDataProviders
- supplemental data providersgroupingFunction
- provides the group key for a rowgroupNameFunction
- provides the display name for a group
-
-
Method Detail
-
isSectionRow
public boolean isSectionRow(int rowIndex)
- Specified by:
isSectionRow
in interfaceSectionTableModel<G>
- Returns:
- true of the row is a group header.
-
getSectionRow
public int getSectionRow(int rowIndex)
- Specified by:
getSectionRow
in interfaceSectionTableModel<G>
- Returns:
- the index of the group header row for the specified row.
-
getSectionName
public java.lang.String getSectionName(int rowIndex)
- Specified by:
getSectionName
in interfaceSectionTableModel<G>
- Returns:
- the display text for the header row of the group containing the specified row.
-
getGroupNumber
public int getGroupNumber(int rowIndex)
- Specified by:
getGroupNumber
in interfaceSectionTableModel<G>
- Returns:
- the group number of the specified row.
-
getGroup
public java.util.List<T> getGroup(int groupNumber)
- Specified by:
getGroup
in interfaceSectionTableModel<G>
- Returns:
- the beans in the specified group.
-
setBeans
public void setBeans(java.util.Collection<T> beans)
Description copied from interface:BeanTableModel
Replace all rows of the table model.- Specified by:
setBeans
in interfaceBeanTableModel<G>
- Parameters:
beans
- the new rows
-
updateBeans
public void updateBeans(java.util.Collection<T> beans, java.util.function.BiPredicate<T,T> isEqual)
Description copied from interface:BeanTableModel
Update existing rows and add missing rows to the table model.- Specified by:
updateBeans
in interfaceBeanTableModel<G>
- Parameters:
beans
- beans to update/appendisEqual
- used to determine if a row is already in the model
-
getBeans
public java.util.List<T> getBeans()
-
getSections
public java.util.List<G> getSections()
-
getBeanCount
public int getBeanCount()
Description copied from interface:BeanTableModel
Get the number of beans in the model. Need not be the same as the number of the rows in the table.- Specified by:
getBeanCount
in interfaceBeanTableModel<G>
-
getBean
public T getBean(int rowIndex)
Description copied from interface:BeanTableModel
Get the bean at the specified index.- Specified by:
getBean
in interfaceBeanTableModel<G>
- Parameters:
rowIndex
- the index of the bean.
-
setBean
protected void setBean(int rowIndex, T bean)
-
getValue
public java.lang.Object getValue(T bean, int columnIndex)
Description copied from interface:BeanTableModel
Get a column cell value for a bean.- Specified by:
getValue
in interfaceBeanTableModel<G>
-
addBean
public void addBean(T bean)
-
remove
public void remove(int rowIndex)
Remove a bean from a group. If the group is empty then the group is also removed.
-
removeAll
public java.util.List<T> removeAll(G group)
Remove an entire group.- Returns:
- the removed beans.
-
getColumnIdentifier
public ColumnAdapter<? super T,?> getColumnIdentifier(int modelIndex)
- Specified by:
getColumnIdentifier
in interfaceColumnIdentifier
-
getColumnName
public java.lang.String getColumnName(int modelIndex)
- Specified by:
getColumnName
in interfacejavax.swing.table.TableModel
- Overrides:
getColumnName
in classjavax.swing.table.AbstractTableModel
-
getColumnClass
public java.lang.Class<?> getColumnClass(int columnIndex)
- Specified by:
getColumnClass
in interfacejavax.swing.table.TableModel
- Overrides:
getColumnClass
in classjavax.swing.table.AbstractTableModel
-
getRowCount
public int getRowCount()
- Specified by:
getRowCount
in interfacejavax.swing.table.TableModel
-
getColumnCount
public int getColumnCount()
- Specified by:
getColumnCount
in interfacejavax.swing.table.TableModel
-
notifyDataProviders
protected void notifyDataProviders(T row, java.lang.String columnId, java.lang.Object oldValue)
-
indexOf
public int indexOf(java.util.function.Predicate<T> predicate)
Find the index of a bean matching a predicate.- Returns:
- the index of the first matching bean or -1 if none match.
-
getValueAt
public java.lang.Object getValueAt(int rowIndex, int columnIndex)
- Specified by:
getValueAt
in interfacejavax.swing.table.TableModel
-
isCellEditable
public boolean isCellEditable(int rowIndex, int columnIndex)
- Specified by:
isCellEditable
in interfacejavax.swing.table.TableModel
- Overrides:
isCellEditable
in classjavax.swing.table.AbstractTableModel
-
getCursor
public java.awt.Cursor getCursor(java.awt.event.MouseEvent event, javax.swing.JTable table, int rowIndex, int columnIndex)
Description copied from interface:BeanTableModel
Get the mouse cursor to display for a cell.- Specified by:
getCursor
in interfaceBeanTableModel<G>
rowIndex
- the row index of the cellcolumnIndex
- the column index of the cell- Returns:
- the mouse cursor to use for the cell or
null
to use the default cursor.
-
handleClick
public void handleClick(java.awt.event.MouseEvent event, javax.swing.JTable table, int rowIndex, int columnIndex)
Description copied from interface:BeanTableModel
Handle a mouse click on a cell.- Specified by:
handleClick
in interfaceBeanTableModel<G>
- Parameters:
event
- the click eventrowIndex
- the row containing the cellcolumnIndex
- the column index of the cell
-
setValueAt
public void setValueAt(java.lang.Object aValue, int rowIndex, int columnIndex)
- Specified by:
setValueAt
in interfacejavax.swing.table.TableModel
- Overrides:
setValueAt
in classjavax.swing.table.AbstractTableModel
-
-