Improve CMS UI utilities.
authorMathieu Baudier <mbaudier@argeo.org>
Fri, 16 Jul 2021 08:17:57 +0000 (10:17 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Fri, 16 Jul 2021 08:17:57 +0000 (10:17 +0200)
org.argeo.cms.ui/src/org/argeo/cms/ui/CmsStyles.java
org.argeo.cms.ui/src/org/argeo/cms/ui/util/BundleCmsTheme.java
org.argeo.cms.ui/src/org/argeo/cms/ui/util/CmsLink.java
org.argeo.cms.ui/src/org/argeo/cms/ui/util/CmsStyle.java
org.argeo.cms.ui/src/org/argeo/cms/ui/util/CmsUiUtils.java
org.argeo.cms.ui/src/org/argeo/cms/ui/util/DefaultImageManager.java
org.argeo.cms.ui/src/org/argeo/cms/ui/util/SimpleStyle.java [new file with mode: 0644]

index 506e971ccb14191b131e9c315b0a13dd0ad509e3..678a497170f60cf90cd7ff7a1f3c5d6b2f6679f8 100644 (file)
@@ -1,6 +1,7 @@
 package org.argeo.cms.ui;
 
 /** Styles references in the CSS. */
+@Deprecated
 public interface CmsStyles {
        // General
        public final static String CMS_SHELL = "cms_shell";
index 3b2669e02242e9f96c39238f5c4f804695933982..9a3cf9d47d87b653fcb5de6d346284e27950dc2c 100644 (file)
@@ -33,6 +33,9 @@ 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;
@@ -103,14 +106,17 @@ public class BundleCmsTheme extends AbstractCmsTheme {
                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) {
index b3c4a8a579914f665469004d2ba4cafe9150e74a..4cad1de60fa74eb2f2fb487a6bd6dd008d5b7b63 100644 (file)
@@ -1,5 +1,6 @@
 package org.argeo.cms.ui.util;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -7,14 +8,12 @@ import java.net.URL;
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 
-import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.api.NodeUtils;
-import org.argeo.cms.CmsException;
 import org.argeo.cms.auth.CurrentUser;
-import org.argeo.cms.ui.CmsStyles;
 import org.argeo.cms.ui.CmsUiProvider;
+import org.argeo.jcr.JcrException;
 import org.eclipse.rap.rwt.RWT;
 import org.eclipse.rap.rwt.service.ResourceManager;
 import org.eclipse.swt.SWT;
@@ -32,9 +31,10 @@ public class CmsLink implements CmsUiProvider {
        private BundleContext bundleContext;
 
        private String label;
-       private String custom;
+       private String style;
        private String target;
        private String image;
+       private boolean openNew = false;
        private MouseListener mouseListener;
 
        private int horizontalAlignment = SWT.CENTER;
@@ -52,14 +52,18 @@ public class CmsLink implements CmsUiProvider {
        }
 
        public CmsLink(String label, String target) {
-               this(label, target, null);
+               this(label, target, (String) null);
        }
 
-       public CmsLink(String label, String target, String custom) {
+       public CmsLink(String label, String target, CmsStyle style) {
+               this(label, target, style != null ? style.style() : null);
+       }
+
+       public CmsLink(String label, String target, String style) {
                super();
                this.label = label;
                this.target = target;
-               this.custom = custom;
+               this.style = style;
                init();
        }
 
@@ -89,7 +93,7 @@ public class CmsLink implements CmsUiProvider {
                comp.setLayout(CmsUiUtils.noSpaceGridLayout());
 
                Label link = new Label(comp, SWT.NONE);
-               link.setData(RWT.MARKUP_ENABLED, Boolean.TRUE);
+               CmsUiUtils.markup(link);
                GridData layoutData = new GridData(horizontalAlignment, verticalAlignment, false, false);
                if (image != null) {
                        if (imageHeight != null)
@@ -100,13 +104,8 @@ public class CmsLink implements CmsUiProvider {
                }
 
                link.setLayoutData(layoutData);
-               if (custom != null) {
-                       comp.setData(RWT.CUSTOM_VARIANT, custom);
-                       link.setData(RWT.CUSTOM_VARIANT, custom);
-               } else {
-                       comp.setData(RWT.CUSTOM_VARIANT, CmsStyles.CMS_LINK);
-                       link.setData(RWT.CUSTOM_VARIANT, CmsStyles.CMS_LINK);
-               }
+               CmsUiUtils.style(comp, style != null ? style : getDefaultStyle());
+               CmsUiUtils.style(link, style != null ? style : getDefaultStyle());
 
                // label
                StringBuilder labelText = new StringBuilder();
@@ -118,16 +117,19 @@ public class CmsLink implements CmsUiProvider {
                                        String homePath = homeNode.getPath();
                                        labelText.append("/#" + homePath);
                                } catch (RepositoryException e) {
-                                       throw new CmsException("Cannot get home path", e);
+                                       throw new JcrException("Cannot get home path", e);
                                }
                        } else {
                                labelText.append(loggedInTarget);
                        }
                        labelText.append("\">");
                } else if (target != null) {
-                       labelText.append("<a style='color:inherit;text-decoration:inherit;' href=\"");
-                       labelText.append(target);
-                       labelText.append("\">");
+                       labelText.append("<a style='color:inherit;text-decoration:inherit;' href='");
+                       labelText.append(target).append("'");
+                       if (openNew) {
+                               labelText.append(" target='_blank'");
+                       }
+                       labelText.append(">");
                }
                if (image != null) {
                        registerImageIfNeeded();
@@ -162,17 +164,12 @@ public class CmsLink implements CmsUiProvider {
                ResourceManager resourceManager = RWT.getResourceManager();
                if (!resourceManager.isRegistered(image)) {
                        URL res = getImageUrl();
-                       InputStream inputStream = null;
-                       try {
-                               IOUtils.closeQuietly(inputStream);
-                               inputStream = res.openStream();
+                       try (InputStream inputStream = res.openStream()) {
                                resourceManager.register(image, inputStream);
                                if (log.isTraceEnabled())
                                        log.trace("Registered image " + image);
-                       } catch (Exception e) {
-                               throw new CmsException("Cannot load image " + image, e);
-                       } finally {
-                               IOUtils.closeQuietly(inputStream);
+                       } catch (IOException e) {
+                               throw new RuntimeException("Cannot load image " + image, e);
                        }
                }
        }
@@ -180,16 +177,12 @@ public class CmsLink implements CmsUiProvider {
        private ImageData loadImage() {
                URL url = getImageUrl();
                ImageData result = null;
-               InputStream inputStream = null;
-               try {
-                       inputStream = url.openStream();
+               try (InputStream inputStream = url.openStream()) {
                        result = new ImageData(inputStream);
                        if (log.isTraceEnabled())
                                log.trace("Loaded image " + image);
-               } catch (Exception e) {
-                       throw new CmsException("Cannot load image " + image, e);
-               } finally {
-                       IOUtils.closeQuietly(inputStream);
+               } catch (IOException e) {
+                       throw new RuntimeException("Cannot load image " + image, e);
                }
                return result;
        }
@@ -204,7 +197,7 @@ public class CmsLink implements CmsUiProvider {
                }
 
                if (url == null)
-                       throw new CmsException("No image " + image + " available.");
+                       throw new IllegalStateException("No image " + image + " available.");
 
                return url;
        }
@@ -217,8 +210,14 @@ public class CmsLink implements CmsUiProvider {
                this.label = label;
        }
 
+       public void setStyle(String style) {
+               this.style = style;
+       }
+
+       /** @deprecated Use {@link #setStyle(String)} instead. */
+       @Deprecated
        public void setCustom(String custom) {
-               this.custom = custom;
+               this.style = custom;
        }
 
        public void setTarget(String target) {
@@ -255,7 +254,8 @@ public class CmsLink implements CmsUiProvider {
                } else if ("center".equals(vAlign)) {
                        verticalAlignment = SWT.CENTER;
                } else {
-                       throw new CmsException("Unsupported vertical allignment " + vAlign + " (must be: top, bottom or center)");
+                       throw new IllegalArgumentException(
+                                       "Unsupported vertical alignment " + vAlign + " (must be: top, bottom or center)");
                }
        }
 
@@ -271,4 +271,11 @@ public class CmsLink implements CmsUiProvider {
                this.imageHeight = imageHeight;
        }
 
+       public void setOpenNew(boolean openNew) {
+               this.openNew = openNew;
+       }
+
+       protected String getDefaultStyle() {
+               return SimpleStyle.link.name();
+       }
 }
index ddbe485ea427ab4aed52bb14c376d16f7aad8614..ed2244b8ddce03e283df7b90f4d3a867a0529f54 100644 (file)
@@ -4,17 +4,19 @@ package org.argeo.cms.ui.util;
 public interface CmsStyle {
        String name();
 
+       /** @deprecated use {@link #style()} instead. */
        @Deprecated
        default String toStyleClass() {
-               return getClassPrefix() + "-" + name();
+               return style();
        }
 
        default String style() {
-               return getClassPrefix() + "-" + name();
+               String classPrefix = getClassPrefix();
+               return "".equals(classPrefix) ? name() : classPrefix + "-" + name();
        }
 
        default String getClassPrefix() {
-               return "cms";
+               return "";
        }
 
 }
index 428c910e54133dee353aa8d034ffc388fb8f30a5..8b10ef123a3bb9a62122496bfb848e94bf3ee790 100644 (file)
@@ -4,8 +4,11 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.StringTokenizer;
 
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
@@ -113,6 +116,25 @@ public class CmsUiUtils implements CmsConstants {
                return NodeUtils.getDataPath(cn, node);
        }
 
+       /** Clean reserved URL characters for use in HTTP links. */
+       public static String getDataPathForUrl(Node node) throws RepositoryException {
+               return cleanPathForUrl(getDataPath(node));
+       }
+
+       /** Clean reserved URL characters for use in HTTP links. */
+       public static String cleanPathForUrl(String path) throws RepositoryException {
+               StringTokenizer st = new StringTokenizer(path, "/");
+               StringBuilder sb = new StringBuilder();
+               while (st.hasMoreElements()) {
+                       sb.append('/');
+                       String encoded = URLEncoder.encode(st.nextToken(), StandardCharsets.UTF_8);
+                       encoded = encoded.replace("+", "%20");
+                       sb.append(encoded);
+
+               }
+               return sb.toString();
+       }
+
        /** @deprecated Use rowData16px() instead. GridData should not be reused. */
        @Deprecated
        public static RowData ROW_DATA_16px = new RowData(16, 16);
@@ -245,6 +267,8 @@ public class CmsUiUtils implements CmsConstants {
 
        /** Dispose all children of a Composite */
        public static void clear(Composite composite) {
+               if (composite.isDisposed())
+                       return;
                for (Control child : composite.getChildren())
                        child.dispose();
        }
@@ -345,4 +369,5 @@ public class CmsUiUtils implements CmsConstants {
        /** Singleton. */
        private CmsUiUtils() {
        }
+
 }
index c9a3e2291eb1f828a63d14bb30cb9a3d20ef232e..6c4a870c49752f26731be354a492e49d7260d7da 100644 (file)
@@ -11,11 +11,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
-import java.net.URLEncoder;
-import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Paths;
-import java.util.StringTokenizer;
 
 import javax.jcr.Binary;
 import javax.jcr.Node;
@@ -155,22 +152,7 @@ public class DefaultImageManager implements CmsImageManager {
        /** @return null if not available */
        @Override
        public String getImageUrl(Node node) throws RepositoryException {
-               return getCleanDataPath(node);
-       }
-
-       /** Clean special character from the URL. */
-       protected String getCleanDataPath(Node node) throws RepositoryException {
-               String path = CmsUiUtils.getDataPath(node);
-               StringTokenizer st = new StringTokenizer(path, "/");
-               StringBuilder sb = new StringBuilder();
-               while (st.hasMoreElements()) {
-                       sb.append('/');
-                       String encoded = URLEncoder.encode(st.nextToken(), StandardCharsets.UTF_8);
-                       encoded = encoded.replace("+", "%20");
-                       sb.append(encoded);
-
-               }
-               return sb.toString();
+               return CmsUiUtils.getDataPathForUrl(node);
        }
 
        protected String getResourceName(Node node) throws RepositoryException {
diff --git a/org.argeo.cms.ui/src/org/argeo/cms/ui/util/SimpleStyle.java b/org.argeo.cms.ui/src/org/argeo/cms/ui/util/SimpleStyle.java
new file mode 100644 (file)
index 0000000..4bbd19c
--- /dev/null
@@ -0,0 +1,6 @@
+package org.argeo.cms.ui.util;
+
+/** Simple styles used by the CMS UI utilities. */
+public enum SimpleStyle implements CmsStyle {
+       link;
+}