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>
ASectionTableModelthat 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 voidaddBean(T bean)TgetBean(int rowIndex)Get the bean at the specified index.intgetBeanCount()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)intgetColumnCount()ColumnAdapter<? super T,?>getColumnIdentifier(int modelIndex)java.lang.StringgetColumnName(int modelIndex)java.awt.CursorgetCursor(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)intgetGroupNumber(int rowIndex)intgetRowCount()java.lang.StringgetSectionName(int rowIndex)intgetSectionRow(int rowIndex)java.util.List<G>getSections()java.lang.ObjectgetValue(T bean, int columnIndex)Get a column cell value for a bean.java.lang.ObjectgetValueAt(int rowIndex, int columnIndex)voidhandleClick(java.awt.event.MouseEvent event, javax.swing.JTable table, int rowIndex, int columnIndex)Handle a mouse click on a cell.intindexOf(java.util.function.Predicate<T> predicate)Find the index of a bean matching a predicate.booleanisCellEditable(int rowIndex, int columnIndex)booleanisSectionRow(int rowIndex)protected voidnotifyDataProviders(T row, java.lang.String columnId, java.lang.Object oldValue)voidput(G group, T bean)Add a bean to a group.voidputAll(G group, java.util.Collection<? extends T> beans)Add beans to a group.voidremove(int rowIndex)Remove a bean from a group.java.util.List<T>removeAll(G group)Remove an entire group.protected voidsetBean(int rowIndex, T bean)voidsetBeans(com.google.common.collect.Multimap<G,T> beans)voidsetBeans(java.util.Collection<T> beans)Replace all rows of the table model.voidsetValueAt(java.lang.Object aValue, int rowIndex, int columnIndex)voidupdateBeans(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:
isSectionRowin interfaceSectionTableModel<G>- Returns:
- true of the row is a group header.
-
getSectionRow
public int getSectionRow(int rowIndex)
- Specified by:
getSectionRowin interfaceSectionTableModel<G>- Returns:
- the index of the group header row for the specified row.
-
getSectionName
public java.lang.String getSectionName(int rowIndex)
- Specified by:
getSectionNamein 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:
getGroupNumberin interfaceSectionTableModel<G>- Returns:
- the group number of the specified row.
-
getGroup
public java.util.List<T> getGroup(int groupNumber)
- Specified by:
getGroupin interfaceSectionTableModel<G>- Returns:
- the beans in the specified group.
-
setBeans
public void setBeans(java.util.Collection<T> beans)
Description copied from interface:BeanTableModelReplace all rows of the table model.- Specified by:
setBeansin 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:BeanTableModelUpdate existing rows and add missing rows to the table model.- Specified by:
updateBeansin 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:BeanTableModelGet the number of beans in the model. Need not be the same as the number of the rows in the table.- Specified by:
getBeanCountin interfaceBeanTableModel<G>
-
getBean
public T getBean(int rowIndex)
Description copied from interface:BeanTableModelGet the bean at the specified index.- Specified by:
getBeanin 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:BeanTableModelGet a column cell value for a bean.- Specified by:
getValuein 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:
getColumnIdentifierin interfaceColumnIdentifier
-
getColumnName
public java.lang.String getColumnName(int modelIndex)
- Specified by:
getColumnNamein interfacejavax.swing.table.TableModel- Overrides:
getColumnNamein classjavax.swing.table.AbstractTableModel
-
getColumnClass
public java.lang.Class<?> getColumnClass(int columnIndex)
- Specified by:
getColumnClassin interfacejavax.swing.table.TableModel- Overrides:
getColumnClassin classjavax.swing.table.AbstractTableModel
-
getRowCount
public int getRowCount()
- Specified by:
getRowCountin interfacejavax.swing.table.TableModel
-
getColumnCount
public int getColumnCount()
- Specified by:
getColumnCountin 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:
getValueAtin interfacejavax.swing.table.TableModel
-
isCellEditable
public boolean isCellEditable(int rowIndex, int columnIndex)- Specified by:
isCellEditablein interfacejavax.swing.table.TableModel- Overrides:
isCellEditablein 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:BeanTableModelGet the mouse cursor to display for a cell.- Specified by:
getCursorin 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
nullto 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:BeanTableModelHandle a mouse click on a cell.- Specified by:
handleClickin 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:
setValueAtin interfacejavax.swing.table.TableModel- Overrides:
setValueAtin classjavax.swing.table.AbstractTableModel
-
-