X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms.ui%2Fsrc%2Forg%2Fargeo%2Fcms%2Futil%2FCmsUtils.java;h=f4004e493123c75426e3a82b10289ef7491dc516;hb=2aa6142e53fcd55622c7d58d772e76d17e916dda;hp=a1908106296f1f18bd3177e9f4b51bf97571d78b;hpb=c5fa035468228d1f87ab5431a3fad17403eee1c3;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms.ui/src/org/argeo/cms/util/CmsUtils.java b/org.argeo.cms.ui/src/org/argeo/cms/util/CmsUtils.java index a19081062..f4004e493 100644 --- a/org.argeo.cms.ui/src/org/argeo/cms/util/CmsUtils.java +++ b/org.argeo.cms.ui/src/org/argeo/cms/util/CmsUtils.java @@ -1,26 +1,21 @@ package org.argeo.cms.util; +import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; -import javax.jcr.Item; import javax.jcr.Node; -import javax.jcr.Property; import javax.jcr.RepositoryException; import javax.servlet.http.HttpServletRequest; -import org.apache.commons.io.IOUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.argeo.cms.CmsException; -import org.argeo.cms.auth.AuthConstants; import org.argeo.cms.ui.CmsConstants; import org.argeo.cms.ui.CmsView; import org.argeo.eclipse.ui.specific.UiContext; -import org.argeo.jcr.ArgeoJcrConstants; import org.argeo.jcr.JcrUtils; -import org.argeo.node.NodeAuthenticated; +import org.argeo.node.NodeConstants; +import org.argeo.node.NodeUtils; import org.eclipse.rap.rwt.RWT; import org.eclipse.rap.rwt.service.ResourceManager; import org.eclipse.swt.SWT; @@ -30,22 +25,25 @@ 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 CmsUtils implements CmsConstants { - private final static Log log = LogFactory.getLog(CmsUtils.class); + // private final static Log log = LogFactory.getLog(CmsUtils.class); /** - * The CMS view related to this display, or null if none is available from - * this call. + * The CMS view related to this display, or null if none is available from this + * call. */ public static CmsView getCmsView() { - return UiContext.getData(NodeAuthenticated.KEY); + return UiContext.getData(CmsView.KEY); } public static StringBuilder getServerBaseUrl(HttpServletRequest request) { @@ -60,7 +58,8 @@ public class CmsUtils implements CmsConstants { throw new CmsException("Cannot extract server base URL from " + request.getRequestURL(), e); } } -// + + // public static String getDataUrl(Node node, HttpServletRequest request) throws RepositoryException { try { StringBuilder buf = getServerBaseUrl(request); @@ -70,37 +69,15 @@ public class CmsUtils implements CmsConstants { throw new CmsException("Cannot build data URL for " + node, e); } } -// FIXME - private final static String PATH_DATA = "/data"; - private final static String WEBDAV_PUBLIC = PATH_DATA + "/public"; - private final static String WEBDAV_PRIVATE = PATH_DATA + "/files"; + + /** A path in the node repository */ public static String getDataPath(Node node) throws RepositoryException { - assert node != null; - String userId = node.getSession().getUserID(); - if (log.isTraceEnabled()) - log.trace(userId + " : " + node.getPath()); - StringBuilder buf = new StringBuilder(); - boolean isAnonymous = userId.equalsIgnoreCase(AuthConstants.ROLE_ANONYMOUS); - if (isAnonymous) - buf.append(WEBDAV_PUBLIC); - else - buf.append(WEBDAV_PRIVATE); - // TODO convey repo alias vie repository properties - return buf.append('/').append(ArgeoJcrConstants.ALIAS_NODE).append('/').append(node.getSession().getWorkspace().getName()) - .append(node.getPath()).toString(); - } -// -// public static String getCanonicalUrl(Node node, HttpServletRequest request) throws RepositoryException { -// try { -// StringBuilder buf = getServerBaseUrl(request); -// buf.append('/').append('!').append(node.getPath()); -// return new URL(buf.toString()).toString(); -// } catch (MalformedURLException e) { -// throw new CmsException("Cannot build data URL for " + node, e); -// } -// // return request.getRequestURL().append('!').append(node.getPath()) -// // .toString(); -// } + return getDataPath(NodeConstants.NODE, 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 @@ -110,6 +87,10 @@ public class CmsUtils implements CmsConstants { 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; @@ -138,37 +119,37 @@ public class CmsUtils implements CmsConstants { } /** Style widget */ - public static void style(Widget widget, String style) { + public static T style(T widget, String style) { widget.setData(CmsConstants.STYLE, style); + return widget; } /** Enable markups on widget */ - public static void markup(Widget widget) { + public static T markup(T widget) { widget.setData(CmsConstants.MARKUP, true); + return widget; } - public static void setItemHeight(Table table, int height) { - table.setData(CmsConstants.ITEM_HEIGHT, height); + /** + * Apply markup and set text on {@link Label}, {@link Button}, {@link Text}. + * + * @see #markup(Widget) + */ + public static 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; } - /** @return the path or null if not instrumented */ - public static String getDataPath(Widget widget) { - // JCR item - Object data = widget.getData(); - if (data != null && data instanceof Item) { - try { - return ((Item) data).getPath(); - } catch (RepositoryException e) { - throw new CmsException("Cannot find data path of " + data + " for " + widget); - } - } - - // JCR path - data = widget.getData(Property.JCR_PATH); - if (data != null) - return data.toString(); - - return null; + public static void setItemHeight(Table table, int height) { + table.setData(CmsConstants.ITEM_HEIGHT, height); } /** Dispose all children of a Composite */ @@ -240,10 +221,28 @@ public class CmsUtils implements CmsConstants { Image image = new Image(Display.getCurrent(), scaled); return image; } finally { - IOUtils.closeQuietly(in); + try { + in.close(); + } catch (IOException e) { + // silent + } } } + /** Lorem ipsum text to be used during development. */ + public final static String LOREM_IPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit." + + " Etiam eleifend hendrerit sem, ac ultricies massa ornare ac." + + " Cras aliquam sodales risus, vitae varius lacus molestie quis." + + " Vivamus consequat, leo id lacinia volutpat, eros diam efficitur urna, finibus interdum risus turpis at nisi." + + " Curabitur vulputate nulla quis scelerisque fringilla. Integer consectetur turpis id lobortis accumsan." + + " Pellentesque commodo turpis ac diam ultricies dignissim." + + " Curabitur sit amet dolor volutpat lacus aliquam ornare quis sed velit." + + " Integer varius quis est et tristique." + + " Suspendisse pharetra porttitor purus, eget condimentum magna." + + " Duis vitae turpis eros. Sed tincidunt lacinia rutrum." + + " Aliquam velit velit, rutrum ut augue sed, condimentum lacinia augue."; + + /** Singleton. */ private CmsUtils() { } }