Skip to content

Instantly share code, notes, and snippets.

@pives
Created March 25, 2010 18:23
Show Gist options
  • Save pives/343925 to your computer and use it in GitHub Desktop.
Save pives/343925 to your computer and use it in GitHub Desktop.
/**
* Copyright 2010 Philip Ives, Philadelphia, PA
* You may reproduce, reuse and alter this code provided you keep this entire attribution.
* No warranty expressed or Implied.
* @author Philip Ives
*
* class used to bind longs to text boxes.
*
*/
package com.nemt.mmsweb.client.util;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.TextBoxBase;
import com.google.gwt.user.client.ui.Widget;
public class LongWidgetBinder extends Widget implements HasValue<Long> {
/**
* Copyright 2010 Philip Ives, Philadelphia, PA
* You may reproduce, reuse and alter this code provided you keep this entire attribution.
* No warranty expressed or Implied.
* @author Philip Ives
*
* class used to bind longs to text boxes.
*
* example: to use :
* <code>public class someView extends Composite {
* @UiField TextBox someId;
* private LongWidgetBinder longSomeIdBinder;
*
* public someView(){
* longSomeIdBinder = new LongWidgetBinder(42L,someId);
*
*/
private Long v;
private TextBoxBase w;
private Boolean valueChangeHandlerInitialized = false;
public LongWidgetBinder(Long v, TextBoxBase w) {
super();
this.v=v;this.w=w;
}
public Long getValue() {
return Long.parseLong(this.w.getValue());
}
public void setValue(Long value) {
this.w.setValue(value.toString());
}
public void setValue(Long value, boolean fireEvents) {
this.w.setValue(value.toString(),fireEvents);
}
public HandlerRegistration addChangeHandler(ChangeHandler handler) {
return addDomHandler(handler, ChangeEvent.getType());
}
public HandlerRegistration addValueChangeHandler(
ValueChangeHandler<Long> handler) {
// Initialization code
if (!valueChangeHandlerInitialized) {
valueChangeHandlerInitialized = true;
addChangeHandler(new ChangeHandler() {
public void onChange(ChangeEvent event) {
ValueChangeEvent.fire(LongWidgetBinder.this, getValue());
}
});
}
return addHandler(handler, ValueChangeEvent.getType());
}
@Override
public void fireEvent(GwtEvent<?> event) {
super.fireEvent(event);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment