Skip to content

Commit

Permalink
FilterComboBoxModel.ComboBoxSelection.validPredicate() removed
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorndarri committed Oct 20, 2024
1 parent f85c546 commit 6272755
Show file tree
Hide file tree
Showing 4 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 @@ -10,6 +10,7 @@ Codion Change Log
- DefaultFilterTableModel.VisiblePredicate.predicate bug fixed, now Notify.WHEN_SET.
- ItemComboBoxModel no longer extends DefaultFilterComboBoxModel, now a factory class.
- ItemComboBoxModel.Builder along with builder() added, replacing factory methods.
- FilterComboBoxModel.ComboBoxSelection.validPredicate() 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 @@ -51,7 +51,6 @@ 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 Predicate<?> DEFAULT_VALID_SELECTION_PREDICATE = new DefaultValidSelectionPredicate<>();
private static final Comparator<?> DEFAULT_COMPARATOR = new DefaultComparator<>();

private final DefaultComboBoxSelection selectionModel = new DefaultComboBoxSelection();
Expand Down Expand Up @@ -580,11 +579,6 @@ public Value<Function<Object, T>> translator() {
return selected.translator;
}

@Override
public Value<Predicate<T>> validPredicate() {
return selected.valid;
}

@Override
public State filterSelected() {
return filterSelectedItem;
Expand All @@ -599,20 +593,9 @@ private final class Selected implements Mutable<T> {
private final Value<Function<Object, T>> translator = Value.builder()
.nonNull((Function<Object, T>) DEFAULT_SELECTED_ITEM_TRANSLATOR)
.build();
private final Value<Predicate<T>> valid = Value.builder()
.nonNull((Predicate<T>) DEFAULT_VALID_SELECTION_PREDICATE)
.build();

private T item = null;

private Selected() {
valid.addValidator(predicate -> {
if (predicate != null && !predicate.test(item)) {
throw new IllegalArgumentException("The current selected item does not satisfy the valid selection predicate");
}
});
}

@Override
public T get() {
if (item == null) {
Expand All @@ -634,7 +617,7 @@ public Observer<T> observer() {

private void setSelectedItem(Object item) {
T toSelect = translator.get().apply(Objects.equals(modelItems.nullItem.get(), item) ? null : item);
if (!Objects.equals(this.item, toSelect) && valid.get().test(toSelect)) {
if (!Objects.equals(this.item, toSelect)) {
changing.accept(toSelect);
this.item = toSelect;
empty.set(selectionModel.value() == null);
Expand Down Expand Up @@ -713,14 +696,6 @@ public Collection<T> get() {
}
}

private static final class DefaultValidSelectionPredicate<T> implements Predicate<T> {

@Override
public boolean test(T item) {
return true;
}
}

private static final class DefaultComparator<T> implements Comparator<T> {

private final Comparator<T> comparator = Text.collator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,6 @@ interface ComboBoxSelection<T> extends SingleItemSelection<T> {
*/
Value<Function<Object, T>> translator();

/**
* Provides a way for the combo box model to prevent the selection of certain items.
* @return the {@link Value} controlling the valid selection predicate
*/
Value<Predicate<T>> validPredicate();

/**
* Specifies whether filtering the model affects the currently selected item.
* If true, the selection is cleared when the selected item is filtered from
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,18 +343,6 @@ void items() {
assertTrue(values.containsAll(model.items().get()));
}

@Test
void validSelectionPredicate() {
FilterComboBoxModel<Integer> model = new DefaultFilterComboBoxModel<>();
model.items().set(asList(0, 1, 2));
model.setSelectedItem(0);
assertThrows(IllegalArgumentException.class, () -> model.selection().validPredicate().set(item -> item > 0));
model.setSelectedItem(1);
model.selection().validPredicate().set(item -> item > 0);
model.setSelectedItem(0);
assertEquals(1, model.getSelectedItem());
}

@BeforeEach
void setUp() {
testModel = new DefaultFilterComboBoxModel<>();
Expand Down

0 comments on commit 6272755

Please sign in to comment.