org.zkoss.bind.impl.FormBindingHandler Maven / Gradle / Ivy
/* FormBindingHelper.java
Purpose:
Description:
History:
2011/11/14 Created by Dennis Chen
Copyright (C) 2011 Potix Corporation. All Rights Reserved.
*/
package org.zkoss.bind.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zkoss.bind.BindContext;
import org.zkoss.bind.Phase;
import org.zkoss.bind.Property;
import org.zkoss.bind.sys.Binding;
import org.zkoss.bind.sys.InitFormBinding;
import org.zkoss.bind.sys.LoadFormBinding;
import org.zkoss.bind.sys.SaveFormBinding;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
/**
* to help form-binding implementation of BinderImpl
* @author dennis
* @since 6.0.0
*/
/*package*/ class FormBindingHandler extends AbstractBindingHandler{
private static final long serialVersionUID = 1L;
private static final Logger _log = LoggerFactory.getLogger(FormBindingHandler.class);
private final Map> _initFormBindings; //comp+formid -> bindings (load form _prompt)
private final Map> _loadFormPromptBindings; //comp+formid -> bindings (load form _prompt)
private final Map> _loadFormAfterBindings; //command -> bindings (load form after command)
private final Map> _saveFormAfterBindings; //command -> bindings (save form after command)
private final Map> _loadFormBeforeBindings; //command -> bindings (load form before command)
private final Map> _saveFormBeforeBindings; //command -> bindings (save form before command)
FormBindingHandler(){
_initFormBindings = new HashMap>();
_loadFormPromptBindings = new HashMap>();
_loadFormAfterBindings = new HashMap>();
_saveFormAfterBindings = new HashMap>();
_loadFormBeforeBindings = new HashMap>();
_saveFormBeforeBindings = new HashMap>();
}
void addLoadPromptBinding(BindingKey bkey, LoadFormBinding binding) {
List bindings = _loadFormPromptBindings.get(bkey);
if (bindings == null) {
bindings = new ArrayList();
_loadFormPromptBindings.put(bkey, bindings);
}
bindings.add(binding);
}
void addInitBinding(BindingKey bkey, InitFormBinding binding) {
List bindings = _initFormBindings.get(bkey);
if (bindings == null) {
bindings = new ArrayList();
_initFormBindings.put(bkey, bindings);
}
bindings.add(binding);
}
void addLoadBeforeBinding(String command, LoadFormBinding binding) {
List bindings = _loadFormBeforeBindings.get(command);
if (bindings == null) {
bindings = new ArrayList();
_loadFormBeforeBindings.put(command, bindings);
}
bindings.add(binding);
}
void addLoadAfterBinding(String command, LoadFormBinding binding) {
List bindings = _loadFormAfterBindings.get(command);
if (bindings == null) {
bindings = new ArrayList();
_loadFormAfterBindings.put(command, bindings);
}
bindings.add(binding);
}
void addSaveBeforeBinding(String command, SaveFormBinding binding) {
List bindings = _saveFormBeforeBindings.get(command);
if (bindings == null) {
bindings = new ArrayList();
_saveFormBeforeBindings.put(command, bindings);
}
bindings.add(binding);
}
void addSaveAfterBinding(String command, SaveFormBinding binding) {
List bindings = _saveFormAfterBindings.get(command);
if (bindings == null) {
bindings = new ArrayList();
_saveFormAfterBindings.put(command, bindings);
}
bindings.add(binding);
}
Map> getSaveFormBeforeBindings() {
return _saveFormBeforeBindings;
}
Map> getSaveFormAfterBindings() {
return _saveFormAfterBindings;
}
void doSaveBefore(Component comp, String command, Event evt, Set notifys) {
final List bindings = _saveFormBeforeBindings.get(command);
if (bindings != null) {
for (SaveFormBinding binding : bindings) {
doSaveBinding(comp, binding, command, evt, notifys);
}
}
}
//generic operation to save a property binding
private void doSaveBinding(Component comp, SaveFormBinding binding, String command, Event evt, Set notifys) {
final BindContext ctx = BindContextUtil.newBindContext(_binder, binding, true, command, binding.getComponent(), evt);
BindContextUtil.setValidatorArgs(_binder, binding.getComponent(), ctx, binding);
//TODO converter args when we support converter in form
try {
if(_log.isDebugEnabled()){
_log.debug("doSaveFormBinding:binding.save() comp=[%s],binding=[%s],command=[%s],evt=[%s],notifys=[%s]",comp,binding,command,evt,notifys);
}
doPrePhase(Phase.SAVE_BINDING, ctx);
binding.save(ctx);
} finally {
doPostPhase(Phase.SAVE_BINDING, ctx);
}
final Set xnotifys = getNotifys(ctx);
if (xnotifys != null) {
notifys.addAll(xnotifys);
}
}
//generic operation to load a property binding
private void doLoadBinding(Component comp, LoadFormBinding binding, String command) {
final BindContext ctx = BindContextUtil.newBindContext(_binder, binding, false, command, binding.getComponent(), null);
if(binding instanceof InitFormBindingImpl){
ctx.setAttribute(BinderImpl.IGNORE_TRACKER, Boolean.TRUE);//ignore tracker when doing el , we don't need to track the init
}
//TODO converter args when we support converter in form
try {
if(_log.isDebugEnabled()){
_log.debug("doLoadFormBinding:binding.load(),component=[%s],binding=[%s],context=[%s],command=[%s]",comp,binding,ctx,command);
}
doPrePhase(Phase.LOAD_BINDING, ctx);
binding.load(ctx);
//if there is a valodator, clear the validation message after load
if(((BinderImpl)binding.getBinder()).hasValidator(binding.getComponent(), binding.getFormId())){
clearValidationMessages(binding.getBinder(),binding.getComponent(),binding.getFormId());
}
} finally {
doPostPhase(Phase.LOAD_BINDING, ctx);
}
}
void doSaveAfter(Component comp, String command, Event evt, Set notifys) {
final List bindings = _saveFormAfterBindings.get(command);
if (bindings != null) {
for (SaveFormBinding binding : bindings) {
doSaveBinding(comp, binding, command, evt, notifys);
}
}
}
void doLoadBefore(Component comp, String command) {
final List bindings = _loadFormBeforeBindings.get(command);
if (bindings != null) {
for (LoadFormBinding binding : bindings) {
doLoadBinding(comp, binding, command);
}
}
}
void doLoadAfter(Component comp, String command) {
final List bindings = _loadFormAfterBindings.get(command);
if (bindings != null) {
for (LoadFormBinding binding : bindings) {
doLoadBinding(comp, binding, command);
}
}
}
void removeBindings(BindingKey bkey,Set removed) {
List extends Binding> bindingx;
if((bindingx = _initFormBindings.remove(bkey)) !=null){
removed.addAll(bindingx); //comp+_fieldExpr -> bindings (load _prompt)
}
if((bindingx = _loadFormPromptBindings.remove(bkey)) !=null){
removed.addAll(bindingx); //comp+formid -> bindings (load form _prompt)
}
}
void removeBindings(Collection removes) {
for(List bindings:_loadFormAfterBindings.values()){
bindings.removeAll(removes); //command -> bindings (load form after command)
}
for(List bindings:_saveFormAfterBindings.values()){
bindings.removeAll(removes); //command -> bindings (save form after command)
}
for(List bindings:_loadFormBeforeBindings.values()){
bindings.removeAll(removes); //command -> bindings (load form before command)
}
for(List bindings:_saveFormBeforeBindings.values()){
bindings.removeAll(removes); //command -> bindings (save form before command)
}
}
void doLoad(Component comp, BindingKey bkey) {
final List formBindings = _loadFormPromptBindings.get(bkey);
if (formBindings != null) {
for (LoadFormBinding binding : formBindings) {
doLoadBinding(comp,binding,null);
}
}
}
void doInit(Component comp,BindingKey bkey) {
final List initBindings = _initFormBindings.get(bkey);
if (initBindings != null) {
for (InitFormBinding binding : initBindings) {
doLoadBinding(comp, binding,null);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy