Skip to content

Commit

Permalink
EntityTableCellRenderer bug fixed, create(Attribute, SwingEntityTable…
Browse files Browse the repository at this point in the history
…Model) never got called

EntityTable.Config.cellEditorFactory() and cellRendererFactory() added
  • Loading branch information
bjorndarri committed Oct 16, 2024
1 parent 064fa40 commit 2b3c492
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Codion Change Log
- EntityComboBox.model() added for consistency.
- EntityComboBox.createForeignKeyFilterControl() removed.
- EntityTablePanel, no longer removes tooltips from south toolbar buttons, seem to only block mouse cursor in MetalLookAndFeel.
- EntityTableCellRenderer bug fixed, create(Attribute, SwingEntityTableModel) never got called.
- EntityTable.Config.cellEditorFactory() and cellRendererFactory() added.

## 0.18.18
### is.codion.common.db
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,51 @@ public Object value(Object row, Integer identifier) {
)));
}

@Test
void cellRenderers() {
FilterTableModel.Columns<Object, Integer> columns = new FilterTableModel.Columns<>() {
@Override
public List<Integer> identifiers() {
return List.of(0, 1);
}

@Override
public Class<?> columnClass(Integer identifier) {
return Object.class;
}

@Override
public Object value(Object row, Integer identifier) {
return 1;
}
};
List<FilterTableColumn<Integer>> tableColumns = asList(
FilterTableColumn.builder(0).build(),
FilterTableColumn.builder(1).build());

FilterTableModel<Object, Integer> model = FilterTableModel.builder(columns).build();
model.items().addItem(1);

FilterTableCellRenderer<Object> zeroRenderer = FilterTableCellRenderer.builder(Object.class).build();
FilterTableCellRenderer<Object> oneRenderer = FilterTableCellRenderer.builder(Object.class).build();

FilterTable<Object, Integer> table = FilterTable.builder(model, tableColumns)
.cellRenderer(0, zeroRenderer)
.cellRendererFactory(new FilterTableCellRenderer.Factory<Object, Integer>() {
@Override
public <T> FilterTableCellRenderer<T> create(Integer identifier, FilterTableModel<Object, Integer> tableModel) {
return (FilterTableCellRenderer<T>) oneRenderer;
}
})
.build();

assertSame(zeroRenderer, table.columnModel().column(0).getCellRenderer());
assertSame(zeroRenderer, table.getCellRenderer(0, 0));

assertSame(oneRenderer, table.columnModel().column(1).getCellRenderer());
assertSame(oneRenderer, table.getCellRenderer(0, 1));
}

private static boolean tableModelContainsAll(List<TestRow> rows, boolean includeFiltered,
FilterTableModel<TestRow, Integer> model) {
for (TestRow row : rows) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public interface Factory extends FilterTableCellRenderer.Factory<Entity, Attribu

@Override
default <T> FilterTableCellRenderer<T> create(Attribute<?> attribute, FilterTableModel<Entity, Attribute<?>> tableModel) {
return (FilterTableCellRenderer<T>) builder(attribute, (SwingEntityTableModel) tableModel).build();
return (FilterTableCellRenderer<T>) create(attribute, (SwingEntityTableModel) tableModel);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2462,7 +2462,7 @@ public Config autoResizeModeSelection(AutoResizeModeSelection autoResizeModeSele
}

/**
* @param editAttributeSelection specifies attributes are selected when editing the selected records
* @param editAttributeSelection specifies how attribute selection is presented selected when editing the selected records
* @return this Config instance
*/
public Config editAttributeSelection(EditAttributeSelection editAttributeSelection) {
Expand Down Expand Up @@ -2529,6 +2529,17 @@ public <T, A extends Attribute<T>> Config cellEditor(A attribute, FilterTableCel
return this;
}

/**
* Overridden by {@link #cellEditor(Attribute, FilterTableCellEditor)}.
* @param cellEditorFactory the cell editor factory
* @return this Config instance
* @see FilterTable.Builder#cellRendererFactory(FilterTableCellRenderer.Factory)
*/
public Config cellEditorFactory(FilterTableCellEditor.Factory<Attribute<?>> cellEditorFactory) {
tableBuilder.cellEditorFactory(requireNonNull(cellEditorFactory));
return this;
}

/**
* Sets the cell renderer for the given attribute
* @param attribute the attribute
Expand All @@ -2544,6 +2555,17 @@ public <T, A extends Attribute<T>> Config cellRenderer(A attribute, FilterTableC
return this;
}

/**
* Overridden by {@link #cellRenderer(Attribute, FilterTableCellRenderer)}.
* @param cellRendererFactory the cell renderer factory
* @return this Config instance
* @see FilterTable.Builder#cellRendererFactory(FilterTableCellRenderer.Factory)
*/
public Config cellRendererFactory(FilterTableCellRenderer.Factory<Entity, Attribute<?>> cellRendererFactory) {
tableBuilder.cellRendererFactory(cellRendererFactory);
return this;
}

/**
* @param referentialIntegrityErrorHandling the action to take on a referential integrity error on delete
* @return this Config instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import is.codion.framework.domain.entity.Entity;
import is.codion.framework.domain.entity.OrderBy;
import is.codion.framework.domain.entity.attribute.Attribute;
import is.codion.swing.common.ui.component.table.FilterTableCellRenderer;
import is.codion.swing.common.ui.component.table.FilterTableColumn;
import is.codion.swing.common.ui.component.table.FilterTableColumnModel;
import is.codion.swing.framework.model.SwingEntityTableModel;
Expand Down Expand Up @@ -200,6 +201,28 @@ void orderQueryBySortOrder() {
assertEquals(Employee.NAME, orderBy.orderByColumns().get(1).column());
}

@Test
void cellRenderers() {
SwingEntityTableModel tableModel = new SwingEntityTableModel(Employee.TYPE, CONNECTION_PROVIDER);
tableModel.refresh();
FilterTableCellRenderer<String> nameRenderer = EntityTableCellRenderer.builder(Employee.NAME, tableModel).build();
FilterTableCellRenderer<Double> commissionRenderer = EntityTableCellRenderer.builder(Employee.COMMISSION, tableModel).build();
EntityTablePanel tablePanel = new EntityTablePanel(tableModel, config -> config
.cellRenderer(Employee.NAME, nameRenderer)
.cellRendererFactory(new EntityTableCellRenderer.Factory() {
@Override
public <T> FilterTableCellRenderer<T> create(Attribute<T> attribute, SwingEntityTableModel tableModel) {
if (attribute.equals(Employee.COMMISSION)) {
return (FilterTableCellRenderer<T>) commissionRenderer;
}

return EntityTableCellRenderer.builder(attribute, tableModel).build();
}
}));
assertSame(nameRenderer, tablePanel.table().columnModel().column(Employee.NAME).getCellRenderer());
assertSame(commissionRenderer, tablePanel.table().columnModel().column(Employee.COMMISSION).getCellRenderer());
}

private static List<Entity> initTestEntities(Entities entities) {
List<Entity> testEntities = new ArrayList<>(5);
String[] stringValues = new String[] {"a", "b", "c", "d", "e"};
Expand Down

0 comments on commit 2b3c492

Please sign in to comment.