com.extjs.gxt.ui.client.binding.FieldBinding Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gxt Show documentation
Show all versions of gxt Show documentation
Rich Internet Application Framework for GWT
/*
* Sencha GXT 2.3.1a - Sencha for GWT
* Copyright(c) 2007-2013, Sencha, Inc.
* [email protected]
*
* http://www.sencha.com/products/gxt/license/
*/
package com.extjs.gxt.ui.client.binding;
import com.extjs.gxt.ui.client.data.ChangeEvent;
import com.extjs.gxt.ui.client.data.ChangeEventSource;
import com.extjs.gxt.ui.client.data.ChangeListener;
import com.extjs.gxt.ui.client.data.Model;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.PropertyChangeEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.FieldEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.Record;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.widget.form.Field;
/**
* A two-way binding between a ModelData and Field instance. The binding will be
* 1-way when the bound model does not support change events.
*
* @see ModelData
* @see Field
*/
@SuppressWarnings({"unchecked","rawtypes"})
public class FieldBinding {
protected Field field;
protected ModelData model;
protected String property;
protected Store store;
private Listener changeListener;
private ChangeListener modelListener;
private Converter converter;
private boolean updateOriginalValue = false;
/**
* Creates a new binding instance.
*
* @param field the bound field for the binding
*/
public FieldBinding(Field field, String property) {
this.field = field;
this.property = property;
changeListener = new Listener() {
public void handleEvent(FieldEvent be) {
onFieldChange(be);
}
};
modelListener = new ChangeListener() {
public void modelChanged(ChangeEvent event) {
if (event.getType() == ChangeEventSource.Update) {
onModelChange((PropertyChangeEvent) event);
}
}
};
}
/**
* Binds the model and field. This method also updates the fields original
* value which controls the dirty state of the field.
*
* @param model the model to be bound
*/
public void bind(ModelData model) {
if (this.model != null) {
unbind();
}
this.model = model;
field.addListener(Events.Change, changeListener);
if (model instanceof Model) {
((Model) model).addChangeListener(modelListener);
}
updateField(updateOriginalValue);
}
/**
* Returns the bindings converter.
*
* @return the converter
*/
public Converter getConverter() {
return converter;
}
/**
* Returns the bound field.
*
* @return the field
*/
public Field