Search in sources :

Example 1 with ThemeDefinition

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

the class UpdateThemedImportsTest method setup.

@Before
public void setup() throws Exception {
    File tmpRoot = temporaryFolder.getRoot();
    frontendDirectory = new File(tmpRoot, DEFAULT_FRONTEND_DIR);
    nodeModulesPath = new File(tmpRoot, NODE_MODULES);
    generatedPath = new File(tmpRoot, Paths.get(TARGET, DEFAULT_GENERATED_DIR).toString());
    importsFile = new File(generatedPath, IMPORTS_NAME);
    Assert.assertTrue(nodeModulesPath.mkdirs());
    createImport("./src/subfolder/sub-template.js", "");
    createImport("./src/client-side-template.js", "import 'xx' from './subfolder/sub-template.js';" + "import '@vaadin/vaadin-button/src/vaadin-button.js'");
    createImport("./src/client-side-no-themed-template.js", "");
    createImport("./src/main-template.js", "import 'xx' from './client-side-template.js';" + "import \"./client-side-no-themed-template.js\";" + "import './src/wrong-themed-template.js';" + "import '@vaadin/vaadin-button/src/vaadin-button.js'");
    // create themed modules
    createImport("./theme/myTheme/subfolder/sub-template.js", "");
    createImport("./theme/myTheme/client-side-template.js", "");
    createImport("./theme/myTheme/main-template.js", "");
    // wrong-themed-template.js should not be resolved inside node_modules.
    // It should be searched only inside frontend directory
    createImport("theme/myTheme/wrong-themed-template.js", "");
    // create css files to avoid exception when files not found during the
    // test
    createImport("./foo.css", "");
    createImport("@vaadin/vaadin-mixed-component/bar.css", "");
    // make external component's module and its themed version
    createImport("@vaadin/vaadin-button/src/vaadin-button.js", "");
    createImport("@vaadin/vaadin-button/theme/myTheme/vaadin-button.js", "");
    ClassFinder finder = getClassFinder();
    FrontendDependencies deps = new FrontendDependencies(finder) {

        @Override
        public List<String> getModules() {
            return Stream.of("./src/main-template.js").collect(Collectors.toList());
        }

        @Override
        public Set<String> getScripts() {
            return Collections.emptySet();
        }

        @Override
        public AbstractTheme getTheme() {
            return new MyTheme();
        }

        @Override
        public ThemeDefinition getThemeDefinition() {
            return new ThemeDefinition(MyTheme.class, "", "");
        }
    };
    updater = new TaskUpdateImports(finder, deps, cf -> null, tmpRoot, generatedPath, frontendDirectory, null, null, false, TARGET, true, false, Mockito.mock(FeatureFlags.class));
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) DEFAULT_FRONTEND_DIR(com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_FRONTEND_DIR) DEFAULT_GENERATED_DIR(com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_GENERATED_DIR) StringUtils(org.apache.commons.lang3.StringUtils) FrontendDependencies(com.vaadin.flow.server.frontend.scanner.FrontendDependencies) Charset(java.nio.charset.Charset) ThemeDefinition(com.vaadin.flow.theme.ThemeDefinition) ExpectedException(org.junit.rules.ExpectedException) AbstractTheme(com.vaadin.flow.theme.AbstractTheme) Before(org.junit.Before) ClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder) Files(java.nio.file.Files) NODE_MODULES(com.vaadin.flow.server.frontend.FrontendUtils.NODE_MODULES) Set(java.util.Set) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) TARGET(com.vaadin.flow.server.Constants.TARGET) Collectors(java.util.stream.Collectors) File(java.io.File) IMPORTS_NAME(com.vaadin.flow.server.frontend.FrontendUtils.IMPORTS_NAME) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) MatcherAssert(org.hamcrest.MatcherAssert) Rule(org.junit.Rule) Paths(java.nio.file.Paths) FeatureFlags(com.vaadin.experimental.FeatureFlags) Assert(org.junit.Assert) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) ClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder) File(java.io.File) FrontendDependencies(com.vaadin.flow.server.frontend.scanner.FrontendDependencies) ThemeDefinition(com.vaadin.flow.theme.ThemeDefinition) Before(org.junit.Before)

Example 2 with ThemeDefinition

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

the class TaskUpdateThemeImportTest method setUp.

@Before
public void setUp() throws IOException {
    projectRoot = temporaryFolder.getRoot();
    npmFolder = temporaryFolder.getRoot();
    frontendDirectory = new File(projectRoot, DEFAULT_FRONTEND_DIR);
    frontendGeneratedDirectory = new File(projectRoot, DEFAULT_PROJECT_FRONTEND_GENERATED_DIR);
    File frontendFolder = new File(npmFolder, FrontendUtils.DEFAULT_FRONTEND_DIR);
    themeImportFile = new File(new File(frontendFolder, FrontendUtils.GENERATED), THEME_IMPORTS_NAME);
    themeImportTsFile = new File(new File(frontendFolder, FrontendUtils.GENERATED), THEME_IMPORTS_D_TS_NAME);
    dummyThemeClass = Mockito.mock(AbstractTheme.class).getClass();
    customTheme = new ThemeDefinition(dummyThemeClass, CUSTOM_VARIANT_NAME, CUSTOM_THEME_NAME);
    taskUpdateThemeImport = new TaskUpdateThemeImport(npmFolder, customTheme, frontendDirectory, frontendGeneratedDirectory);
}
Also used : File(java.io.File) ThemeDefinition(com.vaadin.flow.theme.ThemeDefinition) Before(org.junit.Before)

Example 3 with ThemeDefinition

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

the class WebComponentUI method assignThemeVariant.

private void assignThemeVariant() {
    WebComponentConfigurationRegistry registry = getConfigurationRegistry();
    Optional<Theme> theme = registry.getEmbeddedApplicationAnnotation(Theme.class);
    if (!theme.isPresent() || theme.get().themeClass().equals(AbstractTheme.class)) {
        return;
    }
    AbstractTheme themeInstance = Instantiator.get(this).getOrCreate(theme.get().themeClass());
    ThemeDefinition definition = new ThemeDefinition(theme.get());
    Map<String, String> attributes = themeInstance.getHtmlAttributes(definition.getVariant());
    registry.getConfigurations().forEach(config -> addAttributes(config.getTag(), attributes));
}
Also used : WebComponentConfigurationRegistry(com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry) AbstractTheme(com.vaadin.flow.theme.AbstractTheme) Theme(com.vaadin.flow.theme.Theme) AbstractTheme(com.vaadin.flow.theme.AbstractTheme) ThemeDefinition(com.vaadin.flow.theme.ThemeDefinition)

Example 4 with ThemeDefinition

use of com.vaadin.flow.theme.ThemeDefinition 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)

Example 5 with ThemeDefinition

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

the class FrontendDependenciesTest method appShellConfigurator_collectedAsEndpoint.

@Test
public void appShellConfigurator_collectedAsEndpoint() throws ClassNotFoundException {
    Mockito.when(classFinder.getSubTypesOf(AppShellConfigurator.class)).thenReturn(Collections.singleton(MyAppShell.class));
    Mockito.when(classFinder.loadClass(FakeLumo.class.getName())).thenReturn((Class) FakeLumo.class);
    FrontendDependencies dependencies = new FrontendDependencies(classFinder, false);
    Assert.assertEquals("UI and AppShell should be found", 2, dependencies.getEndPoints().size());
    AbstractTheme theme = dependencies.getTheme();
    Assert.assertNotNull("Theme not found in endpoint", theme);
    ThemeDefinition themeDefinition = dependencies.getThemeDefinition();
    Assert.assertNotNull("ThemeDefinition is not filled", themeDefinition);
    Assert.assertEquals(FakeLumo.class, themeDefinition.getTheme());
}
Also used : AbstractTheme(com.vaadin.flow.theme.AbstractTheme) ThemeDefinition(com.vaadin.flow.theme.ThemeDefinition) Test(org.junit.Test)

Aggregations

ThemeDefinition (com.vaadin.flow.theme.ThemeDefinition)6 AbstractTheme (com.vaadin.flow.theme.AbstractTheme)4 FeatureFlags (com.vaadin.experimental.FeatureFlags)2 File (java.io.File)2 IOException (java.io.IOException)2 List (java.util.List)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Before (org.junit.Before)2 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 TARGET (com.vaadin.flow.server.Constants.TARGET)1 PWA (com.vaadin.flow.server.PWA)1