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));
}
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);
}
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));
}
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);
}
}
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());
}
Aggregations