Massive Argeo APIs refactoring
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / ui / util / CmsUiUtils.java
index 39df49ebfba4f60689fc59eda53eaf95aedfe317..8b384799fb9e2410aacb7f42a88f1c63fb89af92 100644 (file)
@@ -4,46 +4,48 @@ 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.StringTokenizer;
 
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 import javax.servlet.http.HttpServletRequest;
 
-import org.argeo.api.NodeConstants;
-import org.argeo.api.NodeUtils;
-import org.argeo.cms.CmsException;
-import org.argeo.cms.ui.CmsConstants;
-import org.argeo.cms.ui.CmsView;
-import org.argeo.eclipse.ui.specific.UiContext;
+import org.argeo.api.cms.Cms2DSize;
+import org.argeo.api.cms.CmsView;
+import org.argeo.api.cms.CmsConstants;
+import org.argeo.cms.jcr.CmsJcrUtils;
+import org.argeo.cms.swt.CmsSwtUtils;
+import org.argeo.cms.ui.CmsUiConstants;
 import org.argeo.jcr.JcrUtils;
 import org.eclipse.rap.rwt.RWT;
 import org.eclipse.rap.rwt.service.ResourceManager;
-import org.eclipse.swt.SWT;
 import org.eclipse.swt.graphics.Image;
 import org.eclipse.swt.graphics.ImageData;
-import org.eclipse.swt.graphics.Point;
-import org.eclipse.swt.layout.GridData;
-import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.layout.RowData;
-import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Table;
-import org.eclipse.swt.widgets.Text;
-import org.eclipse.swt.widgets.Widget;
 
 /** Static utilities for the CMS framework. */
-public class CmsUiUtils implements CmsConstants {
+public class CmsUiUtils {
        // private final static Log log = LogFactory.getLog(CmsUiUtils.class);
 
+       /*
+        * CMS VIEW
+        */
+
        /**
         * The CMS view related to this display, or null if none is available from this
         * call.
+        * 
+        * @deprecated Use {@link CmsSwtUtils#getCmsView(Composite)} instead.
         */
+       @Deprecated
        public static CmsView getCmsView() {
-               return UiContext.getData(CmsView.KEY);
+//             return UiContext.getData(CmsView.class.getName());
+               return CmsSwtUtils.getCmsView(Display.getCurrent().getActiveShell());
        }
 
        public static StringBuilder getServerBaseUrl(HttpServletRequest request) {
@@ -55,107 +57,64 @@ public class CmsUiUtils implements CmsConstants {
                                buf.append(':').append(url.getPort());
                        return buf;
                } catch (MalformedURLException e) {
-                       throw new CmsException("Cannot extract server base URL from " + request.getRequestURL(), e);
+                       throw new IllegalArgumentException("Cannot extract server base URL from " + request.getRequestURL(), e);
                }
        }
 
        //
-       public static String getDataUrl(Node node, HttpServletRequest request) throws RepositoryException {
+       public static String getDataUrl(Node node, HttpServletRequest request) {
                try {
                        StringBuilder buf = getServerBaseUrl(request);
                        buf.append(getDataPath(node));
                        return new URL(buf.toString()).toString();
                } catch (MalformedURLException e) {
-                       throw new CmsException("Cannot build data URL for " + node, e);
+                       throw new IllegalArgumentException("Cannot build data URL for " + node, e);
                }
        }
 
        /** A path in the node repository */
-       public static String getDataPath(Node node) throws RepositoryException {
-               return getDataPath(NodeConstants.EGO_REPOSITORY, node);
-       }
-
-       public static String getDataPath(String cn, Node node) throws RepositoryException {
-               return NodeUtils.getDataPath(cn, node);
-       }
-
-       /** @deprecated Use rowData16px() instead. GridData should not be reused. */
-       @Deprecated
-       public static RowData ROW_DATA_16px = new RowData(16, 16);
-
-       public static GridLayout noSpaceGridLayout() {
-               return noSpaceGridLayout(new GridLayout());
-       }
-
-       public static GridLayout noSpaceGridLayout(int columns) {
-               return noSpaceGridLayout(new GridLayout(columns, false));
-       }
-
-       public static GridLayout noSpaceGridLayout(GridLayout layout) {
-               layout.horizontalSpacing = 0;
-               layout.verticalSpacing = 0;
-               layout.marginWidth = 0;
-               layout.marginHeight = 0;
-               return layout;
+       public static String getDataPath(Node node) {
+               return getDataPath(CmsConstants.EGO_REPOSITORY, node);
        }
 
-       //
-       // GRID DATA
-       //
-       public static GridData fillWidth() {
-               return grabWidth(SWT.FILL, SWT.FILL);
+       public static String getDataPath(String cn, Node node) {
+               return CmsJcrUtils.getDataPath(cn, node);
        }
 
-       public static GridData fillAll() {
-               return new GridData(SWT.FILL, SWT.FILL, true, true);
+       /** Clean reserved URL characters for use in HTTP links. */
+       public static String getDataPathForUrl(Node node) {
+               return cleanPathForUrl(getDataPath(node));
        }
 
-       public static GridData grabWidth(int horizontalAlignment, int verticalAlignment) {
-               return new GridData(horizontalAlignment, horizontalAlignment, true, false);
-       }
+       /** Clean reserved URL characters for use in HTTP links. */
+       public static String cleanPathForUrl(String path) {
+               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);
 
-       public static RowData rowData16px() {
-               return new RowData(16, 16);
+               }
+               return sb.toString();
        }
 
-       /** Style widget */
-       public static <T extends Widget> T style(T widget, String style) {
-               widget.setData(CmsConstants.STYLE, style);
-               return widget;
-       }
+       /** @deprecated Use rowData16px() instead. GridData should not be reused. */
+       @Deprecated
+       public static RowData ROW_DATA_16px = new RowData(16, 16);
 
-       /** Enable markups on widget */
-       public static <T extends Widget> T markup(T widget) {
-               widget.setData(CmsConstants.MARKUP, true);
-               return widget;
-       }
+       
 
-       /**
-        * Apply markup and set text on {@link Label}, {@link Button}, {@link Text}.
-        * 
-        * @see #markup(Widget)
+       /*
+        * FORM LAYOUT
         */
-       public static <T extends Widget> T text(T widget, String txt) {
-               markup(widget);
-               if (widget instanceof Label)
-                       ((Label) widget).setText(txt);
-               else if (widget instanceof Button)
-                       ((Button) widget).setText(txt);
-               else if (widget instanceof Text)
-                       ((Text) widget).setText(txt);
-               else
-                       throw new IllegalArgumentException("Unsupported widget type " + widget.getClass());
-               return widget;
-       }
 
-       public static void setItemHeight(Table table, int height) {
-               table.setData(CmsConstants.ITEM_HEIGHT, height);
-       }
+       
 
-       /** Dispose all children of a Composite */
-       public static void clear(Composite composite) {
-               for (Control child : composite.getChildren())
-                       child.dispose();
+       @Deprecated
+       public static void setItemHeight(Table table, int height) {
+               table.setData(CmsUiConstants.ITEM_HEIGHT, height);
        }
 
        //
@@ -189,12 +148,24 @@ 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);
+               String src;
+               src = (serverBase != null ? serverBase : "") + getDataPathForUrl(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();
        }
 
-       public static String img(String src, Point size) {
-               return img(src, Integer.toString(size.x), Integer.toString(size.y));
+       public static String img(String src, Cms2DSize size) {
+               return img(src, Integer.toString(size.getWidth()), Integer.toString(size.getHeight()));
        }
 
        public static StringBuilder imgBuilder(String src, String width, String height) {
@@ -202,22 +173,22 @@ public class CmsUiUtils implements CmsConstants {
                                .append("' src='").append(src).append("'");
        }
 
-       public static String noImg(Point size) {
+       public static String noImg(Cms2DSize size) {
                ResourceManager rm = RWT.getResourceManager();
-               return CmsUiUtils.img(rm.getLocation(NO_IMAGE), size);
+               return CmsUiUtils.img(rm.getLocation(CmsUiConstants.NO_IMAGE), size);
        }
 
        public static String noImg() {
-               return noImg(NO_IMAGE_SIZE);
+               return noImg(CmsUiConstants.NO_IMAGE_SIZE);
        }
 
-       public static Image noImage(Point size) {
+       public static Image noImage(Cms2DSize size) {
                ResourceManager rm = RWT.getResourceManager();
                InputStream in = null;
                try {
-                       in = rm.getRegisteredContent(NO_IMAGE);
+                       in = rm.getRegisteredContent(CmsUiConstants.NO_IMAGE);
                        ImageData id = new ImageData(in);
-                       ImageData scaled = id.scaledTo(size.x, size.y);
+                       ImageData scaled = id.scaledTo(size.getWidth(), size.getHeight());
                        Image image = new Image(Display.getCurrent(), scaled);
                        return image;
                } finally {
@@ -245,4 +216,5 @@ public class CmsUiUtils implements CmsConstants {
        /** Singleton. */
        private CmsUiUtils() {
        }
+
 }