Home‎ > ‎UI‎ > ‎

LeafValueEditor


LeafValueEditor<Boolean> 

Here is a LeafValueEditor<Boolean> which looks like the image below. It simple makes true false a yes no question.
The checkboxes on the right are the editor here.
public class BooleanYesNo extends Composite implements LeafValueEditor<Boolean> {

 
interface BooleanYesNoUiBinder extends UiBinder<Widget, BooleanYesNo> {}
 
private static BooleanYesNoUiBinder uiBinder = GWT.create(BooleanYesNoUiBinder.class);
 
@UiField CheckBox yes;
 
@UiField CheckBox no;

 
public BooleanYesNo() {
    initWidget
(uiBinder.createAndBindUi(this));
 
}

 
@Override
 
public void setValue(Boolean value) {
   
if (value == null) {
      yes
.setValue(false);
     
no.setValue(false);
     
   
} else if (value == true) {
      yes
.setValue(true);
     
no.setValue(false);
     
   
} else if (value == false) {
      yes
.setValue(false);
     
no.setValue(true);
   
}
 
}

 
@Override
 
public Boolean getValue() {
   
Boolean b = null;
   
if (yes.getValue() == false && no.getValue() == false)  {
      b
= null;
   
} else if (yes.getValue() == true) {
      b
= true;
   
} else if (no.getValue() == true) {
      b
= false;
   
}
   
return b;
 
}

 
@UiHandler("yes")
 
void onYesClick(ClickEvent event) {
    presenter
.setRunning(true);
   
if (yes.getValue() == true) {
     
no.setValue(false);
   
}
 
}
 
 
@UiHandler("no")
 
void onNoClick(ClickEvent event) {
    presenter
.setRunning(true);
   
if (no.getValue() == true) {
      yes
.setValue(false);
   
}
 
}
 
}


LeafValueEditor<Long>

I use this editor to render a drop down box to select a school. I think I could do this better, but in this form it works just fine. On the next version I plan on using key provider.

DropDown Editor
public class SchoolDropDown extends Composite implements LeafValueEditor<Long>, ValueAwareEditor<Long> {

 
interface SchoolDropDownUiBinder extends UiBinder<Widget, SchoolDropDown> {}
 
private static SchoolDropDownUiBinder uiBinder = GWT.create(SchoolDropDownUiBinder.class);
 
@UiField(provided = true)
 
ValueListBox<Long> schoolId = new ValueListBox<Long>(new com.google.gwt.text.shared.Renderer<Long>() {
   
public String render(Long object) {
     
return getSchool(object);
   
}
   
public void render(Long object, Appendable appendable) throws IOException {
     
String s = render(object);
      appendable
.append(s);
   
}
 
});

 
private ClientFactory clientFactory;
 
private Presenter presenter;
 
private List<SchoolJdoProxy> schools;
 
private Long schoolIdLong;

 
public SchoolDropDown() {
    initWidget
(uiBinder.createAndBindUi(this));
   
    schoolId
.addValueChangeHandler(new ValueChangeHandler<Long>() {
     
public void onValueChange(ValueChangeEvent<Long> event) {
        presenter
.setRunning(true);
     
}
   
});
 
}
 
 
public void setClientFactory(ClientFactory clientFactory) {
   
this.clientFactory = clientFactory;
    loadSchools
();
 
}
 
 
public void setPresenter(Presenter presenter) {
   
this.presenter = presenter;
 
}

 
@Override
 
public void setValue(Long value) {
   
this.schoolIdLong = value;
    schoolId
.setValue(schoolIdLong);
 
}

 
@Override
 
public Long getValue() {
   
Long selected = schoolId.getValue();
   
return selected;
 
}

 
@Override
 
public void setDelegate(EditorDelegate<Long> delegate) {
 
}

 
@Override
 
public void flush() {
 
}

 
@Override
 
public void onPropertyChange(String... paths) {
 
}
 
 
private void loadSchools() {
   
SchoolJdoRequest req = clientFactory.getRequestFactory().getSchoolJdoRequest();
   
SchoolJdoFilterProxy filter = req.create(SchoolJdoFilterProxy.class);
    filter
.setLimit(100);
    filter
.setAccountId(clientFactory.getAccountId());
    req
.findSchoolList(filter).fire(new Receiver<List<SchoolJdoProxy>>() {
     
public void onSuccess(List<SchoolJdoProxy> schoolJdoProxyList) {
        processSchool
(schoolJdoProxyList);
     
}
   
});
 
}

 
private void processSchool(List<SchoolJdoProxy> schoolJdoProxyList) {
   
this.schools = schoolJdoProxyList;
   
if (schoolJdoProxyList == null || schoolJdoProxyList.isEmpty()) {
     
return;
   
}
   
List<Long> longList = new ArrayList<Long>();
   
for (SchoolJdoProxy schoolJdoProxy : schoolJdoProxyList) {
      longList
.add(schoolJdoProxy.getIdLong());
   
}
    schoolId
.setAcceptableValues(longList);
    schoolId
.setValue(schoolIdLong);
 
}
 
 
private String getSchool(Long schoolId) {
   
if (schoolId == null || schoolId == 0 || schools == null || schools.size() ==0) {
     
return "";
   
}
   
String s = "";
   
for (SchoolJdoProxy school : schools) {
     
if (school.getIdLong() != null && school.getIdLong() == schoolId) {
        s
= school.getName();
     
}
   
}
   
return s;
 
}
}



Comments