X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms.ui%2Fsrc%2Forg%2Fargeo%2Fcms%2Fui%2Futil%2FBundleCmsTheme.java;h=9a3cf9d47d87b653fcb5de6d346284e27950dc2c;hb=ecf8ab7382eadecab5a93261f261d0fd2168ffa8;hp=1076b6ca2b72f9351d2aa065b9526420fdcf5855;hpb=1233dcea2383bfe5c83e5ec33d0c502afff22601;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms.ui/src/org/argeo/cms/ui/util/BundleCmsTheme.java b/org.argeo.cms.ui/src/org/argeo/cms/ui/util/BundleCmsTheme.java index 1076b6ca2..9a3cf9d47 100644 --- a/org.argeo.cms.ui/src/org/argeo/cms/ui/util/BundleCmsTheme.java +++ b/org.argeo.cms.ui/src/org/argeo/cms/ui/util/BundleCmsTheme.java @@ -15,8 +15,6 @@ import java.util.Set; import java.util.TreeSet; import java.util.stream.Collectors; -import org.argeo.cms.ui.CmsTheme; -import org.eclipse.swt.graphics.Image; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; @@ -35,17 +33,26 @@ public class BundleCmsTheme extends AbstractCmsTheme { public final static String CMS_THEME_PROPERTY = "argeo.cms.theme"; public final static String CMS_THEME_BUNDLE_PROPERTY = "argeo.cms.theme.bundle"; + private final static String HEADER_CSS = "header.css"; + private final static String FONTS_TXT = "fonts.txt"; + // private final static Log log = LogFactory.getLog(BundleCmsTheme.class); private String themeId; + private Set webCssPaths = new TreeSet<>(); private Set rapCssPaths = new TreeSet<>(); + private Set swtCssPaths = new TreeSet<>(); private Set imagesPaths = new TreeSet<>(); + private Set fontsPaths = new TreeSet<>(); private String headerCss; private List fonts = new ArrayList<>(); private String basePath; - private String rapCssPath; + private String styleCssPath; +// private String webCssPath; +// private String rapCssPath; +// private String swtCssPath; private Bundle themeBundle; public BundleCmsTheme() { @@ -79,26 +86,37 @@ public class BundleCmsTheme extends AbstractCmsTheme { themeBundle = findThemeBundle(bundleContext, symbolicName); } basePath = "/"; - rapCssPath = "/rap/"; + styleCssPath = "/style/"; +// webCssPath = "/css/"; +// rapCssPath = "/rap/"; +// swtCssPath = "/swt/"; // this.themeId = RWT.DEFAULT_THEME_ID; this.themeId = themeBundle.getSymbolicName(); - addRapStyleSheets(themeBundle); - addResources("*.png"); - addResources("*.gif"); - addResources("*.jpg"); - addResources("*.jpeg"); - addResources("*.svg"); - addResources("*.ico"); + webCssPaths = addCss(themeBundle, "/css/"); + rapCssPaths = addCss(themeBundle, "/rap/"); + swtCssPaths = addCss(themeBundle, "/swt/"); + addImages("*.png"); + addImages("*.gif"); + addImages("*.jpg"); + addImages("*.jpeg"); + addImages("*.svg"); + addImages("*.ico"); + + addFonts("*.woff"); + addFonts("*.woff2"); // fonts - URL fontsUrl = themeBundle.getEntry(basePath + "fonts.txt"); + URL fontsUrl = themeBundle.getEntry(basePath + FONTS_TXT); if (fontsUrl != null) { loadFontsUrl(fontsUrl); } // common CSS header (plain CSS) - URL headerCssUrl = themeBundle.getEntry(basePath + "header.css"); + URL headerCssUrl = themeBundle.getEntry(basePath + HEADER_CSS); if (headerCssUrl != null) { + // added to plain Web CSS + webCssPaths.add(basePath + HEADER_CSS); + // and it will also be used by RAP: try (BufferedReader buffer = new BufferedReader(new InputStreamReader(headerCssUrl.openStream(), UTF_8))) { headerCss = buffer.lines().collect(Collectors.joining("\n")); } catch (IOException e) { @@ -125,23 +143,35 @@ public class BundleCmsTheme extends AbstractCmsTheme { return sb.toString(); } - void addRapStyleSheets(Bundle themeBundle) { - Enumeration themeResources = themeBundle.findEntries(rapCssPath, "*.css", true); - if (themeResources == null) - return; - while (themeResources.hasMoreElements()) { - String resource = themeResources.nextElement().getPath(); - // remove first '/' so that RWT registers it - resource = resource.substring(1); - if (!resource.endsWith("/")) { -// if (rapCss.containsKey(resource)) -// log.warn("Overriding " + resource + " from " + themeBundle.getSymbolicName()); -// rapCss.put(resource, ssRL); - rapCssPaths.add(resource); + Set addCss(Bundle themeBundle, String path) { + Set paths = new TreeSet<>(); + + // common CSS + Enumeration commonResources = themeBundle.findEntries(styleCssPath, "*.css", true); + if (commonResources != null) { + while (commonResources.hasMoreElements()) { + String resource = commonResources.nextElement().getPath(); + // remove first '/' so that RWT registers it + resource = resource.substring(1); + if (!resource.endsWith("/")) { + paths.add(resource); + } } - } + // specific CSS + Enumeration themeResources = themeBundle.findEntries(path, "*.css", true); + if (themeResources != null) { + while (themeResources.hasMoreElements()) { + String resource = themeResources.nextElement().getPath(); + // remove first '/' so that RWT registers it + resource = resource.substring(1); + if (!resource.endsWith("/")) { + paths.add(resource); + } + } + } + return paths; } void loadFontsUrl(URL url) { @@ -158,7 +188,7 @@ public class BundleCmsTheme extends AbstractCmsTheme { } } - void addResources(String pattern) { + void addImages(String pattern) { Enumeration themeResources = themeBundle.findEntries(basePath, pattern, true); if (themeResources == null) return; @@ -177,6 +207,25 @@ public class BundleCmsTheme extends AbstractCmsTheme { } + void addFonts(String pattern) { + Enumeration themeResources = themeBundle.findEntries(basePath, pattern, true); + if (themeResources == null) + return; + while (themeResources.hasMoreElements()) { + String resource = themeResources.nextElement().getPath(); + // remove first '/' so that RWT registers it + resource = resource.substring(1); + if (!resource.endsWith("/")) { +// if (resources.containsKey(resource)) +// log.warn("Overriding " + resource + " from " + themeBundle.getSymbolicName()); +// resources.put(resource, themeBRL); + fontsPaths.add(resource); + } + + } + + } + @Override public InputStream getResourceAsStream(String resourceName) throws IOException { URL res = themeBundle.getEntry(resourceName); @@ -193,7 +242,6 @@ public class BundleCmsTheme extends AbstractCmsTheme { public String getThemeId() { return themeId; } - // public void setThemeId(String themeId) { // this.themeId = themeId; @@ -215,16 +263,40 @@ public class BundleCmsTheme extends AbstractCmsTheme { // this.rapCssPath = cssPath; // } + @Override + public Set getWebCssPaths() { + return webCssPaths; + } + @Override public Set getRapCssPaths() { return rapCssPaths; } + @Override + public Set getSwtCssPaths() { + return swtCssPaths; + } + @Override public Set getImagesPaths() { return imagesPaths; } + @Override + public Set getFontsPaths() { + return fontsPaths; + } + + @Override + public InputStream loadPath(String path) throws IOException { + URL url = themeBundle.getResource(path); + if (url == null) + throw new IllegalArgumentException( + "Path " + path + " not found in bundle " + themeBundle.getSymbolicName()); + return url.openStream(); + } + private static Bundle findThemeBundle(BundleContext bundleContext, String themeId) { if (themeId == null) return null; @@ -241,4 +313,14 @@ public class BundleCmsTheme extends AbstractCmsTheme { return themeBundle; } + @Override + public int hashCode() { + return themeId.hashCode(); + } + + @Override + public String toString() { + return "Bundle CMS Theme " + themeId; + } + }