Class GenericComposer<T extends Component>

  • All Implemented Interfaces:
    java.io.Serializable, EventListener, SerializableEventListener, Composer<T>, ComposerExt<T>
    Direct Known Subclasses:
    GenericAutowireComposer

    public abstract class GenericComposer<T extends Component>
    extends GenericEventListener
    implements Composer<T>, ComposerExt<T>, java.io.Serializable

    A skeletal composer that you can extend and write intuitive onXxx event handler methods; this class will registers onXxx events to the supervised component automatically.

    Alternatives: in most cases, you don't extend from GenericComposer directly. Rather, you can extend from one of the following skeletons.

    SelectorComposer
    It supports the autowiring based on Java annotation and a CSS3-based selector. If you don't know which one to use, use SelectorComposer.
    GenericForwardComposer
    It supports the autowiring based on naming convention. You don't need to specify annotations explicitly, but it is error-prone if it is used properly.

    The following is an example. The onOK and onCancel event listener is registered into the target main window automatically.

    
     <zscript><!-- both OK in zscript or a compiled Java class -->
     public class MyComposer extends GenericComposer {
        public void onOK() {
            //doOK!
            //...
        }
        public void onCancel() {
            //doCancel
            //...
        } 
     }
     </zscript>
    
     <window id="main" apply="MyComposer">
         ...
     </window>
     

    since 3.6.1, this composer would be assigned as an attribute of the given component per the naming convention composed of the component id and composer Class name. e.g. If the applied component id is "xwin" and this composer class is org.zkoss.MyComposer, then the variable name would be "xwin$MyComposer". You can reference this composer with Component.getAttributeOrFellow(java.lang.String, boolean) or via EL as ${xwin$MyComposer} of via annotate data binder as @{xwin$MyComposer}, etc. If this composer is the first composer applied to the component, a shorter variable name composed of the component id and a String "composer" would be also available for use. Per the above example, you can also reference this composer with the name "xwin$composer".

    In general, xwin$composer is suggested because EL expressions won't depend on the composer's class name. However, xwin$MyComposer is useful if you apply multiple composers to the same component.

    Notice that, since 3.6.2, this composer becomes serializable.

    Since:
    3.0.1
    Author:
    robbiecheng
    See Also:
    Serialized Form
    • Field Detail

      • _applied

        protected java.lang.String _applied
    • Constructor Detail

      • GenericComposer

        public GenericComposer()
    • Method Detail

      • getPage

        protected Page getPage()
        Returns the current page.
        Since:
        5.0.10
      • doAfterCompose

        public void doAfterCompose​(T comp)
                            throws java.lang.Exception
        Registers onXxx events to the supervised component; a subclass that override this method should remember to call super.doAfterCompose(comp) or it will not work.
        Specified by:
        doAfterCompose in interface Composer<T extends Component>
        Parameters:
        comp - the component has been composed
        Throws:
        java.lang.Exception
      • doBeforeCompose

        public ComponentInfo doBeforeCompose​(Page page,
                                             Component parent,
                                             ComponentInfo compInfo)
        Description copied from interface: ComposerExt
        Invokes before composing a component. If you want to manipulate the specified component info, you can use ComponentInfo.duplicate() to make a copy and then modify it such that it won't affect the default behavior.
        Specified by:
        doBeforeCompose in interface ComposerExt<T extends Component>
        Parameters:
        page - the page for composing this component.
        parent - the parent component, or null if it is the root component.
        compInfo - the component info used to instantiate the component. Notice that it is null if the page is rendered as ZK JSP, since ComponentInfo is not available under ZK JSP.
        Returns:
        the component info used to instantiate the component. In most case, it shall return compInfo. If null is returned, the component won't be instantiated. In other words, it is ignored.
      • doBeforeComposeChildren

        public void doBeforeComposeChildren​(T comp)
                                     throws java.lang.Exception
        Description copied from interface: ComposerExt
        Invokes after the component is instantiated and initialized, but before composing any child.
        Specified by:
        doBeforeComposeChildren in interface ComposerExt<T extends Component>
        Parameters:
        comp - the component being composed
        Throws:
        java.lang.Exception
      • doCatch

        public boolean doCatch​(java.lang.Throwable ex)
                        throws java.lang.Exception
        Description copied from interface: ComposerExt
        Called when an exception occurs when composing the component.

        If you don't want to handle the exception, simply returns false. boolean doCatch(Throwable ex) {return false;}

        An exception thrown in this method is simply logged. It has no effect on the execution. If you want to ignore the exception, just return true.

        Specified by:
        doCatch in interface ComposerExt<T extends Component>
        Parameters:
        ex - the exception being thrown
        Returns:
        whether to ignore the exception. If false is returned, the exception will be re-thrown. Note: once a composer's doCatch returns true, the exception will be ignored and it means doCatch of the following composers won't be called.
        Throws:
        java.lang.Exception