| @ -0,0 +1,4 @@ | |||
| package app.navigation; | |||
| public interface Navigable { | |||
| } | |||
| @ -0,0 +1,5 @@ | |||
| package app.navigation; | |||
| public interface NavigableService<T extends Navigable> { | |||
| Class<T> getNavigableClass(); | |||
| } | |||
| @ -0,0 +1,127 @@ | |||
| package app.navigation.match; | |||
| import app.data.entity.Match; | |||
| import app.data.entity.Matchday; | |||
| import app.data.entity.Season; | |||
| import app.data.service.MatchService; | |||
| import app.data.service.MatchdayService; | |||
| import app.data.service.SeasonService; | |||
| import app.navigation.Navigable; | |||
| import app.navigation.Navigation; | |||
| import com.vaadin.flow.component.select.Select; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.lang.NonNull; | |||
| import org.springframework.lang.Nullable; | |||
| import java.util.List; | |||
| import java.util.Optional; | |||
| public class MatchNavigation extends Navigation { | |||
| private boolean onlyMatchdaysWithActivity; | |||
| public MatchNavigation(@Autowired SeasonService seasonService, | |||
| @Autowired MatchdayService matchdayService, | |||
| @Autowired MatchService matchService) { | |||
| super(seasonService, matchdayService, matchService); | |||
| } | |||
| public void setOnlyMatchdaysWithActivity(boolean onlyMatchdaysWithActivity) { | |||
| this.onlyMatchdaysWithActivity = onlyMatchdaysWithActivity; | |||
| } | |||
| @Override | |||
| @SuppressWarnings("unchecked") | |||
| protected <PARENT extends Navigable, CHILD extends Navigable> List<CHILD> getChildren(@Nullable PARENT parent, @NonNull Class<CHILD> childClass) { | |||
| if (childClass.equals(Season.class)) { | |||
| return (List<CHILD>) getSeasonService().getAllSeasonsSorted(); | |||
| } | |||
| if (childClass.equals(Matchday.class)) { | |||
| assert parent != null; | |||
| if (onlyMatchdaysWithActivity) | |||
| return (List<CHILD>) getMatchdayService().getMatchdaysWithActivitySortedOrElseListWithOnlyFirstMatchday((Season) parent); | |||
| return (List<CHILD>) getMatchdayService().getMatchdaysSorted((Season) parent); | |||
| } | |||
| if (childClass.equals(Match.class)) { | |||
| assert parent != null; | |||
| return (List<CHILD>) getMatchService().getMatches((Matchday) parent); | |||
| } | |||
| throw new UnsupportedOperationException(String.format("This method is not supported for childClass %s", childClass.getSimpleName())); | |||
| } | |||
| @Override | |||
| @SuppressWarnings("unchecked") | |||
| protected <T extends Navigable> T getDefaultValue(Class<T> clazz) { | |||
| if (clazz.equals(Season.class)) { | |||
| assert !getSeasonList().isEmpty(); | |||
| return (T) getSeasonList().get(getSeasonList().size() - 1); | |||
| } | |||
| if (clazz.equals(Matchday.class)) { | |||
| assert !getMatchdayList().isEmpty(); | |||
| Matchday matchdayToSelect = getMatchdayList().get(0); | |||
| for (Matchday matchday : getMatchdayList()) | |||
| if (getMatchdayService().hasActivity(matchday)) matchdayToSelect = matchday; | |||
| return (T) matchdayToSelect; | |||
| } | |||
| if (clazz.equals(Match.class)) { | |||
| assert !getMatchList().isEmpty(); | |||
| return (T) getMatchList().get(0); | |||
| } | |||
| throw new UnsupportedOperationException(String.format("This method is not supported for clazz %s", clazz.getSimpleName())); | |||
| } | |||
| public SeasonService getSeasonService() { | |||
| return (SeasonService) serviceMap.get(Season.class); | |||
| } | |||
| public MatchdayService getMatchdayService() { | |||
| return (MatchdayService) serviceMap.get(Matchday.class); | |||
| } | |||
| public MatchService getMatchService() { | |||
| return (MatchService) serviceMap.get(Match.class); | |||
| } | |||
| @SuppressWarnings("unchecked") | |||
| public List<Season> getSeasonList() { | |||
| return (List<Season>) listMap.get(Season.class); | |||
| } | |||
| @SuppressWarnings("unchecked") | |||
| public List<Matchday> getMatchdayList() { | |||
| return (List<Matchday>) listMap.get(Matchday.class); | |||
| } | |||
| @SuppressWarnings("unchecked") | |||
| public List<Match> getMatchList() { | |||
| return (List<Match>) listMap.get(Match.class); | |||
| } | |||
| @SuppressWarnings("unchecked") | |||
| public Select<Season> getSeasonSelect() { | |||
| return (Select<Season>) selectMap.get(Season.class); | |||
| } | |||
| @SuppressWarnings("unchecked") | |||
| public Select<Matchday> getMatchdaySelect() { | |||
| return (Select<Matchday>) selectMap.get(Matchday.class); | |||
| } | |||
| @SuppressWarnings("unchecked") | |||
| public Select<Match> getMatchSelect() { | |||
| return (Select<Match>) selectMap.get(Match.class); | |||
| } | |||
| public Optional<Matchday> getSelectedMatchday() { | |||
| return getMatchdaySelect().getOptionalValue(); | |||
| } | |||
| public Optional<Season> getSelectedSeason() { | |||
| return getSeasonSelect().getOptionalValue(); | |||
| } | |||
| public Optional<Match> getSelectedMatch() { | |||
| return getMatchSelect().getOptionalValue(); | |||
| } | |||
| } | |||
| @ -0,0 +1,37 @@ | |||
| package app.navigation.match; | |||
| import app.data.service.MatchService; | |||
| import app.data.service.MatchdayService; | |||
| import app.data.service.SeasonService; | |||
| import app.navigation.AbstractNavigationHeader; | |||
| import app.navigation.NavigationService; | |||
| import app.navigation.match.components.MatchNavigationHeader; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Service; | |||
| @Service | |||
| public class MatchNavigationService implements NavigationService<MatchNavigation> { | |||
| private final SeasonService seasonService; | |||
| private final MatchdayService matchdayService; | |||
| private final MatchService matchService; | |||
| public MatchNavigationService(@Autowired SeasonService seasonService, | |||
| @Autowired MatchdayService matchdayService, | |||
| @Autowired MatchService matchService) { | |||
| this.seasonService = seasonService; | |||
| this.matchdayService = matchdayService; | |||
| this.matchService = matchService; | |||
| } | |||
| @Override | |||
| public MatchNavigation getNewNavigation() { | |||
| return new MatchNavigation(seasonService, matchdayService, matchService); | |||
| } | |||
| @Override | |||
| public AbstractNavigationHeader<MatchNavigation> getNewNavigationHeader(MatchNavigation navigation) { | |||
| return new MatchNavigationHeader(navigation); | |||
| } | |||
| } | |||
| @ -0,0 +1,20 @@ | |||
| package app.navigation.match.components; | |||
| import app.navigation.AbstractNavigationHeader; | |||
| import app.navigation.match.MatchNavigation; | |||
| import com.vaadin.flow.component.html.Label; | |||
| public class MatchNavigationHeader extends AbstractNavigationHeader<MatchNavigation> { | |||
| public MatchNavigationHeader(MatchNavigation navigation) { | |||
| super(navigation); | |||
| } | |||
| @Override | |||
| protected void defineChildren() { | |||
| removeAll(); | |||
| add(new Label("Season:"), navigation.getSeasonSelect()); | |||
| add(new Label("Matchday:"), navigation.getMatchdaySelect()); | |||
| add(new Label("Match:"), navigation.getMatchSelect()); | |||
| } | |||
| } | |||
| @ -0,0 +1,99 @@ | |||
| package app.navigation.matchday; | |||
| import app.data.entity.Match; | |||
| import app.data.entity.Matchday; | |||
| import app.data.entity.Season; | |||
| import app.data.service.MatchService; | |||
| import app.data.service.MatchdayService; | |||
| import app.data.service.SeasonService; | |||
| import app.navigation.Navigable; | |||
| import app.navigation.Navigation; | |||
| import com.vaadin.flow.component.select.Select; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.lang.NonNull; | |||
| import org.springframework.lang.Nullable; | |||
| import java.util.List; | |||
| import java.util.Optional; | |||
| public class MatchdayNavigation extends Navigation { | |||
| private boolean onlyMatchdaysWithActivity; | |||
| MatchdayNavigation(@Autowired SeasonService seasonService, | |||
| @Autowired MatchdayService matchdayService) { | |||
| super(seasonService, matchdayService); | |||
| } | |||
| public void setOnlyMatchdaysWithActivity(boolean onlyMatchdaysWithActivity) { | |||
| this.onlyMatchdaysWithActivity = onlyMatchdaysWithActivity; | |||
| } | |||
| @Override | |||
| @SuppressWarnings({"unchecked", "DuplicatedCode"}) | |||
| protected <PARENT extends Navigable, CHILD extends Navigable> List<CHILD> getChildren(@Nullable PARENT parent, @NonNull Class<CHILD> childClass) { | |||
| if (childClass.equals(Season.class)) { | |||
| return (List<CHILD>) getSeasonService().getAllSeasonsSorted(); | |||
| } | |||
| if (childClass.equals(Matchday.class)) { | |||
| assert parent != null; | |||
| if (onlyMatchdaysWithActivity) | |||
| return (List<CHILD>) getMatchdayService().getMatchdaysWithActivitySortedOrElseListWithOnlyFirstMatchday((Season) parent); | |||
| return (List<CHILD>) getMatchdayService().getMatchdaysSorted((Season) parent); | |||
| } | |||
| throw new UnsupportedOperationException(String.format("This method is not supported for childClass %s", childClass.getSimpleName())); | |||
| } | |||
| @Override | |||
| @SuppressWarnings({"unchecked", "DuplicatedCode"}) | |||
| protected <T extends Navigable> T getDefaultValue(Class<T> clazz) { | |||
| if (clazz.equals(Season.class)) { | |||
| assert !getSeasonList().isEmpty(); | |||
| return (T) getSeasonList().get(getSeasonList().size() - 1); | |||
| } | |||
| if (clazz.equals(Matchday.class)) { | |||
| assert !getMatchdayList().isEmpty(); | |||
| Matchday matchdayToSelect = getMatchdayList().get(0); | |||
| for (Matchday matchday : getMatchdayList()) | |||
| if (getMatchdayService().hasActivity(matchday)) matchdayToSelect = matchday; | |||
| return (T) matchdayToSelect; | |||
| } | |||
| throw new UnsupportedOperationException(String.format("This method is not supported for clazz %s", clazz.getSimpleName())); | |||
| } | |||
| public SeasonService getSeasonService() { | |||
| return (SeasonService) serviceMap.get(Season.class); | |||
| } | |||
| public MatchdayService getMatchdayService() { | |||
| return (MatchdayService) serviceMap.get(Matchday.class); | |||
| } | |||
| @SuppressWarnings("unchecked") | |||
| public List<Season> getSeasonList() { | |||
| return (List<Season>) listMap.get(Season.class); | |||
| } | |||
| @SuppressWarnings("unchecked") | |||
| public List<Matchday> getMatchdayList() { | |||
| return (List<Matchday>) listMap.get(Matchday.class); | |||
| } | |||
| @SuppressWarnings("unchecked") | |||
| public Select<Season> getSeasonSelect() { | |||
| return (Select<Season>) selectMap.get(Season.class); | |||
| } | |||
| @SuppressWarnings("unchecked") | |||
| public Select<Matchday> getMatchdaySelect() { | |||
| return (Select<Matchday>) selectMap.get(Matchday.class); | |||
| } | |||
| public Optional<Matchday> getSelectedMatchday() { | |||
| return getMatchdaySelect().getOptionalValue(); | |||
| } | |||
| public Optional<Season> getSelectedSeason() { | |||
| return getSeasonSelect().getOptionalValue(); | |||
| } | |||
| } | |||
| @ -0,0 +1,33 @@ | |||
| package app.navigation.matchday; | |||
| import app.data.service.MatchService; | |||
| import app.data.service.MatchdayService; | |||
| import app.data.service.SeasonService; | |||
| import app.navigation.AbstractNavigationHeader; | |||
| import app.navigation.NavigationService; | |||
| import app.navigation.matchday.components.MatchdayNavigationHeader; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Service; | |||
| @Service | |||
| public class MatchdayNavigationService implements NavigationService<MatchdayNavigation> { | |||
| private final SeasonService seasonService; | |||
| private final MatchdayService matchdayService; | |||
| public MatchdayNavigationService(@Autowired SeasonService seasonService, | |||
| @Autowired MatchdayService matchdayService) { | |||
| this.seasonService = seasonService; | |||
| this.matchdayService = matchdayService; | |||
| } | |||
| @Override | |||
| public MatchdayNavigation getNewNavigation() { | |||
| return new MatchdayNavigation(seasonService, matchdayService); | |||
| } | |||
| @Override | |||
| public AbstractNavigationHeader<MatchdayNavigation> getNewNavigationHeader(MatchdayNavigation navigation) { | |||
| return new MatchdayNavigationHeader(navigation); | |||
| } | |||
| } | |||
| @ -0,0 +1,19 @@ | |||
| package app.navigation.matchday.components; | |||
| import app.navigation.AbstractNavigationHeader; | |||
| import app.navigation.matchday.MatchdayNavigation; | |||
| import com.vaadin.flow.component.html.Label; | |||
| public class MatchdayNavigationHeader extends AbstractNavigationHeader<MatchdayNavigation> { | |||
| public MatchdayNavigationHeader(MatchdayNavigation navigation) { | |||
| super(navigation); | |||
| } | |||
| @Override | |||
| protected void defineChildren() { | |||
| removeAll(); | |||
| add(new Label("Season:"), navigation.getSeasonSelect()); | |||
| add(new Label("Matchday:"), navigation.getMatchdaySelect()); | |||
| } | |||
| } | |||
| @ -0,0 +1,14 @@ | |||
| package app.navigation.matchday.components.button; | |||
| import app.navigation.matchday.MatchdayNavigation; | |||
| import java.util.concurrent.atomic.AtomicInteger; | |||
| class MatchdayButtonUtils { | |||
| static int getMatchdayIndex(MatchdayNavigation matchdayNavigation) { | |||
| AtomicInteger index = new AtomicInteger(-1); | |||
| matchdayNavigation.getSelectedMatchday().ifPresent(matchday -> index.set(matchdayNavigation.getMatchdayList().indexOf(matchday))); | |||
| return index.get(); | |||
| } | |||
| } | |||
| @ -0,0 +1,35 @@ | |||
| package app.navigation.matchday.components.button; | |||
| import app.data.entity.Matchday; | |||
| import app.navigation.match.MatchNavigation; | |||
| import app.navigation.matchday.MatchdayNavigation; | |||
| import com.vaadin.flow.component.button.Button; | |||
| import com.vaadin.flow.component.icon.VaadinIcon; | |||
| import java.util.Optional; | |||
| public class NextMatchdayButton extends Button { | |||
| private final MatchdayNavigation matchdayNavigation; | |||
| public NextMatchdayButton(MatchdayNavigation matchdayNavigation) { | |||
| this.matchdayNavigation = matchdayNavigation; | |||
| setIcon(VaadinIcon.ARROW_RIGHT.create()); | |||
| matchdayNavigation.addRunnableToBeRunAfterSelection(this::configure); | |||
| } | |||
| private void configure() { | |||
| Optional<Matchday> nextMatchday = getNextMatchday(); | |||
| setEnabled(nextMatchday.isPresent()); | |||
| addClickListener(event -> nextMatchday.ifPresent(matchday -> matchdayNavigation.getMatchdaySelect().setValue(matchday))); | |||
| } | |||
| private Optional<Matchday> getNextMatchday() { | |||
| int index = MatchdayButtonUtils.getMatchdayIndex(matchdayNavigation); | |||
| if (index >= 0 && index < matchdayNavigation.getMatchdayList().size() - 1) | |||
| return Optional.ofNullable(matchdayNavigation.getMatchdayList().get(index + 1)); | |||
| return Optional.empty(); | |||
| } | |||
| } | |||
| @ -0,0 +1,34 @@ | |||
| package app.navigation.matchday.components.button; | |||
| import app.data.entity.Matchday; | |||
| import app.navigation.match.MatchNavigation; | |||
| import app.navigation.matchday.MatchdayNavigation; | |||
| 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 MatchdayNavigation matchdayNavigation; | |||
| public PrevMatchdayButton(MatchdayNavigation matchdayNavigation) { | |||
| this.matchdayNavigation = matchdayNavigation; | |||
| setIcon(VaadinIcon.ARROW_LEFT.create()); | |||
| matchdayNavigation.addRunnableToBeRunAfterSelection(this::configure); | |||
| } | |||
| private void configure() { | |||
| Optional<Matchday> prevMatchday = getPrevMatchday(); | |||
| setEnabled(prevMatchday.isPresent()); | |||
| addClickListener(event -> prevMatchday.ifPresent(matchday -> matchdayNavigation.getMatchdaySelect().setValue(matchday))); | |||
| } | |||
| private Optional<Matchday> getPrevMatchday() { | |||
| int index = MatchdayButtonUtils.getMatchdayIndex(matchdayNavigation); | |||
| if (index > 0) return Optional.ofNullable(matchdayNavigation.getMatchdayList().get(index - 1)); | |||
| return Optional.empty(); | |||
| } | |||
| } | |||
| @ -1,172 +0,0 @@ | |||
| package app.navigation.player; | |||
| import app.components.label.ValidationLabel; | |||
| import app.data.entity.Player; | |||
| import app.data.entity.Season; | |||
| import app.data.service.PlayerService; | |||
| import app.data.service.SeasonService; | |||
| import app.navigation.Navigation; | |||
| import app.navigation.NavigationUtils; | |||
| import app.utils.EntityStringUtils; | |||
| import com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent; | |||
| import com.vaadin.flow.component.HasValue.ValueChangeListener; | |||
| import com.vaadin.flow.component.UI; | |||
| import com.vaadin.flow.component.select.Select; | |||
| import com.vaadin.flow.router.BeforeEvent; | |||
| import com.vaadin.flow.router.WildcardParameter; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| import java.util.Optional; | |||
| @SuppressWarnings("OptionalUsedAsFieldOrParameterType") | |||
| public class PlayerNavigation extends Navigation { | |||
| private final PlayerService playerService; | |||
| private final SeasonService seasonService; | |||
| private final List<Player> playerList = new ArrayList<>(); | |||
| private final List<Season> seasonList = new ArrayList<>(); | |||
| private final Select<Player> playerSelect = new Select<>(); | |||
| private final Select<Season> seasonSelect = new Select<>(); | |||
| public PlayerNavigation(@Autowired PlayerService playerService, | |||
| @Autowired SeasonService seasonService) { | |||
| this.playerService = playerService; | |||
| this.seasonService = seasonService; | |||
| fillPlayerSelectWithData(); | |||
| playerSelect.addValueChangeListener(playerSelectValueChangeListener()); | |||
| seasonSelect.addValueChangeListener(seasonSelectValueChangeListener()); | |||
| playerSelect.setItemLabelGenerator(EntityStringUtils::getPlayerString); | |||
| seasonSelect.setItemLabelGenerator(EntityStringUtils::getSeasonString); | |||
| } | |||
| public void addRunnableToBeRunAfterSelection(Runnable runnable) { | |||
| runnablesToBeRunAfterSelection.add(runnable); | |||
| } | |||
| protected void updateUrl() { | |||
| String playerParam = null; | |||
| String seasonParam = null; | |||
| if (playerSelect.getOptionalValue().isPresent()) | |||
| playerParam = EntityStringUtils.getPlayerStringForURL(playerSelect.getValue()); | |||
| if (seasonSelect.getOptionalValue().isPresent()) | |||
| seasonParam = EntityStringUtils.getSeasonStringForURL(seasonSelect.getValue()); | |||
| String params = NavigationUtils.getWildcardParam(false, playerParam, seasonParam); | |||
| UI.getCurrent().getPage().getHistory().pushState(null, String.format("%s/%s", getRoute(), params)); | |||
| } | |||
| private ValueChangeListener<ComponentValueChangeEvent<Select<Player>, Player>> playerSelectValueChangeListener() { | |||
| return event -> { | |||
| fillSeasonSelectWithData(event.getValue()); | |||
| autoselectSeason(); | |||
| }; | |||
| } | |||
| private ValueChangeListener<ComponentValueChangeEvent<Select<Season>, Season>> seasonSelectValueChangeListener() { | |||
| return event -> doPostSelectionStuff(); | |||
| } | |||
| private void autoselectPlayer() { | |||
| if (playerList.isEmpty()) { | |||
| validationLabel.setText("No Players in List!"); | |||
| validationLabel.setValid(false); | |||
| return; | |||
| } | |||
| playerSelect.setValue(playerList.get(0)); | |||
| } | |||
| private void autoselectSeason() { | |||
| if (seasonList.isEmpty()) { | |||
| validationLabel.setText("No Season in List!"); | |||
| validationLabel.setValid(false); | |||
| return; | |||
| } | |||
| seasonSelect.setValue(seasonList.get(seasonList.size() - 1)); | |||
| } | |||
| private void fillPlayerSelectWithData() { | |||
| playerList.clear(); | |||
| playerList.addAll(playerService.getAllPlayersSorted()); | |||
| playerSelect.setItems(playerList); | |||
| } | |||
| private void fillSeasonSelectWithData(Player player) { | |||
| seasonList.clear(); | |||
| seasonList.addAll(seasonService.getAllSeasonsForPlayerSorted(player)); | |||
| seasonSelect.setItems(seasonList); | |||
| } | |||
| @Override | |||
| public void setParameter(BeforeEvent event, @WildcardParameter String param) { | |||
| Map<PlayerNavigationLevel, Optional<String>> map = PlayerNavigationUtils.getParameterMap(param); | |||
| navigate(map.get(PlayerNavigationLevel.PLAYER), map.get(PlayerNavigationLevel.SEASON)); | |||
| } | |||
| private void navigate(Optional<String> playerParam, Optional<String> seasonParam) { | |||
| Optional<Player> player = NavigationUtils.getObjectFromParam(playerList, playerParam); | |||
| if (player.isPresent()) playerSelect.setValue(player.get()); | |||
| else autoselectPlayer(); | |||
| Optional<Season> season = NavigationUtils.getObjectFromParam(seasonList, seasonParam); | |||
| if (season.isPresent()) seasonSelect.setValue(season.get()); | |||
| else autoselectSeason(); | |||
| } | |||
| public List<Player> getPlayerList() { | |||
| return playerList; | |||
| } | |||
| public List<Season> getSeasonList() { | |||
| return seasonList; | |||
| } | |||
| public Select<Player> getPlayerSelect() { | |||
| return playerSelect; | |||
| } | |||
| public Select<Season> getSeasonSelect() { | |||
| return seasonSelect; | |||
| } | |||
| public Optional<Player> getSelectedPlayer() { | |||
| return playerSelect.getOptionalValue(); | |||
| } | |||
| public Optional<Season> getSelectedSeason() { | |||
| return seasonSelect.getOptionalValue(); | |||
| } | |||
| public PlayerService getPlayerService() { | |||
| return playerService; | |||
| } | |||
| public SeasonService getSeasonService() { | |||
| return seasonService; | |||
| } | |||
| @Override | |||
| public String getWildcardParam() { | |||
| return NavigationUtils.getWildcardParam( | |||
| false, | |||
| getPlayerParam().orElse(null), | |||
| getSeasonParam().orElse(null)); | |||
| } | |||
| public Optional<String> getPlayerParam() { | |||
| return getSelectedPlayer().map(EntityStringUtils::getPlayerStringForURL); | |||
| } | |||
| public Optional<String> getSeasonParam() { | |||
| return getSelectedSeason().map(EntityStringUtils::getSeasonStringForURL); | |||
| } | |||
| } | |||
| @ -1,7 +0,0 @@ | |||
| package app.navigation.player; | |||
| public enum PlayerNavigationLevel { | |||
| NONE, | |||
| PLAYER, | |||
| SEASON, | |||
| } | |||
| @ -1,24 +0,0 @@ | |||
| package app.navigation.player; | |||
| import app.data.service.PlayerService; | |||
| import app.data.service.SeasonService; | |||
| import app.navigation.NavigationService; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Service; | |||
| @Service | |||
| public class PlayerNavigationService implements NavigationService<PlayerNavigation> { | |||
| private final SeasonService seasonService; | |||
| private final PlayerService playerService; | |||
| public PlayerNavigationService(@Autowired PlayerService playerService, | |||
| @Autowired SeasonService seasonService) { | |||
| this.playerService = playerService; | |||
| this.seasonService = seasonService; | |||
| } | |||
| public PlayerNavigation getNewNavigation() { | |||
| return new PlayerNavigation(playerService, seasonService); | |||
| } | |||
| } | |||
| @ -1,25 +0,0 @@ | |||
| package app.navigation.player; | |||
| import com.vaadin.flow.router.WildcardParameter; | |||
| import org.springframework.lang.NonNull; | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
| import java.util.Optional; | |||
| class PlayerNavigationUtils { | |||
| private PlayerNavigationUtils() { | |||
| } | |||
| @NonNull | |||
| static Map<PlayerNavigationLevel, Optional<String>> getParameterMap(@WildcardParameter String param) { | |||
| Map<PlayerNavigationLevel, Optional<String>> map = new HashMap<>(); | |||
| String[] params = param.split("/"); | |||
| if (params.length >= 1) map.put(PlayerNavigationLevel.PLAYER, Optional.of(params[0])); | |||
| if (params.length >= 2) map.put(PlayerNavigationLevel.SEASON, Optional.of(params[1])); | |||
| map.putIfAbsent(PlayerNavigationLevel.PLAYER, Optional.empty()); | |||
| map.putIfAbsent(PlayerNavigationLevel.SEASON, Optional.empty()); | |||
| return map; | |||
| } | |||
| } | |||
| @ -1,31 +0,0 @@ | |||
| package app.navigation.player.components; | |||
| import app.navigation.player.PlayerNavigation; | |||
| import com.vaadin.flow.component.html.Label; | |||
| import com.vaadin.flow.component.orderedlayout.HorizontalLayout; | |||
| public class PlayerNavigationHeader extends HorizontalLayout { | |||
| private final PlayerNavigation playerNavigation; | |||
| private final Label seasonLabel = new Label("Season:"); | |||
| private final Label playerLabel = new Label("Player:"); | |||
| public PlayerNavigationHeader(PlayerNavigation playerNavigation) { | |||
| this.playerNavigation = playerNavigation; | |||
| defineLayout(); | |||
| configureChildren(); | |||
| } | |||
| private void defineLayout() { | |||
| setWidthFull(); | |||
| setAlignItems(Alignment.CENTER); | |||
| setJustifyContentMode(JustifyContentMode.END); | |||
| } | |||
| private void configureChildren() { | |||
| removeAll(); | |||
| add(playerLabel, playerNavigation.getPlayerSelect()); | |||
| add(seasonLabel, playerNavigation.getSeasonSelect()); | |||
| } | |||
| } | |||
| @ -1,37 +0,0 @@ | |||
| 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 NextPlayerButton extends Button { | |||
| private final PlayerNavigation playerNavigation; | |||
| public NextPlayerButton(PlayerNavigation playerNavigation) { | |||
| this.playerNavigation = playerNavigation; | |||
| setIcon(VaadinIcon.ARROW_RIGHT.create()); | |||
| playerNavigation.addRunnableToBeRunAfterSelection(this::configure); | |||
| } | |||
| private void configure() { | |||
| Optional<Player> nextPlayer = getNextPlayer(); | |||
| setEnabled(nextPlayer.isPresent()); | |||
| addClickListener(event -> nextPlayer.ifPresent(player -> playerNavigation.getPlayerSelect().setValue(player))); | |||
| } | |||
| private Optional<Player> getNextPlayer() { | |||
| int index = PlayerButtonUtils.getPlayerIndex(playerNavigation); | |||
| if (index < 0) return Optional.empty(); | |||
| List<Player> playerList = playerNavigation.getPlayerList(); | |||
| return Optional.ofNullable(playerList.get((index + 1) % playerList.size())); | |||
| } | |||
| } | |||
| @ -1,14 +0,0 @@ | |||
| package app.navigation.player.components.button; | |||
| import app.navigation.player.PlayerNavigation; | |||
| import java.util.concurrent.atomic.AtomicInteger; | |||
| class PlayerButtonUtils { | |||
| static int getPlayerIndex(PlayerNavigation playerNavigation) { | |||
| AtomicInteger index = new AtomicInteger(-1); | |||
| playerNavigation.getSelectedPlayer().ifPresent(player -> index.set(playerNavigation.getPlayerList().indexOf(player))); | |||
| return index.get(); | |||
| } | |||
| } | |||
| @ -1,37 +0,0 @@ | |||
| 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<Player> prevPlayer = getPrevPlayer(); | |||
| setEnabled(prevPlayer.isPresent()); | |||
| addClickListener(event -> prevPlayer.ifPresent(player -> playerNavigation.getPlayerSelect().setValue(player))); | |||
| } | |||
| private Optional<Player> getPrevPlayer() { | |||
| int index = PlayerButtonUtils.getPlayerIndex(playerNavigation); | |||
| if (index < 0) return Optional.empty(); | |||
| List<Player> playerList = playerNavigation.getPlayerList(); | |||
| return Optional.ofNullable(playerList.get((index - 1) % playerList.size())); | |||
| } | |||
| } | |||
| @ -1,280 +0,0 @@ | |||
| package app.navigation.regular; | |||
| import app.data.entity.Match; | |||
| import app.data.entity.Matchday; | |||
| import app.data.entity.Season; | |||
| import app.data.service.MatchService; | |||
| import app.data.service.MatchdayService; | |||
| import app.data.service.SeasonService; | |||
| import app.navigation.Navigation; | |||
| import app.navigation.NavigationUtils; | |||
| import app.utils.EntityStringUtils; | |||
| import com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent; | |||
| import com.vaadin.flow.component.HasValue.ValueChangeListener; | |||
| import com.vaadin.flow.component.UI; | |||
| import com.vaadin.flow.component.select.Select; | |||
| import com.vaadin.flow.router.BeforeEvent; | |||
| import com.vaadin.flow.router.WildcardParameter; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.lang.NonNull; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| import java.util.Optional; | |||
| @SuppressWarnings("OptionalUsedAsFieldOrParameterType") | |||
| public class RegularNavigation extends Navigation { | |||
| private boolean onlyMatchdaysWithActivity; | |||
| private final SeasonService seasonService; | |||
| private final MatchdayService matchdayService; | |||
| private final MatchService matchService; | |||
| private final List<Season> seasonList = new ArrayList<>(); | |||
| private final List<Matchday> matchdayList = new ArrayList<>(); | |||
| private final List<Match> matchList = new ArrayList<>(); | |||
| private final Select<Season> seasonSelect = new Select<>(); | |||
| private final Select<Matchday> matchdaySelect = new Select<>(); | |||
| private final Select<Match> matchSelect = new Select<>(); | |||
| private RegularNavigationLevel regularNavigationLevel = RegularNavigationLevel.SEASON; | |||
| public RegularNavigation(@Autowired SeasonService seasonService, | |||
| @Autowired MatchdayService matchdayService, | |||
| @Autowired MatchService matchService) { | |||
| this.seasonService = seasonService; | |||
| this.matchdayService = matchdayService; | |||
| this.matchService = matchService; | |||
| fillSeasonSelectWithData(); | |||
| seasonSelect.addValueChangeListener(seasonSelectValueChangeListener()); | |||
| matchdaySelect.addValueChangeListener(matchdaySelectValueChangeListener()); | |||
| matchSelect.addValueChangeListener(matchSelectValueChangeListener()); | |||
| seasonSelect.setItemLabelGenerator(EntityStringUtils::getSeasonString); | |||
| matchdaySelect.setItemLabelGenerator(EntityStringUtils::getMatchdayString); | |||
| matchSelect.setItemLabelGenerator(EntityStringUtils::getMatchString); | |||
| } | |||
| public void setOnlyMatchdaysWithActivity(boolean onlyMatchdaysWithActivity) { | |||
| this.onlyMatchdaysWithActivity = onlyMatchdaysWithActivity; | |||
| } | |||
| public void setNavigationLevel(@NonNull RegularNavigationLevel regularNavigationLevel) { | |||
| this.regularNavigationLevel = regularNavigationLevel; | |||
| } | |||
| public void addRunnableToBeRunAfterSelection(Runnable runnable) { | |||
| runnablesToBeRunAfterSelection.add(runnable); | |||
| } | |||
| public boolean seasonEnabled() { | |||
| return this.regularNavigationLevel.compareTo(RegularNavigationLevel.SEASON) >= 0; | |||
| } | |||
| public boolean matchdayEnabled() { | |||
| return this.regularNavigationLevel.compareTo(RegularNavigationLevel.MATCHDAY) >= 0; | |||
| } | |||
| public boolean matchEnabled() { | |||
| return this.regularNavigationLevel.compareTo(RegularNavigationLevel.MATCH) >= 0; | |||
| } | |||
| protected void updateUrl() { | |||
| String seasonParam = null; | |||
| String matchdayParam = null; | |||
| String matchParam = null; | |||
| if (seasonEnabled() && seasonSelect.getOptionalValue().isPresent()) | |||
| seasonParam = EntityStringUtils.getSeasonStringForURL(seasonSelect.getValue()); | |||
| if (matchdayEnabled() && matchdaySelect.getOptionalValue().isPresent()) | |||
| matchdayParam = EntityStringUtils.getMatchdayStringForURL(matchdaySelect.getValue()); | |||
| if (matchEnabled() && matchSelect.getOptionalValue().isPresent()) | |||
| matchParam = EntityStringUtils.getMatchStringForURL(matchSelect.getValue()); | |||
| String params = NavigationUtils.getWildcardParam(editFlag, seasonParam, matchdayParam, matchParam); | |||
| UI.getCurrent().getPage().getHistory().pushState(null, String.format("%s/%s", getRoute(), params)); | |||
| } | |||
| private ValueChangeListener<ComponentValueChangeEvent<Select<Season>, Season>> seasonSelectValueChangeListener() { | |||
| return event -> { | |||
| if (!seasonEnabled()) throw new IllegalStateException("Cannot select season when it is not enabled!"); | |||
| if (matchdayEnabled()) { | |||
| fillMatchdaySelectWithData(event.getValue()); | |||
| autoselectMatchday(); | |||
| return; | |||
| } | |||
| doPostSelectionStuff(); | |||
| }; | |||
| } | |||
| private ValueChangeListener<ComponentValueChangeEvent<Select<Matchday>, Matchday>> matchdaySelectValueChangeListener() { | |||
| return event -> { | |||
| if (!matchdayEnabled()) throw new IllegalStateException("Cannot select matchday when it is not enabled!"); | |||
| if (matchEnabled()) { | |||
| fillMatchSelectWithData(event.getValue()); | |||
| autoselectMatch(); | |||
| return; | |||
| } | |||
| doPostSelectionStuff(); | |||
| }; | |||
| } | |||
| private ValueChangeListener<ComponentValueChangeEvent<Select<Match>, Match>> matchSelectValueChangeListener() { | |||
| return event -> { | |||
| if (!matchEnabled()) throw new IllegalStateException("Cannot select match when it is not enabled!"); | |||
| doPostSelectionStuff(); | |||
| }; | |||
| } | |||
| private void autoselectSeason() { | |||
| if (!seasonEnabled()) | |||
| throw new IllegalStateException("This method should not be called when season is not enabled!"); | |||
| if (seasonList.isEmpty()) { | |||
| validationLabel.setText("No Seasons in List!"); | |||
| validationLabel.setValid(false); | |||
| return; | |||
| } | |||
| seasonSelect.setValue(seasonList.get(seasonList.size() - 1)); | |||
| } | |||
| private void autoselectMatchday() { | |||
| if (!matchdayEnabled()) | |||
| throw new IllegalStateException("This method should not be called when matchday is not enabled!"); | |||
| if (matchdayList.isEmpty()) { | |||
| validationLabel.setText("No Matchdays in List!"); | |||
| validationLabel.setValid(false); | |||
| return; | |||
| } | |||
| Matchday matchdayToSelect = matchdayList.get(0); | |||
| for (Matchday matchday : matchdayList) if (matchdayService.hasActivity(matchday)) matchdayToSelect = matchday; | |||
| matchdaySelect.setValue(matchdayToSelect); | |||
| } | |||
| private void autoselectMatch() { | |||
| if (!matchEnabled()) | |||
| throw new IllegalStateException("This method should not be called when match is not enabled!"); | |||
| if (matchList.isEmpty()) { | |||
| validationLabel.setText("No Matches in List!"); | |||
| validationLabel.setValid(false); | |||
| return; | |||
| } | |||
| matchSelect.setValue(matchList.get(0)); | |||
| } | |||
| private void fillSeasonSelectWithData() { | |||
| seasonList.clear(); | |||
| seasonList.addAll(seasonService.getAllSeasonsSorted()); | |||
| seasonSelect.setItems(seasonList); | |||
| } | |||
| private void fillMatchdaySelectWithData(Season season) { | |||
| matchdayList.clear(); | |||
| List<Matchday> matchdaysToAdd = onlyMatchdaysWithActivity ? | |||
| matchdayService.getMatchdaysWithActivitySortedOrElseListWithOnlyFirstMatchday(season) : matchdayService.getMatchdaysSorted(season); | |||
| matchdayList.addAll(matchdaysToAdd); | |||
| matchdaySelect.setItems(matchdayList); | |||
| } | |||
| private void fillMatchSelectWithData(Matchday matchday) { | |||
| matchList.clear(); | |||
| matchList.addAll(matchService.getMatches(matchday)); | |||
| matchSelect.setItems(matchList); | |||
| } | |||
| @Override | |||
| public void setParameter(BeforeEvent event, @WildcardParameter String param) { | |||
| Map<RegularNavigationLevel, Optional<String>> map = RegularNavigationUtils.getParameterMap(param); | |||
| editFlag = NavigationUtils.editFlag(param); | |||
| navigate(map.get(RegularNavigationLevel.SEASON), map.get(RegularNavigationLevel.MATCHDAY), map.get(RegularNavigationLevel.MATCH)); | |||
| } | |||
| private void navigate(Optional<String> seasonParam, Optional<String> matchdayParam, Optional<String> matchParam) { | |||
| if (!seasonEnabled()) return; | |||
| Optional<Season> season = NavigationUtils.getObjectFromParam(seasonList, seasonParam); | |||
| if (season.isPresent()) seasonSelect.setValue(season.get()); | |||
| else autoselectSeason(); | |||
| if (!matchdayEnabled()) return; | |||
| Optional<Matchday> matchday = NavigationUtils.getObjectFromParam(matchdayList, matchdayParam); | |||
| if (matchday.isPresent()) matchdaySelect.setValue(matchday.get()); | |||
| else autoselectMatchday(); | |||
| if (!matchEnabled()) return; | |||
| Optional<Match> match = NavigationUtils.getObjectFromParam(matchList, matchParam); | |||
| if (match.isPresent()) matchSelect.setValue(match.get()); | |||
| else autoselectMatch(); | |||
| } | |||
| public List<Season> getSeasonList() { | |||
| return seasonList; | |||
| } | |||
| public List<Matchday> getMatchdayList() { | |||
| return matchdayList; | |||
| } | |||
| public List<Match> getMatchList() { | |||
| return matchList; | |||
| } | |||
| public Select<Season> getSeasonSelect() { | |||
| return seasonSelect; | |||
| } | |||
| public Select<Matchday> getMatchdaySelect() { | |||
| return matchdaySelect; | |||
| } | |||
| public Select<Match> getMatchSelect() { | |||
| return matchSelect; | |||
| } | |||
| public Optional<Matchday> getSelectedMatchday() { | |||
| return matchdaySelect.getOptionalValue(); | |||
| } | |||
| public Optional<Season> getSelectedSeason() { | |||
| return seasonSelect.getOptionalValue(); | |||
| } | |||
| public Optional<Match> getSelectedMatch() { | |||
| return matchSelect.getOptionalValue(); | |||
| } | |||
| public SeasonService getSeasonService() { | |||
| return seasonService; | |||
| } | |||
| public MatchdayService getMatchdayService() { | |||
| return matchdayService; | |||
| } | |||
| public MatchService getMatchService() { | |||
| return matchService; | |||
| } | |||
| @Override | |||
| public String getWildcardParam() { | |||
| return NavigationUtils.getWildcardParam( | |||
| editFlag, | |||
| getSeasonParam().orElse(null), | |||
| getMatchdayParam().orElse(null), | |||
| getMatchParam().orElse(null)); | |||
| } | |||
| public Optional<String> getSeasonParam() { | |||
| return getSelectedSeason().map(EntityStringUtils::getSeasonStringForURL); | |||
| } | |||
| public Optional<String> getMatchdayParam() { | |||
| return getSelectedMatchday().map(EntityStringUtils::getMatchdayStringForURL); | |||
| } | |||
| public Optional<String> getMatchParam() { | |||
| return getSelectedMatch().map(EntityStringUtils::getMatchStringForURL); | |||
| } | |||
| } | |||
| @ -1,8 +0,0 @@ | |||
| package app.navigation.regular; | |||
| public enum RegularNavigationLevel { | |||
| NONE, | |||
| SEASON, | |||
| MATCHDAY, | |||
| MATCH | |||
| } | |||
| @ -1,29 +0,0 @@ | |||
| package app.navigation.regular; | |||
| import app.data.service.MatchService; | |||
| import app.data.service.MatchdayService; | |||
| import app.data.service.SeasonService; | |||
| import app.navigation.NavigationService; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Service; | |||
| @Service | |||
| public class RegularNavigationService implements NavigationService<RegularNavigation> { | |||
| private final SeasonService seasonService; | |||
| private final MatchdayService matchdayService; | |||
| private final MatchService matchService; | |||
| public RegularNavigationService(@Autowired SeasonService seasonService, | |||
| @Autowired MatchdayService matchdayService, | |||
| @Autowired MatchService matchService) { | |||
| this.seasonService = seasonService; | |||
| this.matchdayService = matchdayService; | |||
| this.matchService = matchService; | |||
| } | |||
| @Override | |||
| public RegularNavigation getNewNavigation() { | |||
| return new RegularNavigation(seasonService, matchdayService, matchService); | |||
| } | |||
| } | |||
| @ -1,31 +0,0 @@ | |||
| package app.navigation.regular; | |||
| import app.navigation.NavigationUtils; | |||
| import com.vaadin.flow.router.WildcardParameter; | |||
| import org.springframework.lang.NonNull; | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
| import java.util.Optional; | |||
| class RegularNavigationUtils { | |||
| private RegularNavigationUtils() { | |||
| } | |||
| @NonNull | |||
| static Map<RegularNavigationLevel, Optional<String>> getParameterMap(@WildcardParameter String param) { | |||
| Map<RegularNavigationLevel, Optional<String>> map = new HashMap<>(); | |||
| String[] params = param.split("/"); | |||
| if (params.length >= 1 && !params[0].equals(NavigationUtils.EDIT)) | |||
| map.put(RegularNavigationLevel.SEASON, Optional.of(params[0])); | |||
| if (params.length >= 2 && !params[1].equals(NavigationUtils.EDIT)) | |||
| map.put(RegularNavigationLevel.MATCHDAY, Optional.of(params[1])); | |||
| if (params.length >= 3 && !params[2].equals(NavigationUtils.EDIT)) | |||
| map.put(RegularNavigationLevel.MATCH, Optional.of(params[2])); | |||
| map.putIfAbsent(RegularNavigationLevel.MATCH, Optional.empty()); | |||
| map.putIfAbsent(RegularNavigationLevel.MATCHDAY, Optional.empty()); | |||
| map.putIfAbsent(RegularNavigationLevel.SEASON, Optional.empty()); | |||
| return map; | |||
| } | |||
| } | |||
| @ -1,38 +0,0 @@ | |||
| package app.navigation.regular.components; | |||
| import app.navigation.AbstractNavigationHeader; | |||
| import app.navigation.Navigation; | |||
| import app.navigation.regular.RegularNavigation; | |||
| import com.vaadin.flow.component.html.Label; | |||
| import com.vaadin.flow.component.orderedlayout.FlexComponent; | |||
| public class RegularNavigationHeader extends AbstractNavigationHeader<RegularNavigation> { | |||
| private final Label seasonLabel = new Label("Season:"); | |||
| private final Label matchdayLabel = new Label("Matchday:"); | |||
| private final Label matchLabel = new Label("Match:"); | |||
| public RegularNavigationHeader(Navigation navigation) { | |||
| super(navigation); | |||
| } | |||
| private void defineLayout() { | |||
| setWidthFull(); | |||
| setAlignItems(FlexComponent.Alignment.CENTER); | |||
| setJustifyContentMode(FlexComponent.JustifyContentMode.END); | |||
| } | |||
| @Override | |||
| protected void defineChildren() { | |||
| removeAll(); | |||
| if (navigation.seasonEnabled()) { | |||
| add(seasonLabel, navigation.getSeasonSelect()); | |||
| } | |||
| if (navigation.matchdayEnabled()) { | |||
| add(matchdayLabel, navigation.getMatchdaySelect()); | |||
| } | |||
| if (navigation.matchEnabled()) { | |||
| add(matchLabel, navigation.getMatchSelect()); | |||
| } | |||
| } | |||
| } | |||
| @ -1,14 +0,0 @@ | |||
| package app.navigation.regular.components.button; | |||
| import app.navigation.regular.RegularNavigation; | |||
| import java.util.concurrent.atomic.AtomicInteger; | |||
| class MatchdayButtonUtils { | |||
| static int getMatchdayIndex(RegularNavigation regularNavigation) { | |||
| AtomicInteger index = new AtomicInteger(-1); | |||
| regularNavigation.getSelectedMatchday().ifPresent(matchday -> index.set(regularNavigation.getMatchdayList().indexOf(matchday))); | |||
| return index.get(); | |||
| } | |||
| } | |||
| @ -1,37 +0,0 @@ | |||
| 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 NextMatchdayButton extends Button { | |||
| private final RegularNavigation regularNavigation; | |||
| public NextMatchdayButton(RegularNavigation regularNavigation) { | |||
| this.regularNavigation = regularNavigation; | |||
| if (!regularNavigation.matchdayEnabled()) | |||
| throw new IllegalStateException("Cannot instantiate NextMatchdayButton when Matchdays are not enabled!"); | |||
| setIcon(VaadinIcon.ARROW_RIGHT.create()); | |||
| regularNavigation.addRunnableToBeRunAfterSelection(this::configure); | |||
| } | |||
| private void configure() { | |||
| Optional<Matchday> nextMatchday = getNextMatchday(); | |||
| setEnabled(nextMatchday.isPresent()); | |||
| addClickListener(event -> nextMatchday.ifPresent(matchday -> regularNavigation.getMatchdaySelect().setValue(matchday))); | |||
| } | |||
| private Optional<Matchday> getNextMatchday() { | |||
| int index = MatchdayButtonUtils.getMatchdayIndex(regularNavigation); | |||
| if (index >= 0 && index < regularNavigation.getMatchdayList().size() - 1) | |||
| return Optional.ofNullable(regularNavigation.getMatchdayList().get(index + 1)); | |||
| return Optional.empty(); | |||
| } | |||
| } | |||
| @ -1,36 +0,0 @@ | |||
| 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<Matchday> prevMatchday = getPrevMatchday(); | |||
| setEnabled(prevMatchday.isPresent()); | |||
| addClickListener(event -> prevMatchday.ifPresent(matchday -> regularNavigation.getMatchdaySelect().setValue(matchday))); | |||
| } | |||
| private Optional<Matchday> getPrevMatchday() { | |||
| int index = MatchdayButtonUtils.getMatchdayIndex(regularNavigation); | |||
| if (index > 0) return Optional.ofNullable(regularNavigation.getMatchdayList().get(index - 1)); | |||
| return Optional.empty(); | |||
| } | |||
| } | |||