FavoritesSection.java

package ui.entities.config;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlTransient;
import javafx.beans.property.*;
import sidplay.ini.converter.BeanToStringConverter;
import ui.common.properties.LazyListField;
import ui.common.properties.ObservableLazyListField;
import ui.common.properties.ShadowField;
import ui.entities.collection.HVSCEntry;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static java.util.stream.Collectors.toList;
import static ui.entities.collection.HVSCEntry_.*;

@Entity
@Access(AccessType.PROPERTY)
public class FavoritesSection {

    public static final List<HVSCEntry> DEFAULT_FAVORITES = Collections.emptyList();

    public static List<FavoriteColumn> DEFAULT_COLUMNS;

    private Integer id;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @XmlTransient
    @JsonIgnore
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    private ShadowField<StringProperty, String> name = new ShadowField<>(SimpleStringProperty::new, null);

    public String getName() {
        return name.get();
    }

    public void setName(String name) {
        this.name.set(name);
    }

    public final StringProperty nameProperty() {
        return name.property();
    }

    private ShadowField<DoubleProperty, Number> width = new ShadowField<>(
            number -> new SimpleDoubleProperty(number.doubleValue()), 0);

    public Double getWidth() {
        return width.get().doubleValue();
    }

    public void setWidth(Double width) {
        this.width.set(width);
    }

    public final DoubleProperty widthProperty() {
        return width.property();
    }

    private ShadowField<IntegerProperty, Number> selectedRowFrom = new ShadowField<>(
            number -> new SimpleIntegerProperty(number.intValue()), -1);

    public Integer getSelectedRowFrom() {
        return selectedRowFrom.get().intValue();
    }

    public void setSelectedRowFrom(Integer selectedRowFrom) {
        this.selectedRowFrom.set(selectedRowFrom);
    }

    public final IntegerProperty selectedRowFromProperty() {
        return selectedRowFrom.property();
    }

    private ShadowField<IntegerProperty, Number> selectedRowTo = new ShadowField<>(
            number -> new SimpleIntegerProperty(number.intValue()), -1);

    public Integer getSelectedRowTo() {
        return selectedRowTo.get().intValue();
    }

    public void setSelectedRowTo(Integer selectedRowTo) {
        this.selectedRowTo.set(selectedRowTo);
    }

    public final IntegerProperty selectedRowToProperty() {
        return selectedRowTo.property();
    }

    private LazyListField<FavoriteColumn> columns = new LazyListField<>();

    @OneToMany(cascade = CascadeType.ALL)
    public List<FavoriteColumn> getColumns() {
        // Singular attributes cannot be resolved that early, therefore, lazy:
        if (DEFAULT_COLUMNS == null) {
            DEFAULT_COLUMNS = Arrays.asList(new FavoriteColumn(title), new FavoriteColumn(author),
                    new FavoriteColumn(released));
        }
        return columns.get(() -> DEFAULT_COLUMNS.stream().map(FavoriteColumn::new).collect(toList()));
    }

    public void setColumns(List<FavoriteColumn> columns) {
        this.columns.set(columns);
    }

    private ObservableLazyListField<HVSCEntry> favorites = new ObservableLazyListField<>();

    @OneToMany(cascade = CascadeType.ALL)
    @XmlElement(name = "favorite")
    public List<HVSCEntry> getFavorites() {
        return favorites.get(() -> new ArrayList<>(DEFAULT_FAVORITES));
    }

    public void setFavorites(List<HVSCEntry> favorites) {
        this.favorites.set(favorites);
    }

    @Override
    public final String toString() {
        return BeanToStringConverter.toString(this);
    }
}