Skip to content

Instantly share code, notes, and snippets.

@jfuerth
Created July 4, 2012 18:32
Show Gist options
  • Save jfuerth/3048793 to your computer and use it in GitHub Desktop.
Save jfuerth/3048793 to your computer and use it in GitHub Desktop.
package org.jboss.errai.databinding.client;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Widget;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jboss.errai.databinding.client.api.InitialState;
import org.jboss.errai.demo.grocery.client.shared.Store;
public class BindableProxyLoaderImpl implements BindableProxyLoader {
public void loadBindableProxies() {
class StoreProxy extends Store implements BindableProxy {
private Map<String, Widget> bindings = new HashMap<String, Widget>();
private Map<String, HandlerRegistration> handlerRegistrations = new HashMap<String, HandlerRegistration>();
private Store target;
private InitialState initialState;
public StoreProxy(InitialState initialState) {
target = new Store();
this.initialState = initialState;
}
public StoreProxy(Store target, InitialState initialState) {
this.target = target;
this.initialState = initialState;
}
public void setTarget(Object target, InitialState initialState) {
this.target = (Store) target;
this.initialState = initialState;
syncInitialState();
}
public Store unwrap() {
return target;
}
public boolean equals(Object obj) {
return target.equals(obj);
}
public int hashCode() {
return target.hashCode();
}
private void syncInitialState() {
for (Object property : bindings.keySet()) {
syncInitialState(bindings.get(property), (String) property);
}
}
private void syncInitialState(final Widget widget, final String property) {
if (initialState != null) {
if (widget instanceof HasValue) {
HasValue hasValue = (HasValue) widget;
Object value = initialState.getInitialValue(get(property), hasValue.getValue());
hasValue.setValue(value);
set(property, value);
} else if (widget instanceof HasText) {
HasText hasText = (HasText) widget;
Object value = initialState.getInitialValue(get(property), hasText.getText());
String stringValue = (String) value;
hasText.setText(stringValue);
set(property, stringValue);
}
}
}
public void bind(final Widget widget, final String property) {
unbind(property);
if (bindings.containsValue(widget)) {
throw new RuntimeException("Widget already bound to a different property!");
}
bindings.put(property, widget);
if (widget instanceof HasValue) {
handlerRegistrations.put(property, ((HasValue) widget).addValueChangeHandler(new ValueChangeHandler() {
public void onValueChange(ValueChangeEvent event) {
StoreProxy.this.set(property, event.getValue());
}
}));
}
syncInitialState(widget, property);
}
public void unbind(String property) {
bindings.remove(property);
HandlerRegistration reg = handlerRegistrations.remove(property);
if (reg != null) {
reg.removeHandler();
}
}
public void unbind() {
for (Object reg : handlerRegistrations.keySet()) {
((HandlerRegistration) handlerRegistrations.get(reg)).removeHandler();
}
bindings.clear();
handlerRegistrations.clear();
}
public List getDepartments() {
return target.getDepartments();
}
public void setDepartments(List departments) {
target.setDepartments(departments);
if (bindings.containsKey("departments")) {
Widget widget = bindings.get("departments");
if (widget instanceof HasValue) {
((HasValue) widget).setValue(Convert.to(((HasValue) widget).getValue().getClass(), departments), true);
} else if (widget instanceof HasText) {
((HasText) widget).setText((String) Convert.to(String.class, departments));
}
}
}
public long getId() {
return target.getId();
}
public String getName() {
return target.getName();
}
public void setName(String name) {
target.setName(name);
if (bindings.containsKey("name")) {
Widget widget = bindings.get("name");
if (widget instanceof HasValue) {
((HasValue) widget).setValue(Convert.to(((HasValue) widget).getValue().getClass(), name), true);
} else if (widget instanceof HasText) {
((HasText) widget).setText((String) Convert.to(String.class, name));
}
}
}
public Object get(String property) {
if (property.equals("departments")) {
return getDepartments();
}
if (property.equals("id")) {
return getId();
}
if (property.equals("name")) {
return getName();
}
return null;
}
public void set(String property, Object value) {
if (property.equals("departments")) {
target.setDepartments((List) Convert.to(List.class, value));
}
if (property.equals("name")) {
target.setName((String) Convert.to(String.class, value));
}
}
}
BindableProxyFactory.addBindableProxy(Store.class, new BindableProxyProvider() {
public BindableProxy<?> getBindableProxy(Object model, InitialState state) {
return new StoreProxy((Store) model, state);
}
public BindableProxy<?> getBindableProxy(InitialState state) {
return new StoreProxy(state);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment