📄 databinder.java
字号:
} //save kids of this component for(final Iterator it = comp.getChildren().iterator(); it.hasNext();) { saveComponent((Component) it.next()); //recursive } } /** Load all value from data beans to UI components. */ public void loadAll() { init(); for (final Iterator it = _compBindingMap.keySet().iterator(); it.hasNext(); ) { final Component comp = (Component) it.next(); loadComponent(comp); } } /** Save all values from UI components to beans. */ public void saveAll() { init(); for (final Iterator it = _compBindingMap.keySet().iterator(); it.hasNext(); ) { final Component comp = (Component) it.next(); saveComponent(comp); } } private void loadAttrs(String expr, Collection attrs) { for(final Iterator it = attrs.iterator(); it.hasNext();) { Binding binding = (Binding) it.next(); Component comp = binding.getComponent(); binding.loadAttribute(comp, expr); } } private void loadAttrs(Component comp, Collection attrs) { for(final Iterator it = attrs.iterator(); it.hasNext();) { Binding binding = (Binding) it.next(); binding.loadAttribute(comp); } } private void saveAttrs(Component comp, Collection attrs) { for(final Iterator it = attrs.iterator(); it.hasNext();) { Binding binding = (Binding) it.next(); binding.saveAttribute(comp); } } //[0] expr, [1] loadWhenEvents, [2] saveWhenEvents, [3] access, [4] converter protected Object[] loadPropertyAnnotation(Component comp, String propName, String bindName) { ComponentCtrl compCtrl = (ComponentCtrl) comp; Annotation ann = compCtrl.getAnnotation(propName, bindName); if (ann != null) { final Map attrs = ann.getAttributes(); //(tag, tagExpr) List loadWhenEvents = null; List saveWhenEvents = null; String access = null; String converter = null; String expr = null; for (final Iterator it = attrs.entrySet().iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); String tag = (String) entry.getKey(); String tagExpr = (String) entry.getValue(); if ("save-when".equals(tag)) { saveWhenEvents = parseExpression(tagExpr, ","); } else if ("access".equals(tag)) { access = tagExpr; } else if ("converter".equals(tag)) { converter = tagExpr; } else if ("load-when".equals(tag)) { loadWhenEvents = parseExpression(tagExpr, ","); } else if ("value".equals(tag)) { expr = tagExpr; } } return new Object[] {expr, loadWhenEvents, saveWhenEvents, access, converter}; } return new Object[5]; } //late init protected void init() { if (!_init) { _init = true; // init CollectionItem initCollectionItem(); //setup all added bindings final Set varnameSet = new HashSet(); final LinkedHashSet toBeDetached = new LinkedHashSet(); for(final Iterator it = _compBindingMap.entrySet().iterator(); it.hasNext(); ) { final Entry me = (Entry) it.next(); final Component comp = (Component) me.getKey(); final Map attrMap = (Map) me.getValue(); final Collection bindings = attrMap.values(); //_var special case; meaning a template component if (attrMap.containsKey("_var")) { setupTemplateComponent(comp, getComponentCollectionOwner(comp)); //setup as template components String varname = ((Binding)attrMap.get("_var")).getExpression(); varnameSet.add(varname); comp.setAttribute(VARNAME, varname); setupBindingRenderer(comp); //setup binding renderer toBeDetached.add(comp); } if (bindings != null) { //construct the path dependant tree setupPathTree(bindings, varnameSet); //register save-when event registerSaveEvents(comp, bindings); //register load-when events registerLoadEvents(comp, bindings); } } //detach template components so they will not interfer the visual part for(final Iterator it = toBeDetached.iterator(); it.hasNext(); ) { final Component comp = (Component) it.next(); comp.detach(); } } } private void initCollectionItem(){ addCollectionItem(Listitem.class.getName(), new ListitemCollectionItem()); addCollectionItem(Row.class.getName(), new RowCollectionItem()); } /** * Adds a CollectionItem for this comp. * @see CollectionItem * @since 3.0.0 */ public void addCollectionItem(String comp, CollectionItem decor){ _collectionItemMap.put(comp, decor); } //get Collection owner of a given collection item. private Component getComponentCollectionOwner(Component comp) { CollectionItem decor = getBindingCollectionItem(comp); return decor.getComponentCollectionOwner(comp); } /** * Returns a CollectionItem by the comp accordingly. * @see CollectionItem * @since 3.0.0 */ protected CollectionItem getBindingCollectionItem(Component comp){ String name = comp.getClass().getName(); if (comp instanceof Listitem) { name = Listitem.class.getName(); } else if (comp instanceof Row) { name = Row.class.getName(); } CollectionItem decorName = (CollectionItem)_collectionItemMap.get(name); if(decorName != null){ return decorName; }else{ throw new UiException("Cannot find associated CollectionItem:"+comp); } } //get Collection owner of a given collection item. private Component getCollectionOwner(Component comp) { if (isTemplate(comp)) { return (Component) comp.getAttribute(OWNER); } return getComponentCollectionOwner(comp); } //get associated clone of a given bean and template component private Component getCollectionItem(Component comp, Object bean) { Component owner = getCollectionOwner(comp); CollectionItem decor = getBindingCollectionItem(comp); final ListModel xmodel = decor.getModelByOwner(owner); if (xmodel instanceof BindingListModel) { final BindingListModel model = (BindingListModel) xmodel; int index = model.indexOf(bean); if (index >= 0) { return lookupClone(decor.getComponentAtIndexByOwner(owner, index), comp); } } return null; } //set the binding renderer for the template listitem component private void setupBindingRenderer(Component comp) { getBindingCollectionItem(comp).setupBindingRenderer(comp, this); } private void setupPathTree(Collection bindings, Set varnameSet) { for(final Iterator it = bindings.iterator(); it.hasNext(); ) { final Binding binding = (Binding) it.next(); String[] paths = binding.getPaths(); for(int j = 0; j < paths.length; ++j) { final String path = (String) paths[j]; _pathTree.addBinding(path, binding, varnameSet); } } } private void registerSaveEvents(Component comp, Collection bindings) { for(final Iterator it = bindings.iterator(); it.hasNext(); ) { final Binding binding = (Binding) it.next(); binding.registerSaveEvents(comp); } } private void registerLoadEvents(Component comp, Collection bindings) { for(final Iterator it = bindings.iterator(); it.hasNext(); ) { final Binding binding = (Binding) it.next(); binding.registerLoadEvents(comp); } } //whether exists the specified bean in this DataBinder. /* package */ boolean existsBean(String beanid) { return _beans.containsKey(beanid); } //get a bean by the beanid from this Data binder /* package */ Object getBean(String beanid) { return _beans.get(beanid); } //set a bean into this Data binder /* package */ void setBean(String beanid, Object bean) { _beans.put(beanid, bean); } /** * Sets up the specified comp and its decendents to be as template (or not) */ public void setupTemplateComponent(Component comp, Object owner) { if (existsBindings(comp)) { if (comp.getAttribute(OWNER) != null) { comp.setAttribute(HASTEMPLATEOWNER, Boolean.TRUE); //owner is a template } comp.setAttribute(OWNER, owner); } List kids = comp.getChildren(); for(final Iterator it = kids.iterator(); it.hasNext(); ) { setupTemplateComponent((Component) it.next(), owner); //recursive } } //parse token and return as a List of String /* package */ static List parseExpression(String expr, String separator) { if (expr == null) { return null; } List results = new ArrayList(5); while(true) { int j = expr.indexOf(separator); if (j < 0) { results.add(expr.trim()); return results; } results.add(expr.substring(0, j).trim()); if (expr.length() <= (j+1)) { return results; } expr = expr.substring(j+1); } } //whether a component is a binding template rather than a real component /* package */ static boolean isTemplate(Component comp) { return comp.getAttribute(OWNER) != null; } //whether a cloned component from the template. /* package */ static boolean isClone(Component comp) { //bug #1813055 Multiple listboxes with same selectedItem causes NPE return comp != null && (comp.getAttribute(TEMPLATE) instanceof Component); } //whether has template owner (collection in collection) /* package */ static boolean hasTemplateOwner(Component comp) { //bug #1813055 Multiple listboxes with same selectedItem causes NPE return comp != null && (comp.getAttribute(HASTEMPLATEOWNER) != null); } //set a bean to SameNode Set /* package */ void setBeanSameNodes(Object bean, Set set) { _beanSameNodes.put(bean, set); } //get SameNode Set of the given bean /* package */ Set getBeanSameNodes(Object bean) { return (Set) _beanSameNodes.get(bean); } //remove SameNode set of the given bean /* package */ Set removeBeanSameNodes(Object bean) { return (Set) _beanSameNodes.remove(bean); } /** traverse the path nodes and return the final bean. */ /* package */ Object getBeanAndRegisterBeanSameNodes(Component comp, String path) { return myGetBeanWithExpression(comp, path, true); } private Object getBeanWithExpression(Component comp, String path) { return myGetBeanWithExpression(comp, path, false); } private Object myGetBeanWithExpression(Component comp, String path, boolean registerNode) { Object bean = null; BindingNode currentNode = _pathTree; final List nodeids = parseExpression(path, "."); final Iterator it = nodeids.iterator(); if (it != null && it.hasNext()) { String nodeid = (String) it.next(); currentNode = (BindingNode) currentNode.getKidNode(nodeid); if (currentNode == null) { throw new UiException("Cannot find the specified databind bean expression:" + path); } bean = lookupBean(comp, nodeid); if (registerNode) { registerBeanNode(bean, currentNode); } } else { throw new UiException("Incorrect format of databind bean expression:" + path); } while(bean != null && it.hasNext()) { String nodeid = (String) it.next(); currentNode = (BindingNode) currentNode.getKidNode(nodeid); if (currentNode == null) { throw new UiException("Cannot find the specified databind bean expression:" + path); } try { bean = Fields.get(bean, nodeid); } catch (NoSuchMethodException ex) { throw UiException.Aide.wrap(ex); } if (registerNode) { registerBeanNode(bean, currentNode); } } return bean; } /* package */ void setBeanAndRegisterBeanSameNodes(Component comp, Object val, Binding binding, String path, boolean autoConvert, Object rawval, List loadOnSaveInfos) { Object orgVal = null; Object bean = null; BindingNode currentNode = _pathTree; boolean refChanged = false; //wether this setting change the reference String beanid = null; final List nodeids = parseExpression(path, "."); final List nodes = new ArrayList(nodeids.size()); final Iterator it = nodeids.iterator(); if (it != null && it.hasNext()) { beanid = (String) it.next(); currentNode = (BindingNode) currentNode.getKidNode(beanid); if (currentNode == null) { throw new UiException("Cannot find the specified databind bean expression:" + path); } nodes.add(currentNode); bean = lookupBean(comp, beanid); } else { throw new UiException("Incorrect format of databind bean expression:" + path); } if (!it.hasNext()) { //assign back to where bean is stored orgVal = bean; if(Objects.equals(orgVal, val)) { return; //same value, no need to do anything } if (existsBean(beanid)) { setBean(beanid, val); } else if (!setZScriptVariable(comp, beanid, val)) { comp.setVariable(beanid, val, false); } refChanged = true; } else { if (bean == null) { return; //no bean to set value, skip } int sz = nodeids.size() - 2; //minus first and last beanid in path for(;bean != null && it.hasNext() && sz > 0; --sz) { beanid = (String) it.next(); currentNode = (BindingNode) currentNode.getKidNode(beanid); if (currentNode == null) { throw new UiException("Cannot find the specified databind bean expression:" + path); } nodes.add(currentNode); try { bean = Fields.get(bean, beanid); } catch (NoSuchMethodException ex) { throw UiException.Aide.wrap(ex); } } if (bean == null) { return; //no bean to set value, skip } try { beanid = (String) it.next(); orgVal = Fields.get(bean, beanid); if(Objects.equals(orgVal, val)) { return; //same value, no need to do anything } Fields.set(bean, beanid, val, autoConvert); if (!isPrimitive(val) && !isPrimitive(orgVal)) { //val is a bean (null is not primitive) currentNode = (BindingNode) currentNode.getKidNode(beanid); if (currentNode == null) { throw new UiException("Cannot find the specified databind bean expression:" + path); } nodes.add(currentNode); bean = orgVal; refChanged = true; } } catch (NoSuchMethodException ex) { throw UiException.Aide.wrap(ex); } catch (ModificationException ex) { throw UiException.Aide.wrap(ex);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -