Skip to content

Commit

Permalink
New api to enable auto clean all column filters if table model row co…
Browse files Browse the repository at this point in the history
…unt is 0 (eugener#74)

* New api to enable auto clean of column filters

* add autoclean test

* code cleanup
  • Loading branch information
blasferna authored Feb 3, 2021
1 parent 1ed5881 commit 6117cd8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public abstract class AbstractTableFilter<T extends JTable> implements ITableFil

private final T table;
private final TableFilterState filterState = new TableFilterState();
private boolean autoclean;

public AbstractTableFilter( T table ) {
this.table = table;
Expand Down Expand Up @@ -88,6 +89,9 @@ private void listenForDataChange(TableModel model) {
@Override
public void tableChanged(TableModelEvent e) {
clearDistinctItemCache();
if (autoclean && table.getModel().getRowCount() == 0) {
clear();
}
}
});
}
Expand Down Expand Up @@ -169,6 +173,11 @@ public boolean includeRow( ITableFilter.Row row ) {
public void setFilterState(int column, Collection<DistinctColumnItem> values ) {
filterState.setValues(column, values);
}

@Override
public void setAutoClean(boolean autoclean) {
this.autoclean = autoclean;
}

public void clear() {
filterState.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ public interface Row {
void addChangeListener( IFilterChangeListener listener );
void removeChangeListener( IFilterChangeListener listener );

/**
* Set flag to clear all columns filter if model row count is 0
* @param autoclean
*/
void setAutoClean(boolean autoclean);

/**
* Clear the filter
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public final class TableRowFilterSupport {
private boolean actionsVisible = true;
private int filterIconPlacement = SwingConstants.LEADING;
private boolean useTableRenderers = false;
private boolean autoclean = false;

private TableRowFilterSupport( ITableFilter<?> filter ) {
if ( filter == null ) throw new NullPointerException();
Expand Down Expand Up @@ -109,6 +110,16 @@ public TableRowFilterSupport searchable( boolean searchable ) {
this.searchable = searchable;
return this;
}

/**
* Set flag to clear all filters automatically if model row count is 0
* @param autoclean
* @return
*/
public TableRowFilterSupport autoclean( boolean autoclean ) {
this.autoclean = autoclean;
return this;
}

public TableRowFilterSupport searchFilter(IListFilter searchFilter) {
this.searchFilter = searchFilter;
Expand Down Expand Up @@ -141,7 +152,9 @@ public JTable apply() {
filterPopup.setUseTableRenderers( useTableRenderers );

setupTableHeader();


filter.setAutoClean(autoclean);

return filter.getTable();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ public void actionPerformed(ActionEvent e) {
}
});

toolbar.add( new AbstractAction("Clear Table") {

@Override
public void actionPerformed(ActionEvent e) {
DefaultTableModel modelo = (DefaultTableModel) table.getModel();
modelo.setRowCount(0);
}
});


f.pack();
f.setLocationRelativeTo(null);
Expand All @@ -97,7 +106,8 @@ public void actionPerformed(ActionEvent e) {
private TableRowFilterSupport filter;

private JTable buildTable() {
filter = TableRowFilterSupport.forTable(new JTable())

filter = TableRowFilterSupport.forTable(new JTable(new DefaultTableModel(data, colNames)))
.onFilterChange(new IFilterChangeListener() {
@Override
public void filterChanged(ITableFilter<?> filter) {
Expand All @@ -106,9 +116,11 @@ public void filterChanged(ITableFilter<?> filter) {
})
.actions(true)
.searchable(true)
.useTableRenderers(true);
.useTableRenderers(true)
.autoclean(true);

JTable table = filter.apply();
table.setModel( new DefaultTableModel(data, colNames) );
table.getColumnModel().getColumn(0).setCellRenderer(new TestRenderer());
return table;
}
Expand Down

0 comments on commit 6117cd8

Please # to comment.