From: Mathieu Baudier Date: Mon, 15 Apr 2019 15:45:08 +0000 (+0200) Subject: Refine MVC concepts X-Git-Tag: argeo-commons-2.1.77~10 X-Git-Url: https://git.argeo.org/?a=commitdiff_plain;h=139300c6d5f64dc655a22ae2a067e37b11e1ae72;p=lgpl%2Fargeo-commons.git Refine MVC concepts --- diff --git a/org.argeo.cms.ui/src/org/argeo/cms/ui/CmsUiProvider.java b/org.argeo.cms.ui/src/org/argeo/cms/ui/CmsUiProvider.java index 24415c83c..6ef4b315c 100644 --- a/org.argeo.cms.ui/src/org/argeo/cms/ui/CmsUiProvider.java +++ b/org.argeo.cms.ui/src/org/argeo/cms/ui/CmsUiProvider.java @@ -3,19 +3,28 @@ package org.argeo.cms.ui; import javax.jcr.Node; import javax.jcr.RepositoryException; +import org.argeo.node.MvcProvider; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; /** Stateless factory building an SWT user interface given a JCR context. */ @FunctionalInterface -public interface CmsUiProvider { +public interface CmsUiProvider extends MvcProvider { /** * Initialises a user interface. * - * @param parent - * the parent composite - * @param context - * a context node (holding the JCR underlying session), or null + * @param parent the parent composite + * @param context a context node (holding the JCR underlying session), or null */ public Control createUi(Composite parent, Node context) throws RepositoryException; + + @Override + public default Control apply(Composite parent, Node context) { + try { + return createUi(parent, context); + } catch (RepositoryException e) { + throw new IllegalStateException("Cannot create UI for context " + context, e); + } + } + } diff --git a/org.argeo.node.api/src/org/argeo/node/MvcProvider.java b/org.argeo.node.api/src/org/argeo/node/MvcProvider.java new file mode 100644 index 000000000..4c52b2070 --- /dev/null +++ b/org.argeo.node.api/src/org/argeo/node/MvcProvider.java @@ -0,0 +1,38 @@ +package org.argeo.node; + +import java.util.function.BiFunction; + +/** + * Stateless UI part creator. Takes a parent view (V) and a model context (M) in + * order to create a view part (W) which can then be further configured. Such + * object can be used as services and reference other part of the model which + * are relevant for all created UI part. + */ +@FunctionalInterface +public interface MvcProvider extends BiFunction { + /** + * Whether this parent view is supported. + * + * @return true by default. + */ + default boolean isViewSupported(V parent) { + return true; + } + + /** + * Whether this context is supported. + * + * @return true by default. + */ + default boolean isModelSupported(M context) { + return true; + } + + default W createUiPart(V parent, M context) { + if (!isViewSupported(parent)) + throw new IllegalArgumentException("Parent view " + parent + "is not supported."); + if (!isModelSupported(context)) + throw new IllegalArgumentException("Model context " + context + "is not supported."); + return apply(parent, context); + } +}