Search in sources :

Example 1 with NoTheme

use of com.vaadin.flow.theme.NoTheme in project flow by vaadin.

the class FrontendDependencies method computeApplicationTheme.

/*
     * Visit all end-points and computes the theme for the application. It fails
     * in the case that there are multiple themes for the application or in the
     * case of Theme and NoTheme found in the application.
     *
     * If no theme is found and the application has endpoints, it uses lumo if
     * found in the class-path
     */
private void computeApplicationTheme() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
    // entry-point visits
    for (EndPointData endPoint : endPoints.values()) {
        if (endPoint.getLayout() != null) {
            visitClass(endPoint.getLayout(), endPoint, false);
        }
        if (endPoint.getTheme() != null) {
            visitClass(endPoint.getTheme().getThemeClass(), endPoint, true);
        }
    }
    Set<ThemeData> themes = endPoints.values().stream().filter(data -> data.getTheme().getThemeClass() != null || (data.getTheme().getThemeName() != null && !data.getTheme().getThemeName().isEmpty()) || data.getTheme().isNotheme()).map(EndPointData::getTheme).collect(Collectors.toSet());
    if (themes.size() > 1) {
        String names = endPoints.values().stream().filter(data -> data.getTheme().getThemeClass() != null || data.getTheme().getThemeName() != null || data.getTheme().isNotheme()).map(data -> "found '" + (data.getTheme().isNotheme() ? NoTheme.class.getName() : data.getTheme().getThemeName()) + "' in '" + data.getName() + "'").collect(Collectors.joining("\n      "));
        throw new IllegalStateException("\n Multiple Theme configuration is not supported:\n      " + names);
    }
    Class<? extends AbstractTheme> theme = null;
    String variant = "";
    String themeName = "";
    if (themes.isEmpty()) {
        theme = getDefaultTheme();
    } else {
        // we have a proper theme or no-theme for the app
        ThemeData themeData = themes.iterator().next();
        if (!themeData.isNotheme()) {
            String themeClass = themeData.getThemeClass();
            if (!themeData.getThemeName().isEmpty() && themeClass != null) {
                throw new IllegalStateException("Theme name and theme class can not both be specified. " + "Theme name uses Lumo and can not be used in combination with custom theme class.");
            }
            variant = themeData.getVariant();
            if (themeClass != null) {
                theme = getFinder().loadClass(themeClass);
            } else {
                theme = getDefaultTheme();
                if (theme == null) {
                    throw new IllegalStateException("Lumo dependency needs to be available on the classpath when using a theme name.");
                }
            }
            themeName = themeData.getThemeName();
        }
    }
    // theme could be null when lumo is not found or when a NoTheme found
    if (theme != null) {
        themeDefinition = new ThemeDefinition(theme, variant, themeName);
        themeInstance = new ThemeWrapper(theme);
    }
}
Also used : ClassReader(net.bytebuddy.jar.asm.ClassReader) Arrays(java.util.Arrays) VERSION(com.vaadin.flow.server.frontend.scanner.FrontendClassVisitor.VERSION) Component(com.vaadin.flow.component.Component) URL(java.net.URL) NpmPackage(com.vaadin.flow.component.dependency.NpmPackage) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Route(com.vaadin.flow.router.Route) HashSet(java.util.HashSet) NoTheme(com.vaadin.flow.theme.NoTheme) Map(java.util.Map) ThemeDefinition(com.vaadin.flow.theme.ThemeDefinition) VALUE(com.vaadin.flow.server.frontend.scanner.FrontendClassVisitor.VALUE) UI(com.vaadin.flow.component.UI) AppShellConfigurator(com.vaadin.flow.component.page.AppShellConfigurator) HasErrorParameter(com.vaadin.flow.router.HasErrorParameter) UIInitListener(com.vaadin.flow.server.UIInitListener) AbstractTheme(com.vaadin.flow.theme.AbstractTheme) LinkedHashSet(java.util.LinkedHashSet) PwaConfiguration(com.vaadin.flow.server.PwaConfiguration) WebComponentExporter(com.vaadin.flow.component.WebComponentExporter) Logger(org.slf4j.Logger) WebComponentExporterFactory(com.vaadin.flow.component.WebComponentExporterFactory) PWA(com.vaadin.flow.server.PWA) Collection(java.util.Collection) Set(java.util.Set) ReflectTools(com.vaadin.flow.internal.ReflectTools) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) VaadinServiceInitListener(com.vaadin.flow.server.VaadinServiceInitListener) List(java.util.List) Modifier(java.lang.reflect.Modifier) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) FeatureFlags(com.vaadin.experimental.FeatureFlags) InputStream(java.io.InputStream) ThemeDefinition(com.vaadin.flow.theme.ThemeDefinition)

Aggregations

FeatureFlags (com.vaadin.experimental.FeatureFlags)1 Component (com.vaadin.flow.component.Component)1 UI (com.vaadin.flow.component.UI)1 WebComponentExporter (com.vaadin.flow.component.WebComponentExporter)1 WebComponentExporterFactory (com.vaadin.flow.component.WebComponentExporterFactory)1 NpmPackage (com.vaadin.flow.component.dependency.NpmPackage)1 AppShellConfigurator (com.vaadin.flow.component.page.AppShellConfigurator)1 ReflectTools (com.vaadin.flow.internal.ReflectTools)1 HasErrorParameter (com.vaadin.flow.router.HasErrorParameter)1 Route (com.vaadin.flow.router.Route)1 PWA (com.vaadin.flow.server.PWA)1 PwaConfiguration (com.vaadin.flow.server.PwaConfiguration)1 UIInitListener (com.vaadin.flow.server.UIInitListener)1 VaadinServiceInitListener (com.vaadin.flow.server.VaadinServiceInitListener)1 VALUE (com.vaadin.flow.server.frontend.scanner.FrontendClassVisitor.VALUE)1 VERSION (com.vaadin.flow.server.frontend.scanner.FrontendClassVisitor.VERSION)1 AbstractTheme (com.vaadin.flow.theme.AbstractTheme)1 NoTheme (com.vaadin.flow.theme.NoTheme)1 ThemeDefinition (com.vaadin.flow.theme.ThemeDefinition)1 IOException (java.io.IOException)1