Improve CMS theming.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / ui / util / CmsUiUtils.java
index 39df49ebfba4f60689fc59eda53eaf95aedfe317..167d7341f441748bf1e4bd99d4126572de794f84 100644 (file)
@@ -14,7 +14,6 @@ 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.jcr.JcrUtils;
 import org.eclipse.rap.rwt.RWT;
 import org.eclipse.rap.rwt.service.ResourceManager;
@@ -41,9 +40,13 @@ public class CmsUiUtils implements CmsConstants {
        /**
         * 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) {
@@ -102,18 +105,26 @@ public class CmsUiUtils implements CmsConstants {
        //
        // GRID DATA
        //
-       public static GridData fillWidth() {
-               return grabWidth(SWT.FILL, SWT.FILL);
-       }
-
        public static GridData fillAll() {
                return new GridData(SWT.FILL, SWT.FILL, true, true);
        }
 
+       public static GridData fillWidth() {
+               return grabWidth(SWT.FILL, SWT.FILL);
+       }
+
        public static GridData grabWidth(int horizontalAlignment, int verticalAlignment) {
                return new GridData(horizontalAlignment, horizontalAlignment, true, false);
        }
 
+       public static GridData fillHeight() {
+               return grabHeight(SWT.FILL, SWT.FILL);
+       }
+
+       public static GridData grabHeight(int horizontalAlignment, int verticalAlignment) {
+               return new GridData(horizontalAlignment, horizontalAlignment, false, true);
+       }
+
        public static RowData rowData16px() {
                return new RowData(16, 16);
        }
@@ -124,6 +135,12 @@ public class CmsUiUtils implements CmsConstants {
                return widget;
        }
 
+       /** Style widget */
+       public static <T extends Widget> T style(T widget, CmsStyle style) {
+               widget.setData(CmsConstants.STYLE, style.toStyleClass());
+               return widget;
+       }
+
        /** Enable markups on widget */
        public static <T extends Widget> T markup(T widget) {
                widget.setData(CmsConstants.MARKUP, true);
@@ -133,21 +150,39 @@ public class CmsUiUtils implements CmsConstants {
        /**
         * Apply markup and set text on {@link Label}, {@link Button}, {@link Text}.
         * 
+        * @param widget the widget to style and to use in order to display text
+        * @param txt    the object to display via its <code>toString()</code> method.
+        *               This argument should not be null, but if it is null and
+        *               assertions are disabled "<null>" is displayed instead; if
+        *               assertions are enabled the call will fail.
+        * 
         * @see #markup(Widget)
         */
-       public static <T extends Widget> T text(T widget, String txt) {
+       public static <T extends Widget> T text(T widget, Object txt) {
+               assert txt != null;
+               String str = txt != null ? txt.toString() : "<null>";
                markup(widget);
                if (widget instanceof Label)
-                       ((Label) widget).setText(txt);
+                       ((Label) widget).setText(str);
                else if (widget instanceof Button)
-                       ((Button) widget).setText(txt);
+                       ((Button) widget).setText(str);
                else if (widget instanceof Text)
-                       ((Text) widget).setText(txt);
+                       ((Text) widget).setText(str);
                else
                        throw new IllegalArgumentException("Unsupported widget type " + widget.getClass());
                return widget;
        }
 
+       /** A {@link Label} with markup activated. */
+       public static Label lbl(Composite parent, Object txt) {
+               return text(new Label(parent, SWT.NONE), txt);
+       }
+
+       /** A read-only {@link Text} whose content can be copy/pasted. */
+       public static Text txt(Composite parent, Object txt) {
+               return text(new Text(parent, SWT.NONE), txt);
+       }
+
        public static void setItemHeight(Table table, int height) {
                table.setData(CmsConstants.ITEM_HEIGHT, height);
        }