01: /* AnnotateDataBinderInit.java
02:
03: {{IS_NOTE
04: Purpose:
05:
06: Description:
07:
08: History:
09: Thu Nov 30 17:41:15 2006, Created by Henri Chen
10: }}IS_NOTE
11:
12: Copyright (C) 2006 Potix Corporation. All Rights Reserved.
13:
14: {{IS_RIGHT
15: }}IS_RIGHT
16: */
17: package org.zkoss.zkplus.databind;
18:
19: import org.zkoss.zk.ui.Page;
20: import org.zkoss.zk.ui.Path;
21: import org.zkoss.zk.ui.Component;
22: import org.zkoss.zk.ui.util.Initiator;
23:
24: /**
25: * <p>This initiator class do following things:</p>
26: * <ol>
27: * <li>new an {@link AnnotateDataBinder} instance.</li>
28: * <li>set the AnnotateDataBinder instance as a variable "binder" in the ID Space of the root
29: * component as specified in arg0 "component-path".(if arg0 is not specified, use Page instead.)</li>
30: * <li>call {@link AnnotateDataBinder#loadAll()} to initiate all UI components from the associated data bean.</li>
31: * </ol>
32: * <p>Put the init PI as follows:</p>
33: * <pre>
34: * <?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" [arg0="component|component-path"] [arg1="true|false"]?>
35: * </pre>
36: * <p>Where the arg0 is the component itself (via EL expression) or the component path that specifies the component the AnnotateDataBinder covers. If the
37: * arg0 is not specified or set to string "page", the AnnotateDataBinder will default to cover the whole page.</p>
38: * <p>Where the arg1 is used to decide whether to load default binding configuration defined in lang-addon.xml.
39: * If the arg1 is not specified it is default to true. Note that you have to specify arg0 if you want to specify arg1.</p>
40: *
41: * @author Henri Chen
42: * @see AnnotateDataBinder
43: */
44: public class AnnotateDataBinderInit implements Initiator {
45: private Component _comp;
46: private String _compPath;
47: private String _defaultConfig;
48:
49: /** The AnnotateDataBinder created in doAfterCompose() */
50: protected AnnotateDataBinder _binder;
51:
52: //-- Initator --//
53: public void doAfterCompose(Page page) {
54: boolean b = _defaultConfig != null ? Boolean.valueOf(
55: _defaultConfig).booleanValue() : true;
56: if (_comp instanceof Component) {
57: _binder = new AnnotateDataBinder(_comp, b);
58: _comp.setVariable("binder", _binder, true);
59: } else if (_compPath == null || "page".equals(_compPath)) {
60: _binder = new AnnotateDataBinder(page, b);
61: page.setVariable("binder", _binder);
62: } else if (_compPath.startsWith("/")) {
63: final Component comp = Path.getComponent(_compPath);
64: _binder = new AnnotateDataBinder(comp, b);
65: comp.setVariable("binder", _binder, true);
66: } else {
67: final Component comp = page.getFellow(_compPath);
68: _binder = new AnnotateDataBinder(comp, b);
69: comp.setVariable("binder", _binder, true);
70: }
71: _binder.loadAll(); //load data bean properties into UI components
72: }
73:
74: public boolean doCatch(java.lang.Throwable ex) {
75: return false; // do nothing
76: }
77:
78: public void doFinally() {
79: // do nothing
80: }
81:
82: public void doInit(Page page, java.lang.Object[] args) {
83: if (args.length > 0) {
84: Object arg0 = args[0];
85: if (arg0 instanceof String) {
86: _compPath = (String) arg0;
87: } else if (arg0 instanceof Component) {
88: _comp = (Component) arg0;
89: }
90: }
91:
92: if (args.length > 1) {
93: _defaultConfig = (String) args[1];
94: }
95: }
96: }
|