package app.navigation.player.components.button; import app.data.entity.Player; import app.navigation.player.PlayerNavigation; import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.icon.VaadinIcon; import java.util.List; import java.util.Optional; public class PrevPlayerButton extends Button { private final PlayerNavigation playerNavigation; public PrevPlayerButton(PlayerNavigation playerNavigation) { this.playerNavigation = playerNavigation; setIcon(VaadinIcon.ARROW_LEFT.create()); playerNavigation.addRunnableToBeRunAfterSelection(this::configure); } private void configure() { Optional prevPlayer = getPrevPlayer(); setEnabled(prevPlayer.isPresent()); addClickListener(event -> prevPlayer.ifPresent(player -> playerNavigation.getPlayerSelect().setValue(player))); } private Optional getPrevPlayer() { int index = PlayerButtonUtils.getPlayerIndex(playerNavigation); if (index < 0) return Optional.empty(); List playerList = playerNavigation.getPlayerList(); return Optional.ofNullable(playerList.get((index - 1) % playerList.size())); } }