[zk工具类] 注解强化Composer

sun4love 2010-12-16

 

Java代码 
  1. import java.text.DecimalFormat;  
  2. import java.text.ParseException;  
  3. import java.util.List;  
  4.   
  5. import org.zkoss.zk.ui.Component;  
  6. import org.zkoss.zk.ui.util.GenericAutowireComposer;  
  7. import org.zkoss.zkplus.databind.AnnotateDataBinder;  
  8. import org.zkoss.zkplus.databind.DataBinder;  
  9.   
  10. import com.linktel.common.web.zk.converter.SexRadiogroupConverter;  
  11.   
  12. /** 
  13.  * 注解绑定类 
  14.  * <p> 
  15.  * 在GenericAutowireComposer基础上对注解进行增强, 因此你无需再页面上显式添加注解支持指令 
  16.  *  
  17.  * <pre> 
  18.  * &lt;?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?&gt; 
  19.  * </pre> 
  20.  * <p> 
  21.  *  
  22.  * @author sun4love 
  23.  *  
  24.  */  
  25. abstract public class GenericDataBinderComposer extends GenericAutowireComposer {  
  26.     private static final long serialVersionUID = -8962566563467903754L;  
  27.     protected DataBinder binder;  
  28.   
  29.     @Override  
  30.     public void doAfterCompose(Component comp) throws Exception {  
  31.         super.doAfterCompose(comp);  
  32.         binder = new AnnotateDataBinder(comp);  
  33.         comp.setAttribute("binder", binder);  
  34.     }  
  35.   
  36.     protected void loadAll() {  
  37.         binder.loadAll();  
  38.     }  
  39.   
  40.     protected void loadAttribute(Component comp, String attr) {  
  41.         binder.loadAttribute(comp, attr);  
  42.     }  
  43.   
  44.     protected void loadComponent(Component comp) {  
  45.         binder.loadComponent(comp);  
  46.     }  
  47.   
  48.     /** 
  49.      * 绑定bean到ui组件上 
  50.      * <p> 
  51.      * 范例 
  52.      * <hr /> 
  53.      *  
  54.      * <pre> 
  55.      * <code> 
  56.      * &lt;window id=&quot;userWin&quot; apply=&quot;GenericDataBinderComposerChild&quot;&gt; 
  57.      *    &lt;textbox id=&quot;txtFirstName&quot; /&gt; 
  58.      *    &lt;textbox id=&quot;txtlastName&quot; /&gt; 
  59.      *    &lt;label id=&quot;lblFullName&quot; /&gt; 
  60.      * &lt;/window&gt; 
  61.      * GenericDataBinderComposerChild类 
  62.      *  
  63.      * public GenericDataBinderComposerChild extends GenericDataBinderComposer{ 
  64.      *      private Textbox txtFirstName; 
  65.      *      private Textbox txtLastName; 
  66.      *      private Label lblFullName; 
  67.      *  
  68.      *      public void doAfterCompose(Component comp) throws Exception { 
  69.      *          super.doAfterCompose(comp); 
  70.      *          binder.addBinding(txtFirstName, "value", "userWin$composer.user.firstName"); 
  71.      *          binder.addBinding(txtLastName, "value", "userWin$composer.user.lastName"); 
  72.      *  
  73.      *          //"txtLastName.onBlur"中的txtLastName是页面上&lt;textbox id=&quot;txtlastName&quot; /&gt; 
  74.      *          //userWin$composer中的userWin是页面上window的id,$.composer是固定格式 
  75.      *          addBinding(lblFullName, "value", "userWin$composer.user.fullName","txtLastName.onBlur" , null,null,null); 
  76.      *      } 
  77.      * } 
  78.      * </pre> 
  79.      *  
  80.      * @param comp 
  81.      *            ui组件 
  82.      * @param attr 
  83.      *            ui组件的属性,例如&lt;textbox value="@{user.name}"/&gt;中的value 
  84.      * @param expr 
  85.      *            表达式,例如,例如&lt;textbox value="@{user.name}"/&gt;中的user.name 
  86.      * @param loadWhenEvents 
  87.      *            加载bean到ui的时机,例如comp1.onClck、comp2、onBlur、comp3.onSelect, 
  88.      *            等同于&lt;textbox id=&quot;txtFullName&quot; 
  89.      *            value=&quot;@{user.fullName, load-when=  
  90.      *            'txtLastName.onBlur'}&quot;/&gt;中的txtLastName.onBlur 
  91.      * @param saveWhenEvent 
  92.      *            保存ui value到bean的时机,例如comp3.onBlur, 等同于&lt;textbox 
  93.      *            id=&quot;txtFullName&quot; value=&quot;@{user.fullName, 
  94.      *            save-when=  
  95.      *            'txtFullName.onBlur',}&quot;/&gt;中的txtFullName.onBlur 
  96.      * @param access 
  97.      *            访问安全性,允许保存ui value 到bean,还是是load bean到ui,还是两者都是,或者什么都不做, 
  98.      *            可选值为none,save,load,both(save/load), 
  99.      * @param converter 
  100.      *            转换器,ui到bean之间的互相转换,类似hibernate jdbc type到db type或者struts中的转换器 
  101.      *            ,样例见{@link SexRadiogroupConverter} 
  102.      */  
  103.     protected void addBinding(Component comp, String attr, String expr,  
  104.             String loadWhenEvent, String saveWhenEvent, String access,  
  105.             String converter) {  
  106.         if (loadWhenEvent != null && "".equals(loadWhenEvent.trim())) {  
  107.             binder.addBinding(comp, attr, expr, (List) null, saveWhenEvent,  
  108.                     access, converter);  
  109.         } else {  
  110.             binder.addBinding(comp, attr, expr, new String[] { loadWhenEvent },  
  111.                     saveWhenEvent, access, converter);  
  112.         }  
  113.       
  114.     }  
  115.   
  116.     /** 
  117.      * 绑定bean到ui组件上 
  118.      * <p> 
  119.      * 范例 
  120.      * <hr /> 
  121.      *  
  122.      * <pre> 
  123.      * 页面 
  124.      * &lt;window id=&quot;userWin&quot; apply=&quot;GenericDataBinderComposerChild&quot;&gt; 
  125.      *    &lt;textbox id=&quot;txtFirstName&quot; /&gt; 
  126.      *    &lt;textbox id=&quot;txtlastName&quot; /&gt; 
  127.      *    &lt;label id=&quot;lblFullName&quot; /&gt; 
  128.      * &lt;/window&gt; 
  129.      * GenericDataBinderComposerChild类 
  130.      *  
  131.      * public GenericDataBinderComposerChild extends GenericDataBinderComposer{ 
  132.      *      private Textbox txtFirstName; 
  133.      *      private Textbox txtLastName; 
  134.      *      private Label lblFullName; 
  135.      *  
  136.      *      public void doAfterCompose(Component comp) throws Exception { 
  137.      *          super.doAfterCompose(comp); 
  138.      *          binder.addBinding(txtFirstName, "value", "userWin$composer.user.firstName"); 
  139.      *          binder.add
Global site tag (gtag.js) - Google Analytics