From: Mathieu Baudier Date: Wed, 14 Oct 2020 07:23:05 +0000 (+0200) Subject: Support regular and SWT CSS in CMS Theme. X-Git-Tag: argeo-commons-2.1.89~66 X-Git-Url: https://git.argeo.org/?p=lgpl%2Fargeo-commons.git;a=commitdiff_plain;h=ed21871d5963cd7a53c96b23f2e81ec78416001d Support regular and SWT CSS in CMS Theme. --- diff --git a/org.argeo.cms.ui/src/org/argeo/cms/ui/CmsTheme.java b/org.argeo.cms.ui/src/org/argeo/cms/ui/CmsTheme.java index bd4daec2e..4b3d186f7 100644 --- a/org.argeo.cms.ui/src/org/argeo/cms/ui/CmsTheme.java +++ b/org.argeo.cms.ui/src/org/argeo/cms/ui/CmsTheme.java @@ -19,9 +19,15 @@ public interface CmsTheme { */ InputStream getResourceAsStream(String resourceName) throws IOException; + /** Relative paths to standard web CSS. */ + Set getWebCssPaths(); + /** Relative paths to RAP specific CSS. */ Set getRapCssPaths(); + /** Relative paths to SWT specific CSS. */ + Set getSwtCssPaths(); + /** Relative paths to images such as icons. */ Set getImagesPaths(); @@ -34,6 +40,9 @@ public interface CmsTheme { /** The default icon size (typically the smallest). */ Integer getDefaultIconSize(); + /** Loads one of the relative path provided by the other methods. */ + InputStream loadPath(String path) throws IOException; + /** * And icon with this file name (without the extension), with a best effort to * find the appropriate size, or null if not found. 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..a4749fbab 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; @@ -38,14 +36,19 @@ public class BundleCmsTheme extends AbstractCmsTheme { // 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 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,10 +82,15 @@ 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); + webCssPaths = addCss(themeBundle, "/css/"); + rapCssPaths = addCss(themeBundle, "/rap/"); + swtCssPaths = addCss(themeBundle, "/swt/"); addResources("*.png"); addResources("*.gif"); addResources("*.jpg"); @@ -125,23 +133,33 @@ public class BundleCmsTheme extends AbstractCmsTheme { return sb.toString(); } - void addRapStyleSheets(Bundle themeBundle) { - Enumeration themeResources = themeBundle.findEntries(rapCssPath, "*.css", true); + Set addCss(Bundle themeBundle, String path) { + Set paths = new TreeSet<>(); + Enumeration themeResources = themeBundle.findEntries(path, "*.css", true); if (themeResources == null) - return; + return paths; 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); + paths.add(resource); } - } + // common CSS + Enumeration commonResources = themeBundle.findEntries(styleCssPath, "*.css", true); + if (commonResources == null) + return paths; + 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); + } + } + return paths; } void loadFontsUrl(URL url) { @@ -193,7 +211,6 @@ public class BundleCmsTheme extends AbstractCmsTheme { public String getThemeId() { return themeId; } - // public void setThemeId(String themeId) { // this.themeId = themeId; @@ -215,16 +232,35 @@ 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 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;