Introduce noSpaceRowLayout.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / ui / util / CmsUiUtils.java
index e72965893cf4cc5e57a6b6ee24e819e8250846dc..edf1ba3ef10e18d5d32482fdb106bfa59bb5801b 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;
@@ -30,6 +33,7 @@ import org.eclipse.swt.layout.FormData;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.layout.RowData;
+import org.eclipse.swt.layout.RowLayout;
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
@@ -113,6 +117,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);
@@ -128,6 +151,7 @@ public class CmsUiUtils implements CmsConstants {
                return noSpaceGridLayout(new GridLayout(columns, false));
        }
 
+       /** @return the same layout, with spaces removed. */
        public static GridLayout noSpaceGridLayout(GridLayout layout) {
                layout.horizontalSpacing = 0;
                layout.verticalSpacing = 0;
@@ -156,6 +180,23 @@ public class CmsUiUtils implements CmsConstants {
                return new GridData(horizontalAlignment, horizontalAlignment, false, true);
        }
 
+       /*
+        * ROW LAYOUT
+        */
+       /** @return the same layout, with space and margins removed. */
+       public static RowLayout noSpaceRowLayout(RowLayout rowLayout) {
+               rowLayout.marginTop = 0;
+               rowLayout.marginBottom = 0;
+               rowLayout.marginLeft = 0;
+               rowLayout.marginRight = 0;
+               rowLayout.spacing = 0;
+               return rowLayout;
+       }
+
+       public static RowLayout noSpaceRowLayout(int type) {
+               return noSpaceRowLayout(new RowLayout(type));
+       }
+
        public static RowData rowData16px() {
                return new RowData(16, 16);
        }
@@ -199,6 +240,12 @@ public class CmsUiUtils implements CmsConstants {
                return widget;
        }
 
+       /** Disable markup validation. */
+       public static <T extends Widget> T disableMarkupValidation(T widget) {
+               EclipseUiSpecificUtils.setMarkupValidationDisabledData(widget);
+               return widget;
+       }
+
        /**
         * Apply markup and set text on {@link Label}, {@link Button}, {@link Text}.
         * 
@@ -242,6 +289,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();
        }
@@ -277,6 +326,15 @@ public class CmsUiUtils implements CmsConstants {
        }
 
        // IMAGES
+       public static String img(Node fileNode, String width, String height) {
+               return img(null, fileNode, width, height);
+       }
+
+       public static String img(String serverBase, Node fileNode, String width, String height) {
+               String src = (serverBase != null ? serverBase : "") + NodeUtils.getDataPath(fileNode);
+               return imgBuilder(src, width, height).append("/>").toString();
+       }
+
        public static String img(String src, String width, String height) {
                return imgBuilder(src, width, height).append("/>").toString();
        }
@@ -333,4 +391,5 @@ public class CmsUiUtils implements CmsConstants {
        /** Singleton. */
        private CmsUiUtils() {
        }
+
 }