Class ConventionWires


  • public class ConventionWires
    extends java.lang.Object
    Utilities to wire variables by name conventions. For wiring variables with annotations or with CSS3 selector, please use Selectors instead.
    Since:
    6.0.0
    Author:
    tomyeh
    • Constructor Summary

      Constructors 
      Constructor Description
      ConventionWires()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addForwards​(Component comp, java.lang.Object controller)
      Adds forward conditions to myid source component so onXxx source event received by myid component can be forwarded to the specified target component with the target event name onXxx$myid.
      static void addForwards​(Component comp, java.lang.Object controller, char separator)
      Adds forward conditions to the specified component with a custom separator.
      static void wireController​(Component comp, java.lang.Object controller)
      Wire controller as an attribute of the specified component.
      static void wireController​(Component comp, java.lang.Object controller, char separator)
      Wire controller as an attribute of the specified component with a custom separator.
      static void wireFellows​(IdSpace idspace, java.lang.Object controller)
      Wire fellow components and space owner ancestors of the specified Id space into a controller Java object.
      static void wireFellows​(IdSpace idspace, java.lang.Object controller, char separator)
      Wire fellow components and space owner with a custom separator.
      static void wireFellows​(IdSpace idspace, java.lang.Object controller, char separator, boolean ignoreZScript, boolean ignoreXel)
      Wire fellow components and space owner with full control.
      static void wireImplicit​(Component comp, java.lang.Object controller)
      Wire implicit variables of the specified component into a controller Java object.
      static void wireServiceCommand​(Component comp, java.lang.Object controller)
      Wire controller's command method to be an AuService command that the command can be triggered from client side JavaScript.
      static void wireVariables​(Component comp, java.lang.Object controller)
      Wire accessible variable objects of the specified component into a controller Java object.
      static void wireVariables​(Component comp, java.lang.Object controller, char separator)
      Wire accessible variable objects of the specified component with a custom separator.
      static void wireVariables​(Component comp, java.lang.Object controller, char separator, boolean ignoreZScript, boolean ignoreXel)
      Wire controller as a variable objects of the specified component with full control.
      static void wireVariables​(Page page, java.lang.Object controller)
      Wire accessible variables of the specified page into a controller Java object.
      static void wireVariables​(Page page, java.lang.Object controller, char separator)
      Wire accessible variable objects of the specified page with a custom separator.
      static void wireVariables​(Page page, java.lang.Object controller, char separator, boolean ignoreZScript, boolean ignoreXel)
      Wire accessible variable objects of the specified page with complete control.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ConventionWires

        public ConventionWires()
    • Method Detail

      • wireFellows

        public static final void wireFellows​(IdSpace idspace,
                                             java.lang.Object controller)
        Wire fellow components and space owner ancestors of the specified Id space into a controller Java object. This implementation checks the setXxx() method names first then the field names. If a setXxx() method name matches the id of a fellow or space owner ancestors and with correct argument type, the found method is called with the fellow component as the argument. If no proper setXxx() method then search the field of the controller object for a matched field with name equals to the fellow component's id and proper type. Then the fellow component is assigned as the value of the matched field.

        Note that fellow components are looked up first, then the space owner ancestors

        since 3.5.2, the controller would be assigned as a variable of the given idspace per the naming convention composed of the idspace id and controller Class name. e.g. if the idspace id is "xwin" and the controller class is org.zkoss.MyController, then the variable name would be "xwin$MyController"

        This is useful in writing controller code in MVC design practice. You can wire the components into the controller object per the component's id and do whatever you like.

        Since 3.6.0, for Groovy or other environment that '$' is not applicable, you can invoke wireFellows(IdSpace,Object,char) to use '_' as the separator.

        Parameters:
        idspace - the id space to be bound
        controller - the controller Java object to be injected the fellow components.
      • wireFellows

        public static final void wireFellows​(IdSpace idspace,
                                             java.lang.Object controller,
                                             char separator)
        Wire fellow components and space owner with a custom separator. The separator is used to separate the component ID and additional information, such as event name. By default, it is '$'. However, for Groovy or other environment that '$' is not applicable, you can invoke this method to use '_' as the separator.
        See Also:
        wireFellows(IdSpace, Object)
      • wireFellows

        public static final void wireFellows​(IdSpace idspace,
                                             java.lang.Object controller,
                                             char separator,
                                             boolean ignoreZScript,
                                             boolean ignoreXel)
        Wire fellow components and space owner with full control.
        Parameters:
        separator - the separator used to separate the component ID and event name.
        ignoreZScript - whether to ignore variables defined in zscript when wiring a member.
        ignoreXel - whether to ignore variables defined in varible resolver (Page.addVariableResolver(org.zkoss.xel.VariableResolver)) when wiring a member.
      • wireVariables

        public static final void wireVariables​(Component comp,
                                               java.lang.Object controller)

        Wire accessible variable objects of the specified component into a controller Java object. This implementation checks the setXxx() method names first then the field names. If a setXxx() method name matches the name of the resolved variable object with correct argument type and the associated field value is null, then the method is called with the resolved variable object as the argument. If no proper setXxx() method then search the field name of the controller object. If the field name matches the name of the resolved variable object with correct field type and null field value, the field is then assigned the resolved variable object.

        The controller would be assigned as a variable of the given component per the naming convention composed of the component id and controller Class name. e.g. if the component id is "xwin" and the controller class is org.zkoss.MyController, then the variable name would be "xwin$MyController"

        This is useful in writing controller code in MVC design practice. You can wire the embedded objects, components, and accessible variables into the controller object per the components' id and variables' name and do whatever you like.

        Since 3.6.0, for Groovy or other environment that '$' is not applicable, you can invoke wireVariables(Component,Object,char) to use '_' as the separator.

        Parameters:
        comp - the reference component to wire variables
        controller - the controller Java object to be injected the accessible variable objects.
        See Also:
        GenericAutowireComposer
      • wireVariables

        public static final void wireVariables​(Component comp,
                                               java.lang.Object controller,
                                               char separator)
        Wire accessible variable objects of the specified component with a custom separator. The separator is used to separate the component ID and additional information, such as event name. By default, it is '$'. However, for Groovy or other environment that '$' is not applicable, you can invoke this method to use '_' as the separator.
        See Also:
        wireVariables(Component, Object)
      • wireVariables

        public static final void wireVariables​(Component comp,
                                               java.lang.Object controller,
                                               char separator,
                                               boolean ignoreZScript,
                                               boolean ignoreXel)
        Wire controller as a variable objects of the specified component with full control.
        Parameters:
        separator - the separator used to separate the component ID and event name.
        ignoreZScript - whether to ignore variables defined in zscript when wiring a member.
        ignoreXel - whether to ignore variables defined in varible resolver (Page.addVariableResolver(org.zkoss.xel.VariableResolver)) when wiring a member.
      • wireVariables

        public static final void wireVariables​(Page page,
                                               java.lang.Object controller)

        Wire accessible variables of the specified page into a controller Java object. This implementation checks the setXxx() method names first then the field names. If a setXxx() method name matches the name of the resolved variable object with correct argument type and the associated field value is null, then the method is called with the resolved variable object as the argument. If no proper setXxx() method then search the field name of the controller object. If the field name matches the name of the resolved variable object with correct field type and null field value, the field is then assigned the resolved variable object.

        The controller would be assigned as a variable of the given page per the naming convention composed of the page id and controller Class name. e.g. if the page id is "xpage" and the controller class is org.zkoss.MyController, then the variable name would be "xpage$MyController"

        Since 3.0.8, if the method name of field name matches the ZK implicit object name, ZK implicit object will be wired in, too.

        This is useful in writing controller code in MVC design practice. You can wire the embedded objects, components, and accessible variables into the controller object per the component's id and variable name and do whatever you like.

        Since 3.6.0, for Groovy or other environment that '$' is not applicable, you can invoke wireVariables(Page,Object,char) to use '_' as the separator.

        Parameters:
        page - the reference page to wire variables
        controller - the controller Java object to be injected the fellow components.
      • wireVariables

        public static final void wireVariables​(Page page,
                                               java.lang.Object controller,
                                               char separator)
        Wire accessible variable objects of the specified page with a custom separator. The separator is used to separate the component ID and additional information, such as event name. By default, it is '$'. However, for Groovy or other environment that '$' is not applicable, you can invoke this method to use '_' as the separator.
        See Also:
        wireVariables(Page, Object)
      • wireVariables

        public static final void wireVariables​(Page page,
                                               java.lang.Object controller,
                                               char separator,
                                               boolean ignoreZScript,
                                               boolean ignoreXel)
        Wire accessible variable objects of the specified page with complete control.
        Parameters:
        separator - the separator used to separate the component ID and event name.
        ignoreZScript - whether to ignore variables defined in zscript when wiring a member.
        ignoreXel - whether to ignore variables defined in variable resolver (Page.addVariableResolver(org.zkoss.xel.VariableResolver)) when wiring a member.
      • wireController

        public static final void wireController​(Component comp,
                                                java.lang.Object controller)
        Wire controller as an attribute of the specified component. Please refer to ZK Developer's Reference for details.

        The separator is used to separate the component ID and the controller. By default, it is '$'. However, for Groovy or other environment that '$' is not applicable, you can invoke wireController(Component, Object, char) to use '_' as the separator.

      • wireController

        public static final void wireController​(Component comp,
                                                java.lang.Object controller,
                                                char separator)
        Wire controller as an attribute of the specified component with a custom separator.

        The separator is used to separate the component ID and the controller. By default, it is '$'. However, for Groovy or other environment that '$' is not applicable, you can invoke this method to use '_' as the separator.

      • wireServiceCommand

        public static final void wireServiceCommand​(Component comp,
                                                    java.lang.Object controller)
        Wire controller's command method to be an AuService command that the command can be triggered from client side JavaScript.

        For example,

        
             zkservice.$('$foo').command('commandName', [{bar: 0}, {bar2: 'bar2'}]);
         

        Developer can specify the org.zkoss.zk.ui.jsonServiceParamConverter.class in zk.xml or lang-addon.xml to provide a specific JSON converter which implements Converter

        Parameters:
        comp -
        controller -
        Since:
        8.0.0
      • wireImplicit

        public static final void wireImplicit​(Component comp,
                                              java.lang.Object controller)
        Wire implicit variables of the specified component into a controller Java object.
        Parameters:
        comp - the component
        controller - the controller object
      • addForwards

        public static void addForwards​(Component comp,
                                       java.lang.Object controller)

        Adds forward conditions to myid source component so onXxx source event received by myid component can be forwarded to the specified target component with the target event name onXxx$myid.

        The controller is a POJO file with onXxx$myid methods (the event handler codes). This utility method search such onXxx$myid methods and adds forward condition to the source myid component looked up by Component.getAttributeOrFellow(java.lang.String, boolean) of the specified component, so you don't have to specify in zul file the "forward" attribute one by one. If the source component cannot be looked up or the object looked up is not a component, this method will log the error and ignore it.

        Cascaded '$' will add Forwards cascadedly. E.g. define method onClick$btn$w1 in window w2. This method will add a forward on the button "btn.onClick=w1.onClick$btn" and add another forward on the window w1 "w1.onClick$btn=w2.onClick$btn$w1"

        Since 3.6.0, for Groovy or other environment that '$' is not applicable, you can invoke addForwards(Component,Object,char) to use '_' as the separator.

        Parameters:
        comp - the targetComponent
        controller - the controller code with onXxx$myid event handler methods
      • addForwards

        public static void addForwards​(Component comp,
                                       java.lang.Object controller,
                                       char separator)
        Adds forward conditions to the specified component with a custom separator. The separator is used to separate the component ID and additional information, such as event name. By default, it is '$'. However, for Groovy or other environment that '$' is not applicable, you can invoke this method to use '_' as the separator.
        See Also:
        addForwards(Component, Object)