📄 databinder.java
字号:
myLoadAttribute(comp); } private void myLoadAttribute(Component comp) { if ( _attr.startsWith("_") || comp.getPage() == null) { //cannot load or a control attribute, skip return; } Object val = null; try { Object bean = lookupBean(comp, _beanid); registerSameBeanid(bean); val = (_props == null || bean == null) ? bean : Fields.getField(bean, _props); if (_converter != null) { val = _converter.coerceToUi(val, comp); } Fields.setField(comp, _attr, val, _converter == null); } catch (ClassCastException ex) { throw UiException.Aide.wrap(ex); } catch (NoSuchMethodException ex) { throw UiException.Aide.wrap(ex); } catch (ModificationException ex) { throw UiException.Aide.wrap(ex); } catch (WrongValueException ex) { //Bug #1615371, try to use setRawValue() if ("value".equals(_attr)) { try { Fields.setField(comp, "rawValue", val, _converter == null); } catch (Exception ex1) { //exception throw ex; } } else { throw ex; } } } private void saveAttribute(Component comp) { if (!canSave()) { //cannot save, skip return; } mySaveAttribute(comp); } private void mySaveAttribute(Component comp) { if (_attr.startsWith("_") || comp.getPage() == null) { //cannot save or a control attribute, skip return; } try { final Object bean = lookupBean(comp, _beanid); Object val = Fields.getField(comp, _attr); if (_converter != null) { val = _converter.coerceToBean(val, comp); } if (_props == null) { //assign back to where bean is stored if (!isClone(comp)) { if (_beans.containsKey(_beanid)) { _beans.put(_beanid, val); } else { comp.setVariable(_beanid, val, false); } } //load what is changed //bean == val, nothing to load //bean != null && val == null, get list of bean(remove list of bean), load the list, . //bean == null && val != null, get list of beanid, load the list, register binding with val, add val as binding key. //bean != null && val != null, get list of bean, load the list, register binding with val, change binding key from bean to val. if (bean != val) { if (val == null) { final Set beanidSet = (Set) _sameBeanidMap.remove(bean); loadDependencies(comp, val, beanidSet); } else { Set beanidSet = (Set) _sameBeanidMap.get(bean == null ? _beanid : bean); if (beanidSet != null) { //merge beanidSet final Set valBeanidSet = (Set) _sameBeanidMap.get(val); if (valBeanidSet != null) { valBeanidSet.addAll(beanidSet); beanidSet = valBeanidSet; } else { _sameBeanidMap.put(val, beanidSet); } } loadDependencies(comp, val, beanidSet); registerSameBeanid(val); } } } else if (bean != null) { Fields.setField(bean, _props, val, _converter == null); final Set beanidSet = (Set) _sameBeanidMap.get(bean); loadDependencies(comp, bean, beanidSet); registerSameBeanid(bean); } else { throw new UiException("saveAttribute: Cannot find the specified bean: "+_beanid+" in "+getValue()); }/* final String value = getValue(); final String[] keys = parseExpression(value, "."); final List bindings = _dependency.getAllBindings(keys); for(final Iterator it = bindings.iterator(); it.hasNext();) { final Binding binding = (Binding) it.next(); if (binding == this) { //don't load that just save, skip continue; } binding.loadAttributeByReference(comp); }*/ } catch (ClassCastException ex) { throw UiException.Aide.wrap(ex); } catch (NoSuchMethodException ex) { throw UiException.Aide.wrap(ex); } catch (ModificationException ex) { throw UiException.Aide.wrap(ex); } } private void loadDependencies(Component comp, Object bean, Set beanidSet) { //automatically load back dependent component if (bean instanceof Component) { loadComponent((Component) bean); } //same beanid must be load back if (beanidSet != null) { for(final Iterator its = beanidSet.iterator(); its.hasNext();) { final String beanid = (String) its.next(); final Set bindings = (Set) _beanBindingMap.get(beanid); if (bindings != null) { for(final Iterator it = bindings.iterator(); it.hasNext();) { final Binding binding = (Binding) it.next(); if (binding == this) { //don't load that just save, skip continue; } binding.loadAttributeByReference(comp, bean); } } } } } private Object lookupBean(Component comp, String beanid) { //check collection template special case first Object bean = NA; if (isClone(comp)) { bean = getListModelItem(comp, beanid); } if (bean == NA) { //not available //fetch the bean object if (_beans.containsKey(beanid)) { bean = _beans.get(beanid); } else if (beanid.startsWith("/")) { //a component Path in ID Space bean = Path.getComponent(beanid); } else { bean = comp.getVariable(beanid, false); } } return bean; } private String[] splitBeanid(String expr) { String beanid = null; String props = null; int j = expr.indexOf("."); if (j < 0) { //bean only beanid = expr; props = null; } else { beanid = expr.substring(0, j); props = expr.substring(j+1); } String[] results = new String[2]; results[0] = beanid; results[1] = props; return results; } private abstract class BaseEventListener implements EventListener, Express { protected Component _dataTarget; public BaseEventListener(Component dataTarget) { _dataTarget = dataTarget; } /* public Component getDataTarget() { return _dataTarget; }*/ public boolean isAsap() { return true; } } private class LoadEventListener extends BaseEventListener { public LoadEventListener(Component dataTarget) { super(dataTarget); } public void onEvent(Event event) { final Object dataTarget = lookupComponentByReference(null, event.getTarget(), _dataTarget); if (dataTarget != NA) { myLoadAttribute((Component )dataTarget); } } } private class SaveEventListener extends BaseEventListener { public SaveEventListener(Component dataTarget) { super(dataTarget); } public void onEvent(Event event) { final Object dataTarget = lookupComponentByReference(null, event.getTarget(), _dataTarget); if (dataTarget != NA) { mySaveAttribute((Component )dataTarget); } } } } private class Renderer implements org.zkoss.zul.ListitemRenderer { private Listitem _template; private Renderer(Listitem template) { _template = template; } public void render(Listitem item, java.lang.Object bean) { //clone from template final Listitem clone = (Listitem)_template.clone(); setupTemplate(clone, null); //not template for cloned component tree //copy children into item from clone final List clonekids = clone.getChildren(); item.getChildren().clear(); //addAll will cause kids to move parent and thus change clonekids, must make a copy item.getChildren().addAll(new ArrayList(clonekids)); //setup the clone listitem and collect comp table for template Map compMap = new HashMap(7); setupClone(item, _template, item, compMap); //setup cloneMap for tempalte _cloneMap.put(item, compMap); if (bean != null) { _cloneMap.put(bean, compMap); } //apply the data binding loadComponent(item); } //link cloned to template, collection Save & Load Event mapping information, remove id private void setupClone(Component comp, Component template, Component item, Map compMap) { if (_compBindingMap.containsKey(template)) { //as long as there is a binding compMap.put(template, comp); } setTemplateComponent(comp, template); comp.setAttribute(INDEXITEM, item); comp.setId("@"+comp.getUuid()); //init id to @uuid to avoid duplicate id issue //setup clone kids final Iterator itt = template.getChildren().iterator(); final Iterator itc = comp.getChildren().iterator(); while (itt.hasNext()) { final Component t = (Component) itt.next(); final Component c = (Component) itc.next(); setupClone(c, t, item, compMap); //recursive } } } /* //dependency Node, when a bean saveXXX, the associate loadXXX should be called to reflect the UI component private static class Node { private List _bindingList; private Map _kids = new LinkedHashMap(7); public void add(String[] keys, Binding binding) { insert(keys, binding, 0); } public List getAllBindings(String[] keys) { final Node node = locate(keys, 0); if (node != null) { final List bindings = new ArrayList(32); node.collectBindings(bindings); return bindings; } return null; } private void addBinding(Binding binding) { if (_bindingList == null) { _bindingList = new ArrayList(8); } _bindingList.add(binding); } private void insert(String[] keys, Binding binding, int level) { final String key = keys[level]; Node next = getKidNode(key); if (next == null) { next = new Node(); addKidNode(key, next); } if (keys.length <= (level+1)) { next.addBinding(binding); } else { next.insert(keys, binding, level+1); //recursive } } private Node locate(String[] keys, int level) { final String key = keys[level]; Node next = getKidNode(key); if (keys.length <= (level+1) || next == null) { return next; } else { return next.locate(keys, level+1); //recursive } } private void collectBindings(List list) { if (_bindingList != null) { list.addAll(_bindingList); } for(final Iterator it = _kids.values().iterator(); it.hasNext(); ) { final Node kid = (Node) it.next(); kid.collectBindings(list); //recursive } } private void addKidNode(String key, Node node) { _kids.put(key, node); } private Node getKidNode(String key) { return (Node) _kids.get(key); } } */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -