Fonts support in CMS Theme.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / ui / util / BundleCmsTheme.java
index a4749fbabe150f4624782f45ead79e274dc7fd1f..3b2669e02242e9f96c39238f5c4f804695933982 100644 (file)
@@ -40,6 +40,7 @@ public class BundleCmsTheme extends AbstractCmsTheme {
        private Set<String> rapCssPaths = new TreeSet<>();
        private Set<String> swtCssPaths = new TreeSet<>();
        private Set<String> imagesPaths = new TreeSet<>();
+       private Set<String> fontsPaths = new TreeSet<>();
 
        private String headerCss;
        private List<String> fonts = new ArrayList<>();
@@ -91,12 +92,15 @@ public class BundleCmsTheme extends AbstractCmsTheme {
                webCssPaths = addCss(themeBundle, "/css/");
                rapCssPaths = addCss(themeBundle, "/rap/");
                swtCssPaths = addCss(themeBundle, "/swt/");
-               addResources("*.png");
-               addResources("*.gif");
-               addResources("*.jpg");
-               addResources("*.jpeg");
-               addResources("*.svg");
-               addResources("*.ico");
+               addImages("*.png");
+               addImages("*.gif");
+               addImages("*.jpg");
+               addImages("*.jpeg");
+               addImages("*.svg");
+               addImages("*.ico");
+
+               addFonts("*.woff");
+               addFonts("*.woff2");
 
                // fonts
                URL fontsUrl = themeBundle.getEntry(basePath + "fonts.txt");
@@ -135,28 +139,30 @@ public class BundleCmsTheme extends AbstractCmsTheme {
 
        Set<String> addCss(Bundle themeBundle, String path) {
                Set<String> paths = new TreeSet<>();
-               Enumeration<URL> themeResources = themeBundle.findEntries(path, "*.css", true);
-               if (themeResources == null)
-                       return paths;
-               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);
-                       }
-               }
 
                // 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);
+               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<URL> 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;
@@ -176,7 +182,7 @@ public class BundleCmsTheme extends AbstractCmsTheme {
                }
        }
 
-       void addResources(String pattern) {
+       void addImages(String pattern) {
                Enumeration<URL> themeResources = themeBundle.findEntries(basePath, pattern, true);
                if (themeResources == null)
                        return;
@@ -195,6 +201,25 @@ public class BundleCmsTheme extends AbstractCmsTheme {
 
        }
 
+       void addFonts(String pattern) {
+               Enumeration<URL> 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);
@@ -252,6 +277,11 @@ public class BundleCmsTheme extends AbstractCmsTheme {
                return imagesPaths;
        }
 
+       @Override
+       public Set<String> getFontsPaths() {
+               return fontsPaths;
+       }
+
        @Override
        public InputStream loadPath(String path) throws IOException {
                URL url = themeBundle.getResource(path);
@@ -277,4 +307,14 @@ public class BundleCmsTheme extends AbstractCmsTheme {
                return themeBundle;
        }
 
+       @Override
+       public int hashCode() {
+               return themeId.hashCode();
+       }
+
+       @Override
+       public String toString() {
+               return "Bundle CMS Theme " + themeId;
+       }
+
 }