X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms.ui%2Fsrc%2Forg%2Fargeo%2Fcms%2Fui%2Futil%2FCmsUiUtils.java;h=428c910e54133dee353aa8d034ffc388fb8f30a5;hb=c927a4902014165349575f9621bfe690c70cf67f;hp=3669db3fdb35ad9061f7ef993a5cf3415dae2fb5;hpb=28eae1d74809cb8752ab72219063e822e264c77d;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 3669db3fd..428c910e5 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,6 +4,8 @@ import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; +import java.util.HashMap; +import java.util.Map; import javax.jcr.Node; import javax.jcr.RepositoryException; @@ -11,17 +13,20 @@ 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.eclipse.ui.Selected; +import org.argeo.eclipse.ui.specific.EclipseUiSpecificUtils; 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.events.SelectionListener; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.layout.FormAttachment; +import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.layout.RowData; @@ -38,12 +43,41 @@ import org.eclipse.swt.widgets.Widget; public class CmsUiUtils implements CmsConstants { // private final static Log log = LogFactory.getLog(CmsUiUtils.class); + /* + * CMS VIEW + */ + + /** Sends an event via {@link CmsView#sendEvent(String, Map)}. */ + public static void sendEventOnSelect(Control control, String topic, Map properties) { + SelectionListener listener = (Selected) (e) -> { + CmsView.getCmsView(control.getParent()).sendEvent(topic, properties); + }; + if (control instanceof Button) { + ((Button) control).addSelectionListener(listener); + } else + throw new UnsupportedOperationException("Control type " + control.getClass() + " is not supported."); + } + + /** + * Convenience method to sends an event via + * {@link CmsView#sendEvent(String, Map)}. + */ + public static void sendEventOnSelect(Control control, String topic, String key, Object value) { + Map properties = new HashMap<>(); + properties.put(key, value); + sendEventOnSelect(control, topic, properties); + } + /** * The CMS view related to this display, or null if none is available from this * call. + * + * @deprecated Use {@link CmsView#getCmsView(Composite)} instead. */ + @Deprecated public static CmsView getCmsView() { - return UiContext.getData(CmsView.KEY); +// return UiContext.getData(CmsView.class.getName()); + return CmsView.getCmsView(Display.getCurrent().getActiveShell()); } public static StringBuilder getServerBaseUrl(HttpServletRequest request) { @@ -55,7 +89,7 @@ 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); } } @@ -66,7 +100,7 @@ public class CmsUiUtils implements CmsConstants { 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); } } @@ -83,6 +117,9 @@ public class CmsUiUtils implements CmsConstants { @Deprecated public static RowData ROW_DATA_16px = new RowData(16, 16); + /* + * GRID LAYOUT + */ public static GridLayout noSpaceGridLayout() { return noSpaceGridLayout(new GridLayout()); } @@ -99,9 +136,6 @@ public class CmsUiUtils implements CmsConstants { return layout; } - // - // GRID DATA - // public static GridData fillAll() { return new GridData(SWT.FILL, SWT.FILL, true, true); } @@ -122,25 +156,49 @@ public class CmsUiUtils implements CmsConstants { return new GridData(horizontalAlignment, horizontalAlignment, false, true); } + /* + * ROW LAYOUT + */ public static RowData rowData16px() { return new RowData(16, 16); } + /* + * FORM LAYOUT + */ + + public static FormData coverAll() { + FormData fdLabel = new FormData(); + fdLabel.top = new FormAttachment(0, 0); + fdLabel.left = new FormAttachment(0, 0); + fdLabel.right = new FormAttachment(100, 0); + fdLabel.bottom = new FormAttachment(100, 0); + return fdLabel; + } + + /* + * STYLING + */ + /** Style widget */ public static T style(T widget, String style) { - widget.setData(CmsConstants.STYLE, style); + if (style == null) + return widget;// does nothing + EclipseUiSpecificUtils.setStyleData(widget, style); + if (widget instanceof Control) { + CmsView.getCmsView((Control) widget).applyStyles(widget); + } return widget; } /** Style widget */ public static T style(T widget, CmsStyle style) { - widget.setData(CmsConstants.STYLE, style.toStyleClass()); - return widget; + return style(widget, style.toStyleClass()); } /** Enable markups on widget */ public static T markup(T widget) { - widget.setData(CmsConstants.MARKUP, true); + EclipseUiSpecificUtils.setMarkupData(widget); return widget; } @@ -180,6 +238,7 @@ public class CmsUiUtils implements CmsConstants { return text(new Text(parent, SWT.NONE), txt); } + @Deprecated public static void setItemHeight(Table table, int height) { table.setData(CmsConstants.ITEM_HEIGHT, height); } @@ -221,6 +280,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(); }