2013-02-08 5 views
0

하위 편집기가있는 편집기의 예를 실행하려고합니다. 부모를 비울 때 자식 편집기의 값이 null입니다. 클래스는 개인 및 주소입니다. 주요 편집기이다GWT 편집기 및 하위 편집기

// editor fields 
public TextField firstname; 
public TextField lastname; 
public NumberField<Integer> id; 

public AddressEditor address = new AddressEditor(); 

public PersonEditor(Person p){ 
    asWidget(); 
} 

@Override 
public Widget asWidget() { 
    setWidth(400); 
    setBodyStyle("padding: 5px;"); 
    setHeaderVisible(false); 

    VerticalLayoutContainer c = new VerticalLayoutContainer(); 

    id = new NumberField<Integer>(new IntegerPropertyEditor()); 
    // id.setName("id"); 
    id.setFormat(NumberFormat.getFormat("0.00")); 
    id.setAllowNegative(false); 
    c.add(new FieldLabel(id, "id"), new VerticalLayoutData(1, -1)); 

    firstname = new TextField(); 
    // firstname.setName("firstname"); 
    c.add(new FieldLabel(firstname, "firstname"), new VerticalLayoutData(1, -1)); 

    lastname = new TextField(); 
    lastname.setName("lastname"); 
    c.add(new FieldLabel(lastname, "lastname"), new VerticalLayoutData(1, -1)); 

    c.add(address); 
    add(c); 
    return this; 

서브 편집기 드라이버 생성

public class AddressEditor extends Composite implements Editor<Address> { 

private AddressProperties props = GWT.create(AddressProperties.class); 
private ListStore<Address> store = new ListStore<Address>(props.key()); 
ComboBox<Address> address; 

public AddressEditor() { 
    for(int i = 0; i < 5; i ++) 
     store.add(new Address("city" + i)); 
    address = new ComboBox<Address>(store, props.nameLabel()); 

    address.setAllowBlank(false); 
    address.setForceSelection(true); 
    address.setTriggerAction(TriggerAction.ALL); 
    initWidget(address); 
} 

그리고 이것이 :

private HorizontalPanel hp; 

private Person googleContact; 
PersonDriver driver = GWT.create(PersonDriver.class); 

public void onModuleLoad() { 

    hp = new HorizontalPanel(); 
    hp.setSpacing(10); 

    googleContact = new Person(); 
    PersonEditor pe = new PersonEditor(googleContact); 

    driver.initialize(pe); 
    driver.edit(googleContact); 

    TextButton save = new TextButton("Save"); 
    save.addSelectHandler(new SelectHandler() { 

     @Override 
     public void onSelect(SelectEvent event) { 
      googleContact = driver.flush(); 
      System.out.println(googleContact.getFirstname() + ", " + googleContact.getAddress().getCity()); 
      if (driver.hasErrors()) { 
       new MessageBox("Please correct the errors before saving.").show(); 
       return; 
      } 
     } 
    }); 

googleContact.getFirstname 값 ()가 채워 지지만 googleContact.getAddress()는 항상 null입니다. 내가 누락 된 부분이 있습니까?

답변

1

AddressEditor 필요는 Address 모델에 매핑합니다 - 현재, Address는 하나의 게터/세터를하지 않는 한, 정말 많은 이해하지 것이다 getAddress()setAddress(Address)을,라고하지 않는 것.

ComboBox<Address> (이미 Editor<Address>을 구현)을 원하면 해당 콤보를 PersonEditor에 직접 입력하는 것이 좋습니다. 그렇지 않으면 입력란에 @Path("")을 추가하여 하위 속성 (예 : person.getAddress().getAddress())이 아닌 값 자체를 직접 수정해야 함을 나타냅니다.

주소 편집기를 구현하는 또 다른 방법은 Address 유형의 각 속성을 AddressEditor에 나열하는 것입니다. 이것은 드라이버가 기본적으로 기대하는 것이므로 '주소'라는 필드가 보일 때 혼란 스럽습니다.

코드 자체에 대한 두 가지 간단한 생각 : 드라이버 자체의 작업 인 사람을 PersonEditor에 전달할 필요가 없습니다. 둘째, 편집기 필드는 public 일 필요는 없으며 private 일 수 없습니다.

+0

답변 해 주셔서 감사합니다. 나는 다소 부작용을 설정하는 방법을 혼란스러워했다. @Path ("") ComboBox를 추가하려고했습니다.

주소; 그러나 그것은 작동하지 않았다. 나는 모든 것을 한 반에 넣었고 위대한 일을했다. 다시 한 번 감사드립니다! – Berna

+0

@Berna, 이것이 유효한 대답이라고 생각되면 비어있는 체크 표시를 클릭하여 동의해야합니다. – Jonathan