Massive package refactoring
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / osgi / BundleCmsTheme.java
index 75a2212b65ce5fe6f3a51e07b7b5e925c1dce8db..f60d3352e98c61c921b260c7be244183b91c085b 100644 (file)
@@ -7,17 +7,17 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.URL;
-import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Enumeration;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeSet;
 import java.util.stream.Collectors;
 
-import org.apache.commons.io.IOUtils;
-import org.argeo.api.cms.CmsTheme;
+import org.argeo.api.cms.ux.CmsTheme;
+import org.argeo.cms.util.StreamUtils;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 
@@ -31,18 +31,28 @@ import org.osgi.framework.BundleContext;
  * <code>/ ** /*.{png,gif,jpeg,...}</code>.<br>
  */
 public class BundleCmsTheme implements CmsTheme {
-       public final static String DEFAULT_CMS_THEME_BUNDLE = "org.argeo.theme.argeo2";
+//     public final static String DEFAULT_CMS_THEME_BUNDLE = "org.argeo.theme.argeo2";
 
-       public final static String CMS_THEME_PROPERTY = "argeo.cms.theme";
+//     public final static String CMS_THEME_PROPERTY = "argeo.cms.theme";
+       @Deprecated
        public final static String CMS_THEME_BUNDLE_PROPERTY = "argeo.cms.theme.bundle";
 
+       /** Declared theme ID, to be used by OSGi services to reference it as parent. */
+       public final static String THEME_ID_PROPERTY = "themeId";
+       public final static String SMALL_ICON_SIZE_PROPERTY = "smallIconSize";
+       public final static String BIG_ICON_SIZE_PROPERTY = "bigIconSize";
+
        private final static String HEADER_CSS = "header.css";
        private final static String FONTS_TXT = "fonts.txt";
        private final static String BODY_HTML = "body.html";
 
 //     private final static Log log = LogFactory.getLog(BundleCmsTheme.class);
 
+       private CmsTheme parentTheme;
+
        private String themeId;
+       private String declaredThemeId;;
+
        private Set<String> webCssPaths = new TreeSet<>();
        private Set<String> rapCssPaths = new TreeSet<>();
        private Set<String> swtCssPaths = new TreeSet<>();
@@ -52,7 +62,7 @@ public class BundleCmsTheme implements CmsTheme {
        private String headerCss;
        private List<String> fonts = new ArrayList<>();
 
-       private String bodyHtml="<body></body>";
+       private String bodyHtml = "<body></body>";
 
        private String basePath;
        private String styleCssPath;
@@ -61,13 +71,20 @@ public class BundleCmsTheme implements CmsTheme {
 //     private String swtCssPath;
        private Bundle themeBundle;
 
-       private Integer defaultIconSize = 16;
+       private Integer smallIconSize = 16;
+       private Integer bigIconSize = 32;
 
        public BundleCmsTheme() {
 
        }
 
        public void init(BundleContext bundleContext, Map<String, String> properties) {
+               declaredThemeId = properties.get(THEME_ID_PROPERTY);
+               if (properties.containsKey(SMALL_ICON_SIZE_PROPERTY))
+                       smallIconSize = Integer.valueOf(properties.get(SMALL_ICON_SIZE_PROPERTY));
+               if (properties.containsKey(BIG_ICON_SIZE_PROPERTY))
+                       smallIconSize = Integer.valueOf(properties.get(BIG_ICON_SIZE_PROPERTY));
+
                initResources(bundleContext, null);
        }
 
@@ -100,6 +117,10 @@ public class BundleCmsTheme implements CmsTheme {
 //             swtCssPath = "/swt/";
 //             this.themeId = RWT.DEFAULT_THEME_ID;
                this.themeId = themeBundle.getSymbolicName();
+               if (declaredThemeId != null && !declaredThemeId.equals(themeId))
+                       throw new IllegalArgumentException(
+                                       "Declared theme id " + declaredThemeId + " is different from " + themeId);
+
                webCssPaths = addCss(themeBundle, "/css/");
                rapCssPaths = addCss(themeBundle, "/rap/");
                swtCssPaths = addCss(themeBundle, "/swt/");
@@ -137,7 +158,7 @@ public class BundleCmsTheme implements CmsTheme {
                if (bodyUrl != null) {
                        loadBodyHtml(bodyUrl);
                }
-}
+       }
 
        public String getHtmlHeaders() {
                StringBuilder sb = new StringBuilder();
@@ -156,8 +177,6 @@ public class BundleCmsTheme implements CmsTheme {
                else
                        return sb.toString();
        }
-       
-       
 
        @Override
        public String getBodyHtml() {
@@ -211,7 +230,7 @@ public class BundleCmsTheme implements CmsTheme {
 
        void loadBodyHtml(URL url) {
                try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), UTF_8))) {
-               bodyHtml=       IOUtils.toString(url,StandardCharsets.UTF_8);
+                       bodyHtml = StreamUtils.toString(in);
                } catch (IOException e) {
                        throw new IllegalArgumentException("Cannot load URL " + url, e);
                }
@@ -260,10 +279,11 @@ public class BundleCmsTheme implements CmsTheme {
                URL res = themeBundle.getEntry(resourceName);
                if (res == null) {
                        res = themeBundle.getResource(resourceName);
-                       if (res == null)
-                               return null;
-//                             throw new IllegalArgumentException(
-//                                             "Resource " + resourceName + " not found in bundle " + themeBundle.getSymbolicName());
+                       if (res == null) {
+                               if (parentTheme == null)
+                                       return null;
+                               return parentTheme.getResourceAsStream(resourceName);
+                       }
                }
                return res.openStream();
        }
@@ -272,43 +292,43 @@ public class BundleCmsTheme implements CmsTheme {
                return themeId;
        }
 
-//     public void setThemeId(String themeId) {
-//             this.themeId = themeId;
-//     }
-//
-//     public String getBasePath() {
-//             return basePath;
-//     }
-//
-//     public void setBasePath(String basePath) {
-//             this.basePath = basePath;
-//     }
-//
-//     public String getRapCssPath() {
-//             return rapCssPath;
-//     }
-//
-//     public void setRapCssPath(String cssPath) {
-//             this.rapCssPath = cssPath;
-//     }
-
        @Override
        public Set<String> getWebCssPaths() {
+               if (parentTheme != null) {
+                       Set<String> res = new HashSet<>(parentTheme.getWebCssPaths());
+                       res.addAll(webCssPaths);
+                       return res;
+               }
                return webCssPaths;
        }
 
        @Override
        public Set<String> getRapCssPaths() {
+               if (parentTheme != null) {
+                       Set<String> res = new HashSet<>(parentTheme.getRapCssPaths());
+                       res.addAll(rapCssPaths);
+                       return res;
+               }
                return rapCssPaths;
        }
 
        @Override
        public Set<String> getSwtCssPaths() {
+               if (parentTheme != null) {
+                       Set<String> res = new HashSet<>(parentTheme.getSwtCssPaths());
+                       res.addAll(swtCssPaths);
+                       return res;
+               }
                return swtCssPaths;
        }
 
        @Override
        public Set<String> getImagesPaths() {
+               if (parentTheme != null) {
+                       Set<String> res = new HashSet<>(parentTheme.getImagesPaths());
+                       res.addAll(imagesPaths);
+                       return res;
+               }
                return imagesPaths;
        }
 
@@ -318,17 +338,27 @@ public class BundleCmsTheme implements CmsTheme {
        }
 
        @Override
-       public Integer getDefaultIconSize() {
-               return defaultIconSize;
+       public int getSmallIconSize() {
+               return smallIconSize;
+       }
+
+       @Override
+       public int getBigIconSize() {
+               return bigIconSize;
        }
 
        @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();
+               if (url == null) {
+                       if (parentTheme != null)
+                               return parentTheme.loadPath(path);
+                       else
+                               throw new IllegalArgumentException(
+                                               "Path " + path + " not found in bundle " + themeBundle.getSymbolicName());
+               } else {
+                       return url.openStream();
+               }
        }
 
        private static Bundle findThemeBundle(BundleContext bundleContext, String themeId) {
@@ -357,4 +387,16 @@ public class BundleCmsTheme implements CmsTheme {
                return "Bundle CMS Theme " + themeId;
        }
 
+       public void setParentTheme(CmsTheme parentTheme) {
+               this.parentTheme = parentTheme;
+       }
+
+       public void setSmallIconSize(Integer smallIconSize) {
+               this.smallIconSize = smallIconSize;
+       }
+
+       public void setBigIconSize(Integer bigIconSize) {
+               this.bigIconSize = bigIconSize;
+       }
+
 }