@ -29,7 +29,9 @@ public class Navigation implements HasUrlParameter<String> {
private final String route ;
private final boolean onlyMatchdaysWithActivity ;
private final List < Runnable > runnablesToBeRunAfterSelection = new ArrayList < > ( ) ;
private final List < Runnable > runnablesToBeRunAfterSeasonSelection = new ArrayList < > ( ) ;
private final List < Runnable > runnablesToBeRunAfterMatchdaySelection = new ArrayList < > ( ) ;
private final List < Runnable > runnablesToBeRunAfterMatchSelection = new ArrayList < > ( ) ;
private final SeasonService seasonService ;
private final MatchdayService matchdayService ;
@ -91,23 +93,11 @@ public class Navigation implements HasUrlParameter<String> {
this . autoselectMatch = autoselectMatch ;
}
/ / TODO : run the runnables after each selection ( anyhow ) , then push history state ( UI . getCurrent ( ) . getPage ( ) . getHistory ( ) . pushState ( null , "http://host.com/person?action=edit&id=1" ) ; ) .
/ / Navigate as little as possible .
private void updateUrl ( ) {
String params = NavigationUtils . getWildcardParam ( seasonParam , matchdayParam , matchParam ) ;
UI . getCurrent ( ) . getPage ( ) . getHistory ( ) . pushState ( null , String . format ( "%s/%s" , route , params ) ) ;
}
private HasValue . ValueChangeListener < ? super AbstractField . ComponentValueChangeEvent < Select < Matchday > , Matchday > > matchdaySelectValueChangeListener ( ) {
return matchdayChangeEvent - > {
Matchday matchday = matchdayChangeEvent . getValue ( ) ;
if ( matchday ! = null ) {
matchParam = matchday . toString ( ) ;
updateUrl ( ) ;
runnablesToBeRunAfterSelection . forEach ( Runnable : : run ) ;
}
} ;
}
private HasValue . ValueChangeListener < ? super AbstractField . ComponentValueChangeEvent < Select < Season > , Season > > seasonSelectValueChangeListener ( ) {
return seasonChangeEvent - > {
@ -128,19 +118,36 @@ public class Navigation implements HasUrlParameter<String> {
this . matchdayParam = matchdayParam ;
this . matchParam = null ;
updateUrl ( ) ;
runnablesToBeRunAfterSelection . forEach ( Runnable : : run ) ;
runnablesToBeRunAfterSeasonSe lection . forEach ( Runnable : : run ) ;
}
} ;
}
private HasValue . ValueChangeListener < ? super AbstractField . ComponentValueChangeEvent < Select < Matchday > , Matchday > > matchdaySelectValueChangeListener ( ) {
return matchdayChangeEvent - > {
Matchday matchday = matchdayChangeEvent . getValue ( ) ;
if ( matchday = = null ) {
matchParam = null ;
} else {
matchdayParam = matchday . toString ( ) ;
fillMatchSelectWithData ( matchday ) ;
configureButtons ( ) ;
matchSelect . setValue ( null ) ;
runnablesToBeRunAfterMatchdaySelection . forEach ( Runnable : : run ) ;
}
updateUrl ( ) ;
} ;
}
private HasValue . ValueChangeListener < ? super AbstractField . ComponentValueChangeEvent < Select < Match > , Match > > matchSelectValueChangeListener ( ) {
return matchChangeEvent - > {
Match match = matchChangeEvent . getValue ( ) ;
if ( match ! = null ) {
if ( match = = null ) {
matchParam = null ;
} else {
matchParam = match . toString ( ) ;
updateUrl ( ) ;
runnablesToBeRunAfterSelection . forEach ( Runnable : : run ) ; / / TODO : offer different lists for season , matchday , match
runnablesToBeRunAfterMatch Selection . forEach ( Runnable : : run ) ;
}
} ;
}
@ -169,10 +176,6 @@ public class Navigation implements HasUrlParameter<String> {
return matchdayList . stream ( ) . anyMatch ( matchday - > matchdayParam . equals ( matchday . toString ( ) ) ) ;
}
private void navigate ( String seasonParam , String matchdayParam , String matchParam ) { / / TODO : change this to String . . . - > see where you need which parameters
UI . getCurrent ( ) . navigate ( String . format ( "%s/%s" , route , NavigationUtils . getWildcardParam ( seasonParam , matchdayParam , matchParam ) ) ) ;
}
@Override
public void setParameter ( BeforeEvent event , @WildcardParameter String param ) {
Map < UrlParameterType , String > map = NavigationUtils . getParameterMap ( param ) ;
@ -187,10 +190,8 @@ public class Navigation implements HasUrlParameter<String> {
invalidUrlLabel . setText ( String . format ( "No Match found in Matchday %s in Season %s!" , matchday . toString ( ) , season . toString ( ) ) ) ;
}
private void matchFound ( Season season , Matchday matchday , Match match ) {
private void matchFound ( Match match ) {
matchSelect . setValue ( match ) ;
navigate ( season . toString ( ) , matchday . toString ( ) , match . toString ( ) ) ;
}
private void noMatchdayFound ( Season season ) {
@ -200,7 +201,7 @@ public class Navigation implements HasUrlParameter<String> {
private void autoselectMatch ( Season season , Matchday matchday ) {
Optional < Match > firstMatch = matchService . getFirstMatch ( matchday ) ;
firstMatch . ifPresentOrElse (
match - > matchFound ( season , matchday , match ) ,
this : : matchFound ,
( ) - > noMatchFound ( season , matchday ) ) ;
}
@ -210,9 +211,7 @@ public class Navigation implements HasUrlParameter<String> {
if ( paramInvalid ( matchParam ) & & autoselectMatch ) {
autoselectMatch ( season , matchday ) ;
return ;
}
navigate ( season . toString ( ) , matchday . toString ( ) , matchParam ) ;
}
private void noSeasonFound ( ) {
@ -232,9 +231,7 @@ public class Navigation implements HasUrlParameter<String> {
if ( paramInvalid ( matchdayParam ) & & autoselectMatchday ) {
autoselectMatchday ( season , matchParam ) ;
return ;
}
navigate ( season . toString ( ) , matchdayParam , matchParam ) ;
}
private void autoselectSeason ( String matchdayParam , String matchParam ) {
@ -262,9 +259,6 @@ public class Navigation implements HasUrlParameter<String> {
public void setParameter ( String seasonParam , String matchdayParam , String matchParam ) {
if ( autoselectIfNecessary ( seasonParam , matchdayParam , matchParam ) ) {
for ( Runnable runnable : runnablesToBeRunAfterSelection ) {
runnable . run ( ) ;
}
return ;
}
@ -297,10 +291,6 @@ public class Navigation implements HasUrlParameter<String> {
} else if ( autoselectMatch ) {
invalidUrlLabel . setText ( String . format ( "Invalid URL: Match \"%s\" in Matchday \"%s\" in Season \"%s\" does not exist in the database!" , matchParam , matchdayParam , seasonParam ) ) ;
}
for ( Runnable runnable : runnablesToBeRunAfterSelection ) {
runnable . run ( ) ;
}
}
@Nullable
@ -348,8 +338,16 @@ public class Navigation implements HasUrlParameter<String> {
return null ;
}
public void addRunnableToBeRunAfterSelection ( Runnable runnable ) {
runnablesToBeRunAfterSelection . add ( runnable ) ;
public void addRunnableToBeRunAfterSeasonSelection ( Runnable runnable ) {
runnablesToBeRunAfterSeasonSelection . add ( runnable ) ;
}
public void addRunnableToBeRunAfterMatchdaySelection ( Runnable runnable ) {
runnablesToBeRunAfterMatchdaySelection . add ( runnable ) ;
}
public void addRunnableToBeRunAfterMatchSelection ( Runnable runnable ) {
runnablesToBeRunAfterMatchSelection . add ( runnable ) ;
}
private void configureButtons ( ) {
@ -361,7 +359,9 @@ public class Navigation implements HasUrlParameter<String> {
}
private ComponentEventListener < ClickEvent < Button > > getButtonClickListener ( String matchdayParam ) {
return buttonClickEvent - > navigate ( seasonParam , matchdayParam , matchParam ) ;
return buttonClickEvent - > matchdayList . stream ( )
. filter ( matchday - > matchdayParam . equals ( matchday . toString ( ) ) )
. findFirst ( ) . ifPresent ( matchdaySelect : : setValue ) ;
}
private String getPrevMatchdayParam ( ) {
@ -417,6 +417,6 @@ public class Navigation implements HasUrlParameter<String> {
}
public void selectMatch ( Match match ) {
navigate ( seasonParam , matchdayParam , match . toString ( ) ) ;
matchSelect . setValue ( match ) ;
}
}