X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms.ui%2Fsrc%2Forg%2Fargeo%2Fcms%2Fui%2Futil%2FCmsUiUtils.java;h=2bfeb43cfb593e7f62e6686acfe3cc55d308576c;hb=c53fec78daddb69c489686844188036b04e1615a;hp=6b411cc302eb994676423a873003e22141dc3dce;hpb=495daae85d453bce3e030358372907f9e534bfae;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms.ui/src/org/argeo/cms/ui/util/CmsUiUtils.java b/org.argeo.cms.ui/src/org/argeo/cms/ui/util/CmsUiUtils.java index 6b411cc30..2bfeb43cf 100644 --- a/org.argeo.cms.ui/src/org/argeo/cms/ui/util/CmsUiUtils.java +++ b/org.argeo.cms.ui/src/org/argeo/cms/ui/util/CmsUiUtils.java @@ -4,19 +4,23 @@ 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; import javax.servlet.http.HttpServletRequest; import org.argeo.api.NodeConstants; -import org.argeo.api.NodeUtils; +import org.argeo.cms.jcr.CmsJcrUtils; import org.argeo.cms.ui.CmsConstants; import org.argeo.cms.ui.CmsView; import org.argeo.eclipse.ui.Selected; import org.argeo.eclipse.ui.specific.EclipseUiSpecificUtils; +import org.argeo.jcr.JcrException; import org.argeo.jcr.JcrUtils; import org.eclipse.rap.rwt.RWT; import org.eclipse.rap.rwt.service.ResourceManager; @@ -30,6 +34,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; @@ -110,7 +115,26 @@ public class CmsUiUtils implements CmsConstants { } public static String getDataPath(String cn, Node node) throws RepositoryException { - return NodeUtils.getDataPath(cn, node); + return CmsJcrUtils.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. */ @@ -128,6 +152,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 +181,22 @@ public class CmsUiUtils implements CmsConstants { return new GridData(horizontalAlignment, horizontalAlignment, false, true); } + /* + * ROW LAYOUT + */ + /** @return the same layout, with margins removed. */ + public static RowLayout noMarginsRowLayout(RowLayout rowLayout) { + rowLayout.marginTop = 0; + rowLayout.marginBottom = 0; + rowLayout.marginLeft = 0; + rowLayout.marginRight = 0; + return rowLayout; + } + + public static RowLayout noMarginsRowLayout(int type) { + return noMarginsRowLayout(new RowLayout(type)); + } + public static RowData rowData16px() { return new RowData(16, 16); } @@ -183,7 +224,9 @@ public class CmsUiUtils implements CmsConstants { return widget;// does nothing EclipseUiSpecificUtils.setStyleData(widget, style); if (widget instanceof Control) { - CmsView.getCmsView((Control) widget).applyStyles(widget); + CmsView cmsView = CmsView.getCmsView((Control) widget); + if (cmsView != null) + cmsView.applyStyles(widget); } return widget; } @@ -199,6 +242,12 @@ public class CmsUiUtils implements CmsConstants { return widget; } + /** Disable markup validation. */ + public static T disableMarkupValidation(T widget) { + EclipseUiSpecificUtils.setMarkupValidationDisabledData(widget); + return widget; + } + /** * Apply markup and set text on {@link Label}, {@link Button}, {@link Text}. * @@ -242,6 +291,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(); } @@ -278,7 +329,17 @@ public class CmsUiUtils implements CmsConstants { // IMAGES public static String img(Node fileNode, String width, String height) { - String src = NodeUtils.getDataPath(fileNode); + 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; + try { + src = (serverBase != null ? serverBase : "") + getDataPathForUrl(fileNode); + } catch (RepositoryException e) { + throw new JcrException("Cannot get URL data path for " + fileNode, e); + } return imgBuilder(src, width, height).append("/>").toString(); } @@ -338,4 +399,5 @@ public class CmsUiUtils implements CmsConstants { /** Singleton. */ private CmsUiUtils() { } + }