package com.example.application.views.table; import com.example.application.components.navigation.SeasonAndMatchdayNavigation; import com.example.application.data.bean.PlayerForTable; import com.example.application.data.service.MatchService; import com.example.application.data.service.MatchdayService; import com.example.application.data.service.PlayerService; import com.example.application.data.service.SeasonService; import com.example.application.utils.StringUtils; import com.example.application.utils.VaadinUtils; import com.example.application.navigation.Navigation; import com.example.application.views.main.MainView; import com.vaadin.flow.component.dependency.CssImport; import com.vaadin.flow.component.grid.ColumnTextAlign; import com.vaadin.flow.component.grid.Grid; import com.vaadin.flow.component.grid.GridVariant; import com.vaadin.flow.component.html.Label; import com.vaadin.flow.component.orderedlayout.FlexComponent; import com.vaadin.flow.component.orderedlayout.HorizontalLayout; import com.vaadin.flow.component.orderedlayout.VerticalLayout; import com.vaadin.flow.function.ValueProvider; import com.vaadin.flow.router.*; import org.springframework.beans.factory.annotation.Autowired; import java.util.NoSuchElementException; @CssImport("./views/table/table-view.css") @Route(value = "table", layout = MainView.class) @RouteAlias(value = "", layout = MainView.class) @PageTitle("Schachliga DACH - Table") public class TableView extends VerticalLayout implements HasUrlParameter { private final PlayerService playerService; private final Navigation navigation; private final Label invalidUrlLabel; private final Grid grid = new Grid<>(); public TableView(@Autowired SeasonService seasonService, @Autowired MatchdayService matchdayService, @Autowired MatchService matchService, @Autowired PlayerService playerService) { this.playerService = playerService; this.navigation = new Navigation("table", seasonService, matchdayService, matchService, true); this.navigation.setAutoselectSeason(true); this.navigation.setAutoselectMatchday(true); this.navigation.addRunnableToBeRunAfterSelection(this::configureContent); this.invalidUrlLabel = navigation.getInvalidUrlLabel(); addClassName("table-view"); configureLayout(); } //////////// // LAYOUT // //////////// private void configureLayout() { setWidthFull(); setHeightFull(); setAlignItems(FlexComponent.Alignment.CENTER); add(new SeasonAndMatchdayNavigation(navigation), new HorizontalLayout(grid)); configureGrid(); } protected void configureGrid() { // TODO: add background color for content // TODO: add diff to last matchday Label headerPlace = new Label("Place"); headerPlace.addClassName("important_table_column_header"); Label headerPlayer = new Label("Player"); headerPlayer.addClassName("important_table_column_header"); Label headerMatchesPlayed = new Label("Played"); Label headerMatchPoints = new Label("Points"); headerMatchPoints.addClassName("important_table_column_header"); Label headerWon = new Label("W"); Label headerDrawn = new Label("D"); Label headerLost = new Label("L"); Label headerGames = new Label("Games"); Label headerDiff = new Label("Diff"); grid.addColumn(VaadinUtils.getBoldStringRenderer(PlayerForTable::getPlaceString)) .setHeader(headerPlace) .setTextAlign(ColumnTextAlign.CENTER) .setWidth("5em"); grid.addColumn(VaadinUtils.getPlayerRenderer(PlayerForTable::getPlayer)) .setHeader(headerPlayer) .setTextAlign(ColumnTextAlign.START) .setWidth("13em"); grid.addColumn((ValueProvider) PlayerForTable::getAmountOfMatches) .setHeader(headerMatchesPlayed) .setTextAlign(ColumnTextAlign.CENTER) .setWidth("5em"); grid.addColumn(VaadinUtils.getBoldStringRenderer(player -> String.valueOf(player.getMatchPoints()))) .setHeader(headerMatchPoints) .setTextAlign(ColumnTextAlign.CENTER) .setWidth("6em"); grid.addColumn((ValueProvider) PlayerForTable::getAmountOfMatchesWon) .setHeader(headerWon) .setTextAlign(ColumnTextAlign.CENTER) .setWidth("3em"); grid.addColumn((ValueProvider) PlayerForTable::getAmountOfMatchesDrawn) .setHeader(headerDrawn) .setTextAlign(ColumnTextAlign.CENTER) .setWidth("3em"); grid.addColumn((ValueProvider) PlayerForTable::getAmountOfMatchesLost) .setHeader(headerLost) .setTextAlign(ColumnTextAlign.CENTER) .setWidth("3em"); grid.addColumn((ValueProvider) this::getResultString) .setHeader(headerGames) .setTextAlign(ColumnTextAlign.CENTER) .setWidth("6em"); grid.addColumn((ValueProvider) player -> StringUtils.getSignedString(player.getGamePointDiff())) .setHeader(headerDiff) .setTextAlign(ColumnTextAlign.CENTER) .setWidth("5em"); grid.setWidth("51em"); grid.setHeightByRows(true); grid.addThemeVariants(GridVariant.LUMO_NO_BORDER, GridVariant.LUMO_NO_ROW_BORDERS, GridVariant.LUMO_ROW_STRIPES); } private String getResultString(PlayerForTable player) { return StringUtils.getResultString(player.getGamePointsForSelf(), player.getGamePointsForOpponents()); } ///////////// // CONTENT // ///////////// protected void configureContent() { try { grid.setItems(playerService.getPlayersForTable(navigation.getSelectedMatchday().orElseThrow())); } catch (NoSuchElementException e) { invalidUrlLabel.setText("No season and/or matchday selected! Please select them above."); } } @Override public void setParameter(BeforeEvent event, @WildcardParameter String param) { navigation.setParameter(event, param); } }