com.vaadin.flow.theme.Theme Java Examples

The following examples show how to use com.vaadin.flow.theme.Theme. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebComponentUI.java    From flow with Apache License 2.0 6 votes vote down vote up
private void assignThemeVariant() {
    WebComponentConfigurationRegistry registry = getConfigurationRegistry();
    Optional<Theme> theme = registry
            .getEmbeddedApplicationAnnotation(Theme.class);
    if (!theme.isPresent()) {
        return;
    }
    AbstractTheme themeInstance = Instantiator.get(this)
            .getOrCreate(theme.get().value());
    ThemeDefinition definition = new ThemeDefinition(theme.get());
    Map<String, String> attributes = themeInstance
            .getHtmlAttributes(definition.getVariant());

    registry.getConfigurations()
            .forEach(config -> addAttributes(config.getTag(), attributes));
}
 
Example #2
Source File: FullDependenciesScannerTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void getTheme_explicitTheme_themeIsDiscovered()
        throws ClassNotFoundException {
    Mockito.when(finder.loadClass(LumoTest.class.getName()))
            .thenReturn((Class) LumoTest.class);

    FrontendDependenciesScanner scanner = setUpThemeScanner(
            getAnnotatedClasses(Theme.class), Collections.emptySet(),
            (type, annotationType) -> findAnnotations(type, Theme.class));

    Mockito.verify(finder).loadClass(AbstractTheme.class.getName());

    Assert.assertNotNull(scanner.getTheme());
    Assert.assertEquals("theme/lumo/", scanner.getTheme().getThemeUrl());
    Assert.assertEquals(LumoTest.class,
            scanner.getThemeDefinition().getTheme());
    Assert.assertEquals("dark", scanner.getThemeDefinition().getVariant());
    Assert.assertEquals(0, scanner.getClasses().size());
}
 
Example #3
Source File: FullDependenciesScannerTest.java    From flow with Apache License 2.0 6 votes vote down vote up
private FullDependenciesScanner setUpThemeScanner(
        Set<Class<?>> themedClasses, Set<Class<?>> noThemeClasses,
        SerializableBiFunction<Class<?>, Class<? extends Annotation>, List<? extends Annotation>> annotationFinder)
        throws ClassNotFoundException {
    Class fakeThemeClass = Object.class;
    Class fakeNoThemeClass = Throwable.class;

    Mockito.when(finder.loadClass(Theme.class.getName()))
            .thenReturn(fakeThemeClass);
    Mockito.when(finder.loadClass(NoTheme.class.getName()))
            .thenReturn(fakeNoThemeClass);

    Mockito.when(finder.getAnnotatedClasses(fakeThemeClass))
            .thenReturn(themedClasses);
    Mockito.when(finder.getAnnotatedClasses(fakeNoThemeClass))
            .thenReturn(noThemeClasses);

    return new FullDependenciesScanner(finder, annotationFinder) {
        @Override
        protected Class<? extends AbstractTheme> getLumoTheme() {
            return FakeLumoTheme.class;
        }
    };
}
 
Example #4
Source File: AppLayoutRouterLayoutBase.java    From vaadin-app-layout with Apache License 2.0 5 votes vote down vote up
@Override
protected void onAttach(AttachEvent attachEvent) {
    super.onAttach(attachEvent);
    if (getClass().getAnnotation(Theme.class) != null && getClass().getAnnotation(Theme.class).value() != Lumo.class) {
        attachEvent.getUI().getPage().addStyleSheet("./com/github/appreciated/app-layout/app-layout-styles-material.css");
    } else {
        attachEvent.getUI().getPage().addStyleSheet("./com/github/appreciated/app-layout/app-layout-styles-lumo.css");
    }
    getUI().ifPresent(ui -> ui.addAfterNavigationListener(event -> {
        closeDrawerIfNotPersistent();
    }));
}
 
Example #5
Source File: FrontendClassVisitor.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public AnnotationVisitor visitAnnotation(String descriptor,
        boolean visible) {
    addSignatureToClasses(children, descriptor);

    // We return different visitor implementations depending on the
    // annotation
    String cname = descriptor.replace("/", ".");
    if (className.equals(endPoint.name)
            && cname.contains(Route.class.getName())) {
        return routeVisitor;
    }
    if (cname.contains(JsModule.class.getName())) {
        return jsModuleVisitor;
    }
    if (cname.contains(JavaScript.class.getName())) {
        return jScriptVisitor;
    }
    if (cname.contains(NoTheme.class.getName())) {
        if (className.equals(endPoint.name)) {
            endPoint.theme.notheme = true;
        }
        return null;
    }
    if (cname.contains(Theme.class.getName())) {
        if (className.equals(endPoint.name)) {
            return themeRouteVisitor;
        }
        if (className.equals(endPoint.layout)) {
            return themeLayoutVisitor;
        }
    }
    if (cname.contains(CssImport.class.getName())) {
        return new CssAnnotationVisitor(endPoint.css);
    }
    // default visitor
    return annotationVisitor;
}
 
Example #6
Source File: FullDependenciesScannerTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void getTheme_noThemeAndExplicitTheme_throws()
        throws ClassNotFoundException {
    setUpThemeScanner(getAnnotatedClasses(Theme.class),
            Collections.singleton(NoThemeComponent.class),
            (type, annotationType) -> findAnnotations(type, Theme.class));
}
 
Example #7
Source File: FullDependenciesScannerTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void getTheme_severalExplicitThemes_throws()
        throws ClassNotFoundException {
    Set<Class<?>> themeAnnotatedClasses = getAnnotatedClasses(Theme.class);
    themeAnnotatedClasses.add(ThemedComponent.class);
    setUpThemeScanner(themeAnnotatedClasses, Collections.emptySet(),
            (type, annotationType) -> findAnnotations(type, Theme.class));
}
 
Example #8
Source File: DevModeClassFinderTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void applicableClasses_knownClasses() {
    Collection<Class<?>> classes = getApplicableClasses();

    List<Class<?>> knownClasses = Arrays.asList(
        Route.class,
        UIInitListener.class,
        VaadinServiceInitListener.class,
        WebComponentExporter.class,
        WebComponentExporterFactory.class,
        NpmPackage.class,
        NpmPackage.Container.class,
        JsModule.class,
        JsModule.Container.class,
        JavaScript.class,
        JavaScript.Container.class,
        CssImport.class,
        CssImport.Container.class,
        Theme.class,
        NoTheme.class,
        HasErrorParameter.class);

    for (Class<?> clz : classes) {
        assertTrue("should be a known class " + clz.getName(), knownClasses.contains(clz));
    }
    Assert.assertEquals(knownClasses.size(), classes.size());
}
 
Example #9
Source File: FullDependenciesScanner.java    From flow with Apache License 2.0 4 votes vote down vote up
private ThemeData verifyTheme() {
    try {
        Class<? extends Annotation> loadedThemeAnnotation = getFinder()
                .loadClass(Theme.class.getName());

        Set<Class<?>> annotatedClasses = getFinder()
                .getAnnotatedClasses(loadedThemeAnnotation);
        Set<ThemeData> themes = annotatedClasses.stream()
                .flatMap(clazz -> annotationFinder
                        .apply(clazz, loadedThemeAnnotation).stream())
                .map(theme -> new ThemeData(
                        ((Class<?>) invokeAnnotationMethod(theme, VALUE))
                                .getName(),
                        invokeAnnotationMethodAsString(theme, "variant")))
                .collect(Collectors.toSet());

        Class<? extends Annotation> loadedNoThemeAnnotation = getFinder()
                .loadClass(NoTheme.class.getName());

        Set<Class<?>> notThemeClasses = getFinder()
                .getAnnotatedClasses(loadedNoThemeAnnotation).stream()
                .collect(Collectors.toSet());
        if (themes.size() > 1) {
            throw new IllegalStateException(
                    "Using multiple different Theme configurations is not "
                            + "supported. The list of found themes:\n"
                            + getThemesList(themes));
        }
        if (!themes.isEmpty() && !notThemeClasses.isEmpty()) {
            throw new IllegalStateException("@"
                    + Theme.class.getSimpleName() + " ("
                    + getThemesList(themes) + ") and @"
                    + NoTheme.class.getSimpleName()
                    + " annotations can't be used simultaneously.");
        }
        if (!notThemeClasses.isEmpty()) {
            return ThemeData.createNoTheme();

        }
        return themes.isEmpty() ? null : themes.iterator().next();
    } catch (ClassNotFoundException exception) {
        throw new IllegalStateException(
                "Could not load theme annotation class", exception);
    }
}
 
Example #10
Source File: FullDependenciesScannerTest.java    From flow with Apache License 2.0 4 votes vote down vote up
@Test
public void getModules_explcitTheme_returnAllModulesExcludingNotUsedTheme_getClassesReturnAllModuleAnnotatedComponents()
        throws ClassNotFoundException {
    // use this fake/mock class for the loaded class to check that annotated
    // classes are requested for the loaded class and not for the
    // annotationType
    Class clazz = Object.class;

    Mockito.when(finder.loadClass(JsModule.class.getName()))
            .thenReturn(clazz);

    Mockito.when(finder.getAnnotatedClasses(clazz))
            .thenReturn(getAnnotatedClasses(JsModule.class));

    Class themeClass = Throwable.class;

    Mockito.when(finder.loadClass(Theme.class.getName()))
            .thenReturn(themeClass);

    Set<Class<?>> themeClasses = getAnnotatedClasses(Theme.class);
    themeClasses.add(FakeLumoTheme.class);
    Mockito.when(finder.getAnnotatedClasses(themeClass))
            .thenReturn(themeClasses);
    Assert.assertTrue(themeClasses.size() >= 2);

    Mockito.when(finder.loadClass(LumoTest.class.getName()))
            .thenReturn((Class) LumoTest.class);
    Mockito.when(finder.loadClass(FakeLumoTheme.class.getName()))
            .thenReturn((Class) FakeLumoTheme.class);

    FrontendDependenciesScanner scanner = new FullDependenciesScanner(
            finder, (type, annotation) -> {
                if (annotation.equals(clazz)) {
                    return findAnnotations(type, JsModule.class);
                } else if (annotation.equals(themeClass)) {
                    return findAnnotations(type, Theme.class);
                }
                Assert.fail();
                return null;
            });

    List<String> modules = scanner.getModules();
    Assert.assertEquals(23, modules.size());
    assertJsModules(modules);

    // Theme modules should be included now
    Assert.assertTrue(
            modules.contains("@vaadin/vaadin-lumo-styles/color.js"));
    Assert.assertTrue(
            modules.contains("@vaadin/vaadin-lumo-styles/typography.js"));
    Assert.assertTrue(
            modules.contains("@vaadin/vaadin-lumo-styles/sizing.js"));
    Assert.assertTrue(
            modules.contains("@vaadin/vaadin-lumo-styles/spacing.js"));
    Assert.assertTrue(
            modules.contains("@vaadin/vaadin-lumo-styles/style.js"));
    Assert.assertTrue(
            modules.contains("@vaadin/vaadin-lumo-styles/icons.js"));

    // not used theme module is not included
    Assert.assertFalse(modules.contains("./foo-bar-baz.js"));

    Set<String> classes = scanner.getClasses();
    Assert.assertEquals(13, classes.size());

    assertJsModulesClasses(classes);
    Assert.assertTrue(classes.contains(LumoTest.class.getName()));
    Assert.assertFalse(classes.contains(FakeLumoTheme.class.getName()));
}