fi.evolver.ai.vaadin.view.BaseLayout Maven / Gradle / Ivy
package fi.evolver.ai.vaadin.view;
import java.io.Serial;
import java.util.List;
import org.vaadin.lineawesome.LineAwesomeIcon;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.applayout.AppLayout;
import com.vaadin.flow.component.applayout.DrawerToggle;
import com.vaadin.flow.component.html.H2;
import com.vaadin.flow.component.html.Header;
import com.vaadin.flow.component.html.Image;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.orderedlayout.Scroller;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.sidenav.SideNav;
import com.vaadin.flow.component.sidenav.SideNavItem;
import com.vaadin.flow.router.HasDynamicTitle;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.theme.lumo.LumoUtility;
import fi.evolver.ai.vaadin.VersionInfoConfigurator;
import fi.evolver.ai.vaadin.translations.PageTitleTranslationKey;
import fi.evolver.ai.vaadin.util.AuthUtils;
import fi.evolver.utils.string.StringUtils;
public abstract class BaseLayout extends AppLayout implements HasDynamicTitle {
@Serial
private static final long serialVersionUID = 1L;
protected H2 viewTitle = new H2();
public BaseLayout() {
viewTitle.addClassNames(LumoUtility.FontSize.LARGE, LumoUtility.Margin.NONE);
viewTitle.setMaxWidth("50%");
setPrimarySection(Section.DRAWER);
addDrawerContent();
addHeaderContent();
}
public abstract Image createHeaderImage();
public List createNavRoutes() {
return List.of();
}
public List createNavSections() {
return List.of();
}
public boolean generateDefaultProfileRoute() {
return true;
}
public boolean generateDefaultAdminRoute() {
return true;
}
public boolean displayAppVersion() {
return true;
}
private void addDrawerContent() {
addToDrawer(new Header(createHeaderImage()), new Scroller(createNavigation()));
if (displayAppVersion())
addToDrawer(createVersionInfoContent(VersionInfoConfigurator.appVersion));
}
private VerticalLayout createVersionInfoContent(String appVersion) {
VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
layout.setWidth("90%");
Span versionSpan = new Span(appVersion);
versionSpan.getStyle().set("font-style", "italic");
layout.add(versionSpan);
return layout;
}
private VerticalLayout createNavigation() {
VerticalLayout layout = new VerticalLayout();
layout.setSpacing(true);
layout.setSizeUndefined();
createNavSections().forEach(nav -> {
nav.setWidthFull();
layout.add(nav);
});
if (!createNavRoutes().isEmpty() || generateDefaultProfileRoute()) {
SideNav sideNav = new SideNav();
sideNav.setWidthFull();
createNavRoutes().forEach(sideNav::addItem);
if (generateDefaultProfileRoute())
sideNav.addItem(
new SideNavItem(getTranslation("nav.profile"), "profile", LineAwesomeIcon.USER.create()));
layout.add(sideNav);
}
if (AuthUtils.isAdminUser() && generateDefaultAdminRoute()) {
SideNav adminNav = new SideNav("Admin");
adminNav.setCollapsible(true);
adminNav.setExpanded(true);
adminNav.addItem(
new SideNavItem(getTranslation("nav.admin"), "admin", LineAwesomeIcon.USER_SHIELD_SOLID.create()));
adminNav.setWidthFull();
layout.add(adminNav);
}
return layout;
}
private void addHeaderContent() {
DrawerToggle toggle = new DrawerToggle();
toggle.getElement().setAttribute("aria-label", getTranslation("view.baseLayout.menuToggle"));
addToNavbar(false, toggle, viewTitle);
}
@Override
protected void afterNavigation() {
super.afterNavigation();
viewTitle.setText(getCurrentPageTitle());
}
public void updateTitle(String title) {
viewTitle.setText(title);
}
@Override
public String getPageTitle() {
return getCurrentPageTitle();
}
private String getCurrentPageTitle() {
Component component = getContent();
if (component instanceof HasDynamicTitle)
return ((HasDynamicTitle) component).getPageTitle();
PageTitleTranslationKey translationKey = component.getClass().getAnnotation(PageTitleTranslationKey.class);
if (translationKey != null && StringUtils.hasText(translationKey.value()))
return getTranslation(translationKey.value());
PageTitle title = component.getClass().getAnnotation(PageTitle.class);
if (title != null && StringUtils.hasText(title.value()))
return title.value();
return getTranslation("viewTitle.%s".formatted(component.getClass().getSimpleName()));
}
}