Skip to content

Commit

Permalink
FilterComboBoxModel.ComboBoxItems.validator() removed
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorndarri committed Oct 20, 2024
1 parent 6272755 commit 04456e0
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 44 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
- ItemComboBoxModel no longer extends DefaultFilterComboBoxModel, now a factory class.
- ItemComboBoxModel.Builder along with builder() added, replacing factory methods.
- FilterComboBoxModel.ComboBoxSelection.validPredicate() removed.
- FilterComboBoxModel.ComboBoxItems.validator() removed.
### is.codion.swing.common.ui
- 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,9 @@
import static is.codion.common.value.Value.Notify.WHEN_SET;
import static java.util.Collections.*;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;

final class DefaultFilterComboBoxModel<T> implements FilterComboBoxModel<T> {

private static final Predicate<?> DEFAULT_ITEM_VALIDATOR = new DefaultValidator<>();
private static final Function<Object, ?> DEFAULT_SELECTED_ITEM_TRANSLATOR = new DefaultSelectedItemTranslator<>();
private static final Comparator<?> DEFAULT_COMPARATOR = new DefaultComparator<>();

Expand Down Expand Up @@ -189,10 +187,6 @@ private void includeNullItem(boolean includeNull) {
}

private final class DefaultComboBoxItems implements ComboBoxItems<T> {

private final Value<Predicate<T>> validator = Value.builder()
.nonNull((Predicate<T>) DEFAULT_ITEM_VALIDATOR)
.build();
private final DefaultVisibleItems visible = new DefaultVisibleItems();
private final DefaultFilteredItems filtered = new DefaultFilteredItems();
private final DefaultNullItem nullItem = new DefaultNullItem();
Expand All @@ -202,10 +196,6 @@ private final class DefaultComboBoxItems implements ComboBoxItems<T> {
private boolean cleared = true;

private DefaultComboBoxItems() {
validator.addValidator(validator -> get().stream()
.filter(Objects::nonNull)
.forEach(validator::test));
nullItem.item.addValidator(this::validate);
visible.comparator.addListener(visible::sort);
visible.predicate.addListener(this::filter);
}
Expand All @@ -231,9 +221,7 @@ public void set(Collection<T> items) {
if (nullItem.include.get()) {
visible.items.add(0, null);
}
visible.items.addAll(items.stream()
.map(this::validate)
.collect(toList()));
visible.items.addAll(items);
// Notifies both visible and filtered
filter(false);
cleared = items.isEmpty();
Expand All @@ -243,7 +231,6 @@ public void set(Collection<T> items) {
@Override
public boolean addItem(T item) {
requireNonNull(item);
validate(item);
if (visible.predicate.isNull() || visible.predicate.get().test(item)) {
if (!visible.items.contains(item)) {
visible.items.add(item);
Expand Down Expand Up @@ -327,16 +314,10 @@ public NullItem<T> nullItem() {
return nullItem;
}

@Override
public Value<Predicate<T>> validator() {
return validator;
}

@Override
public void replace(T item, T replacement) {
requireNonNull(item);
requireNonNull(replacement);
validate(replacement);
removeItem(item);
addItem(replacement);
if (Objects.equals(selectionModel.selected.item, item)) {
Expand All @@ -349,14 +330,6 @@ public boolean cleared() {
return cleared;
}

private T validate(T item) {
if (!validator.get().test(item)) {
throw new IllegalArgumentException("Invalid item: " + item);
}

return item;
}

private void filter(boolean filterSelectedItem) {
visible.items.addAll(filtered.items);
filtered.items.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,6 @@ interface NullItem<T> extends Mutable<T> {
*/
interface ComboBoxItems<T> extends Items<T> {

/**
* Provides a way for the model to prevent the addition of certain items.
* Trying to add items that fail validation will result in an exception.
* Note that any translation of the selected item is done before validation.
* @return the {@link Value} controlling the item validator
*/
Value<Predicate<T>> validator();

/**
* Replaces the given item in this combo box model
* @param item the item to replace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,6 @@ public boolean equals(Object o) {
assertEquals("22", model.selection().value().data);
}

@Test
void validator() {
FilterComboBoxModel<Integer> model = new DefaultFilterComboBoxModel<>();
model.items().validator().set(item -> item > 0);
assertThrows(IllegalArgumentException.class, () -> model.items().set(asList(1, 2, 3, 4, 5, 0)));
}

@Test
void items() {
List<Integer> values = asList(0, 1, 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ final class DefaultEntityComboBoxModel implements EntityComboBoxModel {
foreignKeyFilter = new DefaultForeignKeyFilter();
comboBoxModel.selection().filterSelected().set(builder.filterSelected);
comboBoxModel.selection().translator().set(new SelectedItemTranslator());
comboBoxModel.items().validator().set(new ItemValidator());
comboBoxModel.items().visible().predicate().set(foreignKeyFilter.predicate);
if (builder.handleEditEvents) {
addEditListeners();
Expand Down

0 comments on commit 04456e0

Please sign in to comment.