Support regular and SWT CSS in CMS Theme.
authorMathieu Baudier <mbaudier@argeo.org>
Wed, 14 Oct 2020 07:23:05 +0000 (09:23 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Wed, 14 Oct 2020 07:23:05 +0000 (09:23 +0200)
org.argeo.cms.ui/src/org/argeo/cms/ui/CmsTheme.java
org.argeo.cms.ui/src/org/argeo/cms/ui/util/BundleCmsTheme.java

index bd4daec2eec6cf53c59fad747e27a9d0681330b6..4b3d186f7ae47cb3e2059befb3bd7376b781d595 100644 (file)
@@ -19,9 +19,15 @@ public interface CmsTheme {
         */
        InputStream getResourceAsStream(String resourceName) throws IOException;
 
+       /** Relative paths to standard web CSS. */
+       Set<String> getWebCssPaths();
+
        /** Relative paths to RAP specific CSS. */
        Set<String> getRapCssPaths();
 
+       /** Relative paths to SWT specific CSS. */
+       Set<String> getSwtCssPaths();
+
        /** Relative paths to images such as icons. */
        Set<String> 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 <code>null</code> if not found.
index 1076b6ca2b72f9351d2aa065b9526420fdcf5855..a4749fbabe150f4624782f45ead79e274dc7fd1f 100644 (file)
@@ -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<String> webCssPaths = new TreeSet<>();
        private Set<String> rapCssPaths = new TreeSet<>();
+       private Set<String> swtCssPaths = new TreeSet<>();
        private Set<String> imagesPaths = new TreeSet<>();
 
        private String headerCss;
        private List<String> 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<URL> themeResources = themeBundle.findEntries(rapCssPath, "*.css", true);
+       Set<String> addCss(Bundle themeBundle, String path) {
+               Set<String> paths = new TreeSet<>();
+               Enumeration<URL> 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<URL> 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<String> getWebCssPaths() {
+               return webCssPaths;
+       }
+
        @Override
        public Set<String> getRapCssPaths() {
                return rapCssPaths;
        }
 
+       @Override
+       public Set<String> getSwtCssPaths() {
+               return swtCssPaths;
+       }
+
        @Override
        public Set<String> 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;