Skip to content

Commit

Permalink
FilterTableSortModel.setSortingEnabled() and isSortingEnabled() repla…
Browse files Browse the repository at this point in the history
…ced with State based sortingEnabled()
  • Loading branch information
bjorndarri committed Oct 24, 2024
1 parent bbecd28 commit 9ea7c67
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 37 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Codion Change Log
- FilterTableSortModel.sortingChanged() renamed observer, now propagates the sorting state instead of a column identifier.
- FilterTableSortModel.sortOrder() replaced with columnSortOrder(), sortPriority() removed, ColumnSortOrder.priority() added.
- FilterTableSortModel.sorted() removed.
- FilterTableSortModel.setSortingEnabled() and isSortingEnabled() replaced with State based sortingEnabled().
### is.codion.swing.framework.model
- EntityComboBoxModel.Filter and ForeignKeyFilter added.
### is.codion.swing.framework.ui
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@

import is.codion.common.event.Event;
import is.codion.common.observer.Observer;
import is.codion.common.state.State;
import is.codion.swing.common.model.component.table.FilterTableModel.Columns;

import javax.swing.SortOrder;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import static java.util.Collections.unmodifiableList;
import static java.util.Objects.requireNonNull;
Expand All @@ -41,7 +40,7 @@ final class DefaultFilterTableSortModel<R, C> implements FilterTableSortModel<R,
private final Map<C, Comparator<?>> columnComparators = new HashMap<>();
private final Event<Boolean> sortingChanged = Event.event();
private final List<ColumnSortOrder<C>> columnSortOrders = new ArrayList<>(0);
private final Set<C> columnSortingDisabled = new HashSet<>();
private final Map<C, State> sortingEnabled = new HashMap<>();
private final RowComparator comparator = new RowComparator();

DefaultFilterTableSortModel(Columns<R, C> columns) {
Expand All @@ -55,7 +54,7 @@ public Comparator<R> comparator() {

@Override
public Optional<ColumnSortOrder<C>> columnSortOrder(C identifier) {
requireNonNull(identifier);
validateIdentifier(identifier);

return columnSortOrders.stream()
.filter(columnSortOrder -> columnSortOrder.identifier().equals(identifier))
Expand Down Expand Up @@ -86,22 +85,10 @@ public void clear() {
}

@Override
public void setSortingEnabled(C identifier, boolean sortingEnabled) {
requireNonNull(identifier);
if (sortingEnabled) {
columnSortingDisabled.remove(identifier);
}
else {
columnSortingDisabled.add(identifier);
if (removeSortOrder(identifier)) {
sortingChanged.accept(!columnSortOrders.isEmpty());
}
}
}
public State sortingEnabled(C identifier) {
validateIdentifier(identifier);

@Override
public boolean isSortingEnabled(C identifier) {
return !columnSortingDisabled.contains(requireNonNull(identifier));
return sortingEnabled.computeIfAbsent(identifier, this::createSortingEnabledState);
}

@Override
Expand All @@ -110,9 +97,9 @@ public Observer<Boolean> observer() {
}

private void setSortOrder(C identifier, SortOrder sortOrder, boolean addColumnToSort) {
requireNonNull(identifier);
validateIdentifier(identifier);
requireNonNull(sortOrder);
if (!isSortingEnabled(identifier)) {
if (sortingEnabled.containsKey(identifier) && !sortingEnabled.get(identifier).get()) {
throw new IllegalStateException("Sorting is disabled for column: " + identifier);
}
if (!addColumnToSort) {
Expand All @@ -131,6 +118,26 @@ private boolean removeSortOrder(C identifier) {
return columnSortOrders.removeIf(columnSortOrder -> columnSortOrder.identifier().equals(identifier));
}

private State createSortingEnabledState(C identifier) {
return State.builder(true)
.consumer(sortingEnabled -> sortingEnabledChanged(identifier, sortingEnabled))
.build();
}

private void sortingEnabledChanged(C identifier, boolean sortingEnabled) {
if (!sortingEnabled) {
if (removeSortOrder(identifier)) {
sortingChanged.accept(!columnSortOrders.isEmpty());
}
}
}

private void validateIdentifier(C identifier) {
if (!columns.identifiers().contains(requireNonNull(identifier))) {
throw new IllegalArgumentException("Column not found: " + identifier);
}
}

private final class RowComparator implements Comparator<R> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package is.codion.swing.common.ui.component.table;

import is.codion.common.observer.Observer;
import is.codion.common.state.State;

import javax.swing.SortOrder;
import java.util.Comparator;
Expand Down Expand Up @@ -47,7 +48,7 @@ public interface FilterTableSortModel<R, C> {
* @see #addSortOrder(Object, SortOrder)
* @see #columnSortOrder()
* @see #columnSortOrder(Object)
* @see #setSortingEnabled(Object, boolean)
* @see #sortingEnabled(Object)
*/
void setSortOrder(C identifier, SortOrder sortOrder);

Expand All @@ -61,7 +62,7 @@ public interface FilterTableSortModel<R, C> {
* @see #setSortOrder(Object, SortOrder)
* @see #columnSortOrder()
* @see #columnSortOrder(Object)
* @see #setSortingEnabled(Object, boolean)
* @see #sortingEnabled(Object)
*/
void addSortOrder(C identifier, SortOrder sortOrder);

Expand All @@ -83,18 +84,12 @@ public interface FilterTableSortModel<R, C> {
void clear();

/**
* Disabling sorting will cause {@link #setSortOrder(Object, SortOrder)} and
* {@link #addSortOrder(Object, SortOrder)} to throw a {@link IllegalStateException} for the given column.
* Disabling sorting for a column will cause {@link #setSortOrder(Object, SortOrder)} and
* {@link #addSortOrder(Object, SortOrder)} to throw a {@link IllegalStateException}.
* @param identifier the column identifier
* @param sortingEnabled true if sorting should be enabled for the given column
* @return the {@link State} controlling whether sorting is enabled for the given column
*/
void setSortingEnabled(C identifier, boolean sortingEnabled);

/**
* @param identifier the column identifier
* @return true if sorting is enabled for the given column
*/
boolean isSortingEnabled(C identifier);
State sortingEnabled(C identifier);

/**
* @return an observer notified each time the sorting changes, with the current sorted state as event data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ public Object value(Row row, Integer identifier) {

model.clear();
model.setSortOrder(2, SortOrder.ASCENDING);
model.setSortingEnabled(2, false);
assertFalse(model.isSortingEnabled(2));
model.sortingEnabled(2).set(false);
assertFalse(model.columnSortOrder(2).isPresent());
assertThrows(IllegalStateException.class, () -> model.setSortOrder(2, SortOrder.DESCENDING));
model.setSortingEnabled(2, true);
assertTrue(model.isSortingEnabled(2));
model.sortingEnabled(2).set(true);
model.setSortOrder(2, SortOrder.DESCENDING);
assertSame(SortOrder.DESCENDING, model.columnSortOrder(2).orElseThrow().sortOrder());

assertThrows(IllegalArgumentException.class, () -> model.setSortOrder(3, SortOrder.ASCENDING));//unknown column
}

@Test
Expand Down

0 comments on commit 9ea7c67

Please sign in to comment.