📄 annotatedatabinder.java
字号:
* It is used to convert the value between component attribute and bean field. Multiple definition is NOT allowed * and the later defined would override the previous defined one. * Most of the time you don't have to specify this since this DataBinder supports converting most commonly * used types. However, if you specify the TypeConverter class name, this DataBinder will new an instance and use * it to cast the class. * </li> * </ul> * * @since 2.4.0 Supporting @{...} annotations. * @since 3.0.0 Supporting multiple events of save-when tag and validation phase. * @author Henri Chen * @see AnnotateDataBinderInit * @see DataBinder */public class AnnotateDataBinder extends DataBinder { /** * Constructor that read all binding annotations of the components inside the specified desktop. * @param desktop the ZUML desktop. */ public AnnotateDataBinder(Desktop desktop) { this(desktop, true); } /** * Constructor that read all binding annotations of the components inside the specified page. * @param page the ZUML page. */ public AnnotateDataBinder(Page page) { this(page, true); } /** * Constructor that read all binding annotations in the components inside the specified component (inclusive). * @param comp the ZUML component. */ public AnnotateDataBinder(Component comp) { this(comp, true); } /** * Constructor that read all binding annotations of the given components array. * @param comps the Component array. * @since 3.0.0 */ public AnnotateDataBinder(Component[] comps) { this(comps, true); } /** * Constructor that read all binding annotations of the components inside the specified desktop. * @param desktop the ZUML desktop. * @param defaultConfig whether load default binding configuration defined in lang-addon.xml */ public AnnotateDataBinder(Desktop desktop, boolean defaultConfig) { setDefaultConfig(defaultConfig); for (final Iterator it = desktop.getComponents().iterator(); it.hasNext(); ) { loadAnnotations((Component) it.next()); } } /** * Constructor that read all binding annotations of the components inside the specified page. * @param page the ZUML page. * @param defaultConfig whether load default binding configuration defined in lang-addon.xml */ public AnnotateDataBinder(Page page, boolean defaultConfig) { setDefaultConfig(defaultConfig); for (final Iterator it = page.getRoots().iterator(); it.hasNext(); ) { loadAnnotations((Component) it.next()); } } /** * Constructor that read all binding annotations of the given component array. * @param comps the Component array * @param defaultConfig whether load default binding configuration defined in lang-addon.xml * @since 3.0.0 */ public AnnotateDataBinder(Component[] comps, boolean defaultConfig) { setDefaultConfig(defaultConfig); for (int j = 0; j < comps.length; ++j) { loadAnnotations(comps[j]); } } /** * Constructor that read all binding annotations in the components inside the specified component (inclusive). * @param comp the ZUML component. * @param defaultConfig whether load default binding configuration defined in lang-addon.xml */ public AnnotateDataBinder(Component comp, boolean defaultConfig) { setDefaultConfig(defaultConfig); loadAnnotations(comp); } private void loadAnnotations(Component comp) { loadComponentAnnotation(comp); loadComponentPropertyAnnotation(comp); final List children = comp.getChildren(); for (final Iterator it = children.iterator(); it.hasNext(); ) { loadAnnotations((Component) it.next()); //recursive back } } private void loadComponentPropertyAnnotation(Component comp) { loadComponentPropertyAnnotationByAnnotName(comp, "default"); loadComponentPropertyAnnotationByAnnotName(comp, "bind"); } private void loadComponentPropertyAnnotationByAnnotName(Component comp, String annotName) { ComponentCtrl compCtrl = (ComponentCtrl) comp; final List props = compCtrl.getAnnotatedPropertiesBy(annotName); for (final Iterator it = props.iterator(); it.hasNext(); ) { final String propName = (String) it.next(); //[0] value, [1] loadWhenEvents, [2] saveWhenEvents, [3] access, [4] converter final Object[] objs = loadPropertyAnnotation(comp, propName, annotName); addBinding(comp, propName, (String) objs[0], (List) objs[1], (List) objs[2], (String) objs[3], (String) objs[4]); } } private void loadComponentAnnotation(Component comp) { loadComponentAnnotation(comp, "default"); loadComponentAnnotation(comp, "bind"); } private void loadComponentAnnotation(Component comp, String annotName) { ComponentCtrl compCtrl = (ComponentCtrl) comp; Annotation ann = compCtrl.getAnnotation(annotName); if (ann != null) { Map attrs = ann.getAttributes(); for(final Iterator it = attrs.entrySet().iterator(); it.hasNext();) { Entry me = (Entry) it.next(); String attr = (String) me.getKey(); //[0] bean value, [1 ~ *] tag:expression List expr = parseExpression((String) me.getValue(), ";"); if (expr == null || expr.get(0) == null) { throw new UiException("Cannot find any bean value in the annotation <a:bind "+attr+"=\"\"/> for component "+comp+", id="+comp.getId()); } else { List tags = parseExpression((String)expr.get(0), ":"); if (tags.size() > 1) { throw new UiException("bean value must be defined as the first statement in the annotation <a:bind "+attr+"=\"\"/> for component "+comp+", id="+comp.getId()); } } List loadWhenEvents = null; List saveWhenEvents = null; String access = null; String converter = null; //process tags for(int j = 1; j < expr.size(); ++j) { List tags = parseExpression((String)expr.get(j), ":"); if (tags == null) { continue; //skip } if ("load-when".equals(tags.get(0))) { if (tags.size() > 1 && tags.get(1) != null) { loadWhenEvents = parseExpression((String)tags.get(1), ","); } else { loadWhenEvents.add(NULLIFY); } } else if ("save-when".equals(tags.get(0))) { if (tags.size() > 1 && tags.get(1) != null) { saveWhenEvents = parseExpression((String)tags.get(1), ","); } else { saveWhenEvents.add(NULLIFY); } } else if ("access".equals(tags.get(0))) { access = tags.size() > 1 ? (String) tags.get(1) : NULLIFY; } else if ("converter".equals(tags.get(0))) { converter = tags.size() > 1 ? (String) tags.get(1) : NULLIFY; } } if (loadWhenEvents != null && loadWhenEvents.isEmpty()) { loadWhenEvents = null; } if (saveWhenEvents != null && saveWhenEvents.isEmpty()) { saveWhenEvents = null; } addBinding(comp, attr, (String) expr.get(0), loadWhenEvents, saveWhenEvents, access, converter); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -