Implement Custom Java Class"

From Documentation
Line 5: Line 5:
 
=Implement Custom Java Class for Macro=
 
=Implement Custom Java Class for Macro=
  
A custom Java class for macro components must extend from <javadoc>org.zkoss.zk.ui.HtmlMacroComponent</javadoc>. <javadoc>org.zkoss.zk.ui.HtmlMacroComponent</javadoc> will wire fellows and event listener automatically, so the implementation is straightforward.
+
The implementation is straightforward. First, the custom Java class for macro components must extend from <javadoc>org.zkoss.zk.ui.HtmlMacroComponent</javadoc>. Second, it shall invoke <javadoc method="compose">org.zkoss.zk.ui.HtmlMacroComponent</javadoc> in the constructor.
  
 
For example, suppose we have a macro template as follows.
 
For example, suppose we have a macro template as follows.
  
 
<source lang="xml" >
 
<source lang="xml" >
<hbox>
+
<hlayout>
 
Username: <textbox id="who"/>
 
Username: <textbox id="who"/>
</hbox>
+
</hlayout>
 
</source>
 
</source>
  
Line 22: Line 22:
  
 
public class Username extends HtmlMacroComponent {
 
public class Username extends HtmlMacroComponent {
     private Textbox who;
+
     private Textbox who; //will be wired when compose() is called
 
     public Username() {
 
     public Username() {
 
         compose(); //fore the template to be applied
 
         compose(); //fore the template to be applied
Line 34: Line 34:
 
}
 
}
 
</source>
 
</source>
 +
 +
Notice that <javadoc method="compose()">org.zkoss.zk.ui.HtmlMacroComponent</javadoc> will wire fellows and event listener automatically, so we could access them directly (such as the <code>who</code> member).
  
 
=Declare Macro with Custom Java Class=
 
=Declare Macro with Custom Java Class=

Revision as of 10:29, 8 November 2010


Implement Custom Java Class


As described in the earlier sections, a macro component is instantiated to represent a regular macro. By default, HtmlMacroComponent is assumed (and instantiated). However, you provide a custom Java class to provide a better API to simplify the access and to encapsulate the implementation.

Implement Custom Java Class for Macro

The implementation is straightforward. First, the custom Java class for macro components must extend from HtmlMacroComponent. Second, it shall invoke HtmlMacroComponent.compose in the constructor.

For example, suppose we have a macro template as follows.

<hlayout>
	Username: <textbox id="who"/>
</hlayout>

Then, we could implement a Java class for it:

import org.zkoss.zk.ui.HtmlMacroComponent;
import org.zkoss.zul.Textbox;

public class Username extends HtmlMacroComponent {
    private Textbox who; //will be wired when compose() is called
    public Username() {
        compose(); //fore the template to be applied
    }
    public String getWho() {
        return this.who.getValue();
    }
    public void setWho(String who) {
        this.who.setValue(who);
    }
}

Notice that HtmlMacroComponent.compose() will wire fellows and event listener automatically, so we could access them directly (such as the who member).

Declare Macro with Custom Java Class

Use Macro with Custom Java Class

In ZUML

The use of the macro component with a custom Java class in a ZUML page is the same as other macro components.

In Java

The main purpose of introducing a custom Java class is to simplify the use of a macro component in Java. For example, you could invoke a more meaningful setter directly rather than DynamicPropertied.setDynamicProperty(String, Object). In additions, the instantiation could be as simple as follows:

Username ua = new Username();
ua.setParent(wnd);
ua.setWho("Joe");

Version History

Last Update : 2010/11/8

Version Date Content
5.0.5 October, 2010 HtmlMacroComponent.compose() was introduced.



Last Update : 2010/11/08

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.