Skip to content

Commit

Permalink
DefaultFilterTableCellRenderer bug fixed, cell specific background co…
Browse files Browse the repository at this point in the history
…lor now included in alternate row coloring, some refactoring
  • Loading branch information
bjorndarri committed Oct 18, 2024
1 parent 505a32b commit daf62c3
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 57 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Codion Change Log
- DefaultFilterTableCellRenderer.DefaultBuilder bug fixed, useBooleanRenderer now initialized before settings, in order for a correct horizontal alignment for boolean columns.
- FilterTableCellRenderer.Factory.create() T type parameter removed.
- FilterTableColumnModel.identifiers() added.
- DefaultFilterTableCellRenderer bug fixed, cell specific background color now included in alternate row coloring, some refactoring and renaming.
### is.codion.framework.db
- DefaultLocalEntityConnection.delete() bug fixed, database exception now with the correct Operation.
### is.codion.swing.framework.model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
import java.time.temporal.Temporal;
import java.util.function.Function;

import static is.codion.swing.common.ui.Colors.darker;
import static is.codion.swing.common.ui.component.table.FilterTableCellRenderer.DefaultUISettings.DOUBLE_DARKENING_FACTOR;
import static is.codion.swing.common.ui.component.table.FilterTableCellRenderer.DefaultUISettings.blendColors;
import static java.util.Objects.requireNonNull;

final class DefaultFilterTableCellRenderer<R, C, T> extends DefaultTableCellRenderer implements FilterTableCellRenderer<T> {
Expand Down Expand Up @@ -187,7 +190,7 @@ private void update() {
uiSettings.update(leftPadding, rightPadding);
}

private void configure(FilterTable<R, C> filterTable, FilterTableCellRenderer cellRenderer, T value,
private void configure(FilterTable<R, C> filterTable, FilterTableCellRenderer<?> cellRenderer, T value,
boolean isSelected, boolean hasFocus, int rowIndex, int columnIndex) {
requireNonNull(cellRenderer);
requireNonNull(filterTable);
Expand All @@ -212,23 +215,40 @@ else if (cellRenderer instanceof JComponent) {
}

private Color foregroundColor(FilterTable<R, C> filterTable, R row, C identifier, T value) {
return foregroundColor == null ? uiSettings.foreground() : foregroundColor.color(filterTable, row, identifier, value);
Color foreground = foregroundColor.color(filterTable, row, identifier, value);

return foreground == null ? uiSettings.foreground() : foreground;
}

private Color backgroundColor(FilterTable<R, C> filterTable, R row, C identifier, T value, boolean selected, boolean alternateRow) {
Color cellBackgroundColor = null;
if (backgroundColor != null) {
cellBackgroundColor = backgroundColor.color(filterTable, row, identifier, value);
if (alternateRowColoring) {
return backgroundAlternating(filterTable, row, identifier, value, selected, alternateRow);
}
cellBackgroundColor = backgroundColor(cellBackgroundColor, alternateRow, selected);

return backgroundNonAlternating(filterTable, row, identifier, value, selected, alternateRow);
}

private Color backgroundAlternating(FilterTable<R, C> filterTable, R row, C identifier, T value, boolean selected, boolean alternateRow) {
Color cellBackgroundColor = backgroundColor.color(filterTable, row, identifier, value);
cellBackgroundColor = backgroundAlternating(cellBackgroundColor, alternateRow, selected);
if (filterIndicator) {
cellBackgroundColor = uiSettings.background(filterEnabled(filterTable, identifier), alternateRow, cellBackgroundColor);
}
if (cellBackgroundColor != null) {
return cellBackgroundColor;
}
if (alternateRowColoring) {
return alternateRow ? uiSettings.alternateBackground() : uiSettings.background();

return alternateRow ? uiSettings.alternateBackground() : uiSettings.background();
}

private Color backgroundNonAlternating(FilterTable<R, C> filterTable, R row, C identifier, T value, boolean selected, boolean alternateRow) {
Color cellBackgroundColor = backgroundColor.color(filterTable, row, identifier, value);
cellBackgroundColor = backgroundNonAlternating(cellBackgroundColor, selected);
if (filterIndicator) {
cellBackgroundColor = uiSettings.background(filterEnabled(filterTable, identifier), false, cellBackgroundColor);
}
if (cellBackgroundColor != null) {
return cellBackgroundColor;
}
if (uiSettings.alternateRowColor() == null) {
return uiSettings.background();
Expand All @@ -237,6 +257,34 @@ private Color backgroundColor(FilterTable<R, C> filterTable, R row, C identifier
return alternateRow ? uiSettings.alternateRowColor() : uiSettings.background();
}

private Color backgroundAlternating(Color cellBackgroundColor, boolean alternateRow, boolean selected) {
if (cellBackgroundColor != null && alternateRow) {
cellBackgroundColor = darker(cellBackgroundColor, DOUBLE_DARKENING_FACTOR);
}
if (selected) {
Color selectionBackground = alternateRow ? uiSettings.alternateSelectionBackground() : uiSettings.selectionBackground();
if (cellBackgroundColor == null) {
return selectionBackground;
}

return blendColors(cellBackgroundColor, selectionBackground);
}

return cellBackgroundColor;
}

private Color backgroundNonAlternating(Color cellBackgroundColor, boolean selected) {
if (selected) {
if (cellBackgroundColor == null) {
return uiSettings.selectionBackground();
}

return blendColors(cellBackgroundColor, uiSettings.selectionBackground());
}

return cellBackgroundColor;
}

private Border border(FilterTable<?, ?> filterTable, boolean hasFocus, int rowIndex, int columnIndex) {
return hasFocus || isSearchResult(filterTable.searchModel(), rowIndex, columnIndex) ? uiSettings.focusedCellBorder() : uiSettings.defaultCellBorder();
}
Expand All @@ -260,33 +308,19 @@ private static boolean isSearchResult(FilterTableSearchModel searchModel, int ro
private static boolean alternateRow(int rowIndex) {
return rowIndex % 2 != 0;
}

private Color backgroundColor(Color cellBackgroundColor, boolean alternateRow, boolean selected) {
if (selected) {
if (cellBackgroundColor == null) {
return selectionBackgroundColor(alternateRow);
}

return DefaultUISettings.blendColors(cellBackgroundColor, selectionBackgroundColor(alternateRow));
}

return cellBackgroundColor;
}

private Color selectionBackgroundColor(boolean alternateRow) {
return alternateRow ? uiSettings.selectedAlternateBackground() : uiSettings.selectedBackground();
}
}

static final class SettingsBuilder<R, C, T> {

private static final ColorProvider<?, ?, ?> NULL_COLOR_PROVIDER = (table, row, identifier, value) -> null;

private UISettings uiSettings = new DefaultUISettings();
private int leftPadding = TABLE_CELL_LEFT_PADDING.get();
private int rightPadding = TABLE_CELL_RIGHT_PADDING.get();
private boolean alternateRowColoring = ALTERNATE_ROW_COLORING.get();
private boolean filterIndicator = true;
private ColorProvider<R, C, T> backgroundColor;
private ColorProvider<R, C, T> foregroundColor;
private ColorProvider<R, C, T> backgroundColor = (ColorProvider<R, C, T>) NULL_COLOR_PROVIDER;
private ColorProvider<R, C, T> foregroundColor = (ColorProvider<R, C, T>) NULL_COLOR_PROVIDER;
private boolean toolTipData = false;
private Function<T, String> string = new DefaultString<>();
private int horizontalAlignment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ interface Factory<R, C> {

/**
* @param identifier the column identifier
* @param tableModel the table model
* @return a {@link FilterTableCellRenderer} instance for the given column
*/
FilterTableCellRenderer<?> create(C identifier, FilterTableModel<R, C> tableModel);
Expand All @@ -262,34 +263,42 @@ interface Factory<R, C> {
interface UISettings {

/**
* The table foreground color as defined by the {@code Table.foreground} system property
* The table foreground color associated with the {@code Table.foreground} UI key
* @return the foreground color
* @see UIManager#getColor(Object)
* @see UIManager#put(Object, Object)
*/
Color foreground();

/**
* The table background color as defined by the {@code Table.background} system property
* The table background color associated with the {@code Table.background} UI key
* @return the background color
* @see UIManager#getColor(Object)
* @see UIManager#put(Object, Object)
*/
Color background();

/**
* The table alternate row color as defined by the {@code Table.alternateRowColor} system property
* The table alternate row color associated with the {@code Table.alternateRowColor} UI key
* @return the alternate row color, if any
* @see UIManager#getColor(Object)
* @see UIManager#put(Object, Object)
*/
Color alternateRowColor();

/**
* The table selection background color as defined by the {@code Table.selectionBackground} system property
* The table selection background color associated with the {@code Table.selectionBackground} UI key
* @return the selection background color
* @see UIManager#getColor(Object)
* @see UIManager#put(Object, Object)
*/
Color selectedBackground();
Color selectionBackground();

/**
* @return the background color to use for columns with a filter enabled
* @see #filterIndicator()
*/
Color filterBackground();
Color filteredBackground();

/**
* @return the alternate background color
Expand All @@ -299,7 +308,7 @@ interface UISettings {
/**
* @return the alternate background color to use for columns with a filter enabled
*/
Color filterAlternateBackground();
Color alternateFilteredBackground();

/**
* @return the default cell border to use
Expand Down Expand Up @@ -329,7 +338,7 @@ interface UISettings {
/**
* @return the alternate selection background color
*/
Color selectedAlternateBackground();
Color alternateSelectionBackground();

/**
* Updates the colors and border according to the current Look and Feel.
Expand All @@ -352,11 +361,11 @@ class DefaultUISettings implements UISettings {
private Color foreground;
private Color background;
private Color alternateRowColor;
private Color filterBackground;
private Color filterAlternateBackground;
private Color filteredBackground;
private Color alternateFilteredBackground;
private Color alternateBackground;
private Color selectedBackground;
private Color selectedAlternateBackground;
private Color selectionBackground;
private Color alternateSelectionBackground;
private Border defaultCellBorder;
private Border focusedCellBorder;

Expand All @@ -374,10 +383,10 @@ public void update(int leftPadding, int rightPadding) {
if (alternateBackground == null) {
alternateBackground = darker(background, DOUBLE_DARKENING_FACTOR);
}
selectedBackground = UIManager.getColor("Table.selectionBackground");
filterBackground = darker(background, DARKENING_FACTOR);
filterAlternateBackground = darker(alternateBackground, DARKENING_FACTOR);
selectedAlternateBackground = darker(selectedBackground, DARKENING_FACTOR);
selectionBackground = UIManager.getColor("Table.selectionBackground");
filteredBackground = darker(background, DARKENING_FACTOR);
alternateFilteredBackground = darker(alternateBackground, DARKENING_FACTOR);
alternateSelectionBackground = darker(selectionBackground, DARKENING_FACTOR);
defaultCellBorder = leftPadding > 0 || rightPadding > 0 ? createEmptyBorder(0, leftPadding, 0, rightPadding) : null;
focusedCellBorder = createFocusedCellBorder();
}
Expand All @@ -398,13 +407,13 @@ public final Color alternateRowColor() {
}

@Override
public final Color selectedBackground() {
return selectedBackground;
public final Color selectionBackground() {
return selectionBackground;
}

@Override
public final Color filterBackground() {
return filterBackground;
public final Color filteredBackground() {
return filteredBackground;
}

@Override
Expand All @@ -413,13 +422,13 @@ public final Color alternateBackground() {
}

@Override
public final Color filterAlternateBackground() {
return filterAlternateBackground;
public final Color alternateFilteredBackground() {
return alternateFilteredBackground;
}

@Override
public final Color selectedAlternateBackground() {
return selectedAlternateBackground;
public final Color alternateSelectionBackground() {
return alternateSelectionBackground;
}

@Override
Expand Down Expand Up @@ -447,7 +456,7 @@ public final Color filteredBackground(boolean alternateRow, Color cellBackground
return darker(cellBackgroundColor, DARKENING_FACTOR);
}

return alternateRow ? filterAlternateBackground : filterBackground;
return alternateRow ? alternateFilteredBackground : filteredBackground;
}

private CompoundBorder createFocusedCellBorder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private static final class EntityUISettings extends FilterTableCellRenderer.Defa
private final ConditionModel<?> queryCondition;

private Color filteredConditionBackground;
private Color filteredConditionAlternateBackground;
private Color alternateFilteredConditionBackground;

private EntityUISettings(ConditionModel<?> queryCondition) {
this.queryCondition = queryCondition;
Expand All @@ -118,27 +118,27 @@ private EntityUISettings(ConditionModel<?> queryCondition) {
public void update(int leftPadding, int rightPadding) {
super.update(leftPadding, rightPadding);
filteredConditionBackground = darker(background(), DOUBLE_DARKENING_FACTOR);
filteredConditionAlternateBackground = darker(alternateBackground(), DOUBLE_DARKENING_FACTOR);
alternateFilteredConditionBackground = darker(alternateBackground(), DOUBLE_DARKENING_FACTOR);
}

@Override
public Color background(boolean filterEnabled, boolean alternateRow, Color cellBackgroundColor) {
boolean conditionEnabled = queryCondition != null && queryCondition.enabled().get();
if (conditionEnabled || filterEnabled) {
return filterConditionBackground(alternateRow, conditionEnabled && filterEnabled, cellBackgroundColor);
return filtererdConditionBackground(alternateRow, conditionEnabled && filterEnabled, cellBackgroundColor);
}

return cellBackgroundColor;
}

private Color filterConditionBackground(boolean alternateRow, boolean filterAndConditionEnabled, Color cellBackgroundColor) {
private Color filtererdConditionBackground(boolean alternateRow, boolean filterAndConditionEnabled, Color cellBackgroundColor) {
if (cellBackgroundColor != null) {
return darker(cellBackgroundColor, DARKENING_FACTOR);
}

return alternateRow ?
(filterAndConditionEnabled ? filteredConditionAlternateBackground : filterAlternateBackground()) :
(filterAndConditionEnabled ? filteredConditionBackground : filterBackground());
(filterAndConditionEnabled ? alternateFilteredConditionBackground : alternateFilteredBackground()) :
(filterAndConditionEnabled ? filteredConditionBackground : filteredBackground());
}
}
}

0 comments on commit daf62c3

Please sign in to comment.