// scan up the component hierarchy until the real owner is found and collect all cloned // components. private static List<Component> scanClones(DataBinder binder, Component comp) { if (DataBinder.isTemplate(comp)) { final List<Component> owners = scanClones(binder, binder.getCollectionOwner(comp)); // recursive final List<Component> kidowners = new LinkedList<Component>(); for (Component owner : owners) { final CollectionItem decor = binder.getCollectionItemByOwner(owner); // backward compatible, CollectionItemEx.getItems() is faster if (decor instanceof CollectionItemExt) { final CollectionItemExt decorex = (CollectionItemExt) decor; for (final Iterator iti = decorex.getItems(owner).iterator(); iti.hasNext(); ) { final Component item = (Component) iti.next(); kidowners.add(DataBinder.lookupClone(item, comp)); } } else { try { for (int j = 0; true; ++j) { // iterate until out of bound final Component item = decor.getComponentAtIndexByOwner(owner, j); kidowners.add(DataBinder.lookupClone(item, comp)); } } catch (IndexOutOfBoundsException ex) { // ignore, iterate until out of bound } } } return kidowners; } else { final List<Component> owners = new ArrayList<Component>(); owners.add(comp); return owners; } }
/** Get converted value and original value of this Binding. */ private Object[] getAttributeValues(Component comp) { if (!isSavable() || _attr.startsWith("_") || DataBinder.isTemplate(comp) || comp.getPage() == null) { return null; // cannot save, a control attribute, or a detached component, skip! } Object rawval = null; try { rawval = Fields.get(comp, _attr); } catch (NoSuchMethodException ex) { // Bug #1813278, Annotations do not work with xhtml tags if (comp instanceof DynamicPropertied) { final DynamicPropertied dpcomp = (DynamicPropertied) comp; if (dpcomp.hasDynamicProperty(_attr)) { rawval = dpcomp.getDynamicProperty(_attr); } else { throw UiException.Aide.wrap(ex); } } else if (comp.getAttributes() .containsKey( _attr)) { // Feature #2855116. Get value from component custom-attribute(also a // variable in ZK5). rawval = comp.getAttribute(_attr); } else { throw UiException.Aide.wrap(ex); } } try { final Object val = (_converter == null) ? rawval : _converter.coerceToBean(rawval, comp); return val == TypeConverter.IGNORE ? null : new Object[] {val, rawval}; } catch (ClassCastException ex) { throw UiException.Aide.wrap(ex); } }
/** * load bean value into the attribute of the specified component. * * @param comp the component. * @param bean the bean value. */ public void loadAttribute(Component comp, Object bean) { if (!isLoadable() || _attr.startsWith("_") || DataBinder.isTemplate(comp) || comp.getPage() == null) { return; // cannot load, a control attribute, or a detached component, skip! } myLoadAttribute(comp, bean); }
/** * load bean value into the attribute of the specified component. * * @param comp the component. */ public void loadAttribute(Component comp) { if (!isLoadable() || _attr.startsWith("_") || DataBinder.isTemplate(comp) || comp == null // bug #1941947 Cannot find associated CollectionItem || comp.getPage() == null) { return; // cannot load, a control attribute, or a detached component, skip! } Object bean = _binder.getBeanAndRegisterBeanSameNodes(comp, _expression); myLoadAttribute(comp, bean); }
protected void handleEvent(Event event) { for (BindingInfo bi : _dataTargets) { final Component dt = bi.getComponent(); final Binding binding = bi.getBinding(); final DataBinder binder = binding.getBinder(); final Component dataTarget = DataBinder.isTemplate(dt) ? DataBinder.lookupClone(event.getTarget(), dt) : dt; if (dataTarget != null) { binding.loadAttribute(dataTarget); } else { // #bug 2897202 final List<Component> clones = scanClones(binder, dt); for (final Component dataTarget1 : clones) { binding.loadAttribute(dataTarget1); } } } }
protected void handleEvent(Event event) { final Component target = event.getTarget(); final String triggerEventName = event.getName(); final List<BindingInfo> tmplist = new ArrayList<BindingInfo>(_dataTargets.size()); final List<Object> values = new LinkedList<Object>(); final List<Component> refs = new LinkedList<Component>(); final List<Binding> bindings = new LinkedList<Binding>(); // fire onSave for each binding for (BindingInfo bi : _dataTargets) { final Component dt = bi.getComponent(); final Binding binding = bi.getBinding(); final DataBinder binder = binding.getBinder(); final Component dataTarget = DataBinder.isTemplate(dt) ? DataBinder.lookupClone(target, dt) : dt; // bug# 1904389: NullPointerException if save-when in collection binding // event.getTarget() might not inside the collection item (i.e. row, listitem, etc.) // then binder.lookupClone() will return null dataTarget. if (dataTarget != null) { final Object[] vals = binding.getAttributeValues(dataTarget); if (vals != null) { tmplist.add(new BindingInfo(binding, dataTarget, vals)); values.add(vals[0]); refs.add(dataTarget); bindings.add(binding); Events.sendEvent( new BindingSaveEvent("onBindingSave", dataTarget, target, binding, vals[0])); } } else { // bi.getComponent a template and a null dataTarget, meaning all collection items has to // be handled. Search the owner to iterate all cloned items. final List<Component> clones = scanClones(binder, dt); for (Component dataTarget1 : clones) { final Object[] vals = binding.getAttributeValues(dataTarget1); if (vals != null) { tmplist.add(new BindingInfo(binding, dataTarget1, vals)); values.add(vals[0]); refs.add(dataTarget1); bindings.add(binding); Events.sendEvent( new BindingSaveEvent("onBindingSave", dataTarget1, target, binding, vals[0])); } } } } // fire onValidate for target component Events.sendEvent( new BindingValidateEvent("onBindingValidate", target, refs, bindings, values)); // saveAttribute for each binding Component loadOnSaveProxy = null; Component dataTarget = null; DataBinder binder = null; final List<Object> loadOnSaveInfos = new ArrayList<Object>(tmplist.size()); for (BindingInfo bi : tmplist) { dataTarget = bi.getComponent(); final Binding binding = bi.getBinding(); if (binder == null) { binder = binding.getBinder(); } final Object[] vals = bi.getAttributeValues(); binding.saveAttributeValue(dataTarget, vals, loadOnSaveInfos, triggerEventName); if (loadOnSaveProxy == null && dataTarget.isListenerAvailable("onLoadOnSave", true)) { loadOnSaveProxy = dataTarget; } } // bug #1895856 : data binding LoadOnSave works only on the last save-when component // do loadOnSave // if (dataTarget != null) { // Events.postEvent(new Event("onLoadOnSave", dataTarget, loadOnSaveInfos)); // } // (use first working dataTarget as proxy) // feature#2990932, allow disable load-on-save mechanism if (loadOnSaveProxy != null && binder.isLoadOnSave()) { Events.postEvent(new Event("onLoadOnSave", loadOnSaveProxy, loadOnSaveInfos)); } }