Configuration.java

package ui.entities.config;

import com.beust.jcommander.ParametersDelegate;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.XmlTransient;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.input.KeyCode;
import libsidplay.config.IConfig;
import libsidplay.config.IConsoleSection;
import sidplay.ini.converter.BeanToStringConverter;
import ui.common.properties.LazyListField;
import ui.common.properties.ObservableLazyListField;
import ui.common.properties.ShadowField;
import ui.console.Console;
import ui.videoscreen.Video;

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 libsidplay.components.keyboard.KeyTableEntry.*;
import static sidplay.ini.IniDefaults.DEFAULT_FILTER_SECTION;
import static ui.entities.config.KeyTableEntity.of;

/**
 *
 * Configuration of the UI version of JSIDPlay2.
 *
 * @author ken
 *
 */
@Entity
@Access(AccessType.PROPERTY)
@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Configuration implements IConfig {

    public static final List<FilterSection> DEFAULT_FILTERS = DEFAULT_FILTER_SECTION.stream().map(FilterSection::new)
            .collect(toList());

    public static final List<ViewEntity> DEFAULT_VIEWS = Arrays.asList(new ViewEntity(Console.ID),
            new ViewEntity(Video.ID));

    public static final List<FavoritesSection> DEFAULT_FAVORITES = Collections.singletonList(new FavoritesSection());

    public static final List<KeyTableEntity> DEFAULT_KEYCODES = Arrays.asList(of(KeyCode.A.getName(), A),
            of(KeyCode.BACK_SLASH.getName(), ARROW_LEFT), of(KeyCode.DIGIT1.getName(), ONE),
            of(KeyCode.DIGIT2.getName(), TWO), of(KeyCode.DIGIT3.getName(), THREE), of(KeyCode.DIGIT4.getName(), FOUR),
            of(KeyCode.DIGIT5.getName(), FIVE), of(KeyCode.DIGIT6.getName(), SIX), of(KeyCode.DIGIT7.getName(), SEVEN),
            of(KeyCode.DIGIT8.getName(), EIGHT), of(KeyCode.DIGIT9.getName(), NINE), of(KeyCode.DIGIT0.getName(), ZERO),
            of(KeyCode.OPEN_BRACKET.getName(), PLUS), of(KeyCode.CLOSE_BRACKET.getName(), MINUS),
            of(KeyCode.POUND.getName(), POUND), of(KeyCode.HOME.getName(), CLEAR_HOME),
            of(KeyCode.BACK_SPACE.getName(), INS_DEL),

            of(KeyCode.Q.getName(), Q), of(KeyCode.W.getName(), W), of(KeyCode.E.getName(), E),
            of(KeyCode.R.getName(), R), of(KeyCode.T.getName(), T), of(KeyCode.Y.getName(), Y),
            of(KeyCode.U.getName(), U), of(KeyCode.I.getName(), I), of(KeyCode.O.getName(), O),
            of(KeyCode.P.getName(), P), of(KeyCode.SEMICOLON.getName(), AT), of(KeyCode.PLUS.getName(), STAR),
            of(KeyCode.LESS.getName(), ARROW_UP),

            of(KeyCode.ESCAPE.getName(), RUN_STOP), of(KeyCode.A.getName(), A), of(KeyCode.S.getName(), S),
            of(KeyCode.D.getName(), D), of(KeyCode.F.getName(), F), of(KeyCode.G.getName(), G),
            of(KeyCode.H.getName(), H), of(KeyCode.J.getName(), J), of(KeyCode.K.getName(), K),
            of(KeyCode.L.getName(), L), of(KeyCode.BACK_QUOTE.getName(), COLON), of(KeyCode.QUOTE.getName(), SEMICOLON),
            of(KeyCode.SLASH.getName(), EQUALS), of(KeyCode.ENTER.getName(), RETURN),

            of(KeyCode.Z.getName(), Z), of(KeyCode.X.getName(), X), of(KeyCode.C.getName(), C),
            of(KeyCode.V.getName(), V), of(KeyCode.B.getName(), B), of(KeyCode.N.getName(), N),
            of(KeyCode.M.getName(), M), of(KeyCode.COMMA.getName(), COMMA), of(KeyCode.PERIOD.getName(), PERIOD),
            of(KeyCode.MINUS.getName(), SLASH), of(KeyCode.DOWN.getName(), CURSOR_UP_DOWN),
            of(KeyCode.UP.getName(), CURSOR_UP_DOWN), of(KeyCode.RIGHT.getName(), CURSOR_LEFT_RIGHT),
            of(KeyCode.LEFT.getName(), CURSOR_LEFT_RIGHT),

            of(KeyCode.SPACE.getName(), SPACE),

            of(KeyCode.F1.getName(), F1), of(KeyCode.F3.getName(), F3), of(KeyCode.F5.getName(), F5),
            of(KeyCode.F7.getName(), F7));

    private Integer id;

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

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

    private SidPlay2Section sidplay2 = new SidPlay2Section();

    @Embedded
    @Override
    public SidPlay2Section getSidplay2Section() {
        return sidplay2;
    }

    public void setSidplay2Section(SidPlay2Section sidplay2) {
        this.sidplay2 = sidplay2;
    }

    private OnlineSection online = new OnlineSection();

    @Embedded
    public OnlineSection getOnlineSection() {
        return online;
    }

    public void setOnlineSection(OnlineSection online) {
        this.online = online;
    }

    private C1541Section c1541 = new C1541Section();

    @Override
    @Embedded
    public C1541Section getC1541Section() {
        return c1541;
    }

    public void setC1541Section(C1541Section c1541) {
        this.c1541 = c1541;
    }

    private PrinterSection printer = new PrinterSection();

    @Embedded
    @Override
    public PrinterSection getPrinterSection() {
        return printer;
    }

    public void setPrinterSection(PrinterSection printer) {
        this.printer = printer;
    }

    private ConsoleSection consoleSection = new ConsoleSection();

    @Transient
    @XmlTransient
    @JsonIgnore
    @Override
    public IConsoleSection getConsoleSection() {
        return consoleSection;
    }

    public void setConsoleSection(ConsoleSection consoleSection) {
        this.consoleSection = consoleSection;
    }

    private JoystickSection joystickSection = new JoystickSection();

    @Embedded
    public JoystickSection getJoystickSection() {
        return joystickSection;
    }

    public void setJoystickSection(JoystickSection joystick) {
        this.joystickSection = joystick;
    }

    private AudioSection audioSection = new AudioSection();

    @Embedded
    @Override
    public AudioSection getAudioSection() {
        return audioSection;
    }

    public void setAudioSection(AudioSection audio) {
        this.audioSection = audio;
    }

    @ParametersDelegate
    private EmulationSection emulationSection = new EmulationSection();

    @Embedded
    @Override
    public EmulationSection getEmulationSection() {
        return emulationSection;
    }

    public void setEmulationSection(EmulationSection emulation) {
        this.emulationSection = emulation;
    }

    @ParametersDelegate
    private WhatsSidSection whatsSidSection = new WhatsSidSection();

    @Embedded
    @Override
    public WhatsSidSection getWhatsSidSection() {
        return whatsSidSection;
    }

    public void setWhatsSidSection(WhatsSidSection whatsSidSection) {
        this.whatsSidSection = whatsSidSection;
    }

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

    public String getCurrentFavorite() {
        return currentFavorite.get();
    }

    public void setCurrentFavorite(String currentFavorite) {
        this.currentFavorite.set(currentFavorite);
    }

    public final StringProperty currentFavoriteProperty() {
        return currentFavorite.property();
    }

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

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

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

    private Assembly64Section assembly64Section = new Assembly64Section();

    @Embedded
    public Assembly64Section getAssembly64Section() {
        return assembly64Section;
    }

    public void setAssembly64Section(Assembly64Section assembly64Section) {
        this.assembly64Section = assembly64Section;
    }

    private ObservableLazyListField<ViewEntity> views = new ObservableLazyListField<>();

    @OneToMany(cascade = CascadeType.ALL)
    public List<ViewEntity> getViews() {
        return views.get(() -> new ArrayList<>(DEFAULT_VIEWS));
    }

    public void setViews(List<ViewEntity> views) {
        this.views.set(views);
    }

    private LazyListField<FilterSection> filter = new LazyListField<>();

    @OneToMany(cascade = CascadeType.ALL)
    @Override
    public List<FilterSection> getFilterSection() {
        return filter.get(() -> DEFAULT_FILTERS.stream().map(FilterSection::new).collect(toList()));
    }

    public void setFilterSection(List<FilterSection> filter) {
        this.filter.set(filter);
    }

    private LazyListField<KeyTableEntity> keyCodeMap = new LazyListField<>();

    @OneToMany(cascade = CascadeType.ALL)
    public List<KeyTableEntity> getKeyCodeMap() {
        return keyCodeMap.get(() -> DEFAULT_KEYCODES.stream().map(KeyTableEntity::new).collect(toList()));
    }

    public void setKeyCodeMap(List<KeyTableEntity> keyCodeMap) {
        this.keyCodeMap.set(keyCodeMap);
    }

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