package app.navigation.regular.components.button; import app.data.entity.Matchday; import app.navigation.regular.RegularNavigation; import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.icon.VaadinIcon; import java.util.Optional; public class PrevMatchdayButton extends Button { private final RegularNavigation regularNavigation; public PrevMatchdayButton(RegularNavigation regularNavigation) { this.regularNavigation = regularNavigation; if (!regularNavigation.matchdayEnabled()) throw new IllegalStateException("Cannot instantiate PrevMatchdayButton when Matchdays are not enabled!"); setIcon(VaadinIcon.ARROW_LEFT.create()); regularNavigation.addRunnableToBeRunAfterSelection(this::configure); } private void configure() { Optional prevMatchday = getPrevMatchday(); setEnabled(prevMatchday.isPresent()); addClickListener(event -> prevMatchday.ifPresent(matchday -> regularNavigation.getMatchdaySelect().setValue(matchday))); } private Optional getPrevMatchday() { int index = MatchdayButtonUtils.getMatchdayIndex(regularNavigation); if (index > 0) return Optional.ofNullable(regularNavigation.getMatchdayList().get(index - 1)); return Optional.empty(); } }