GWT编辑和副编辑

时间:2013-02-08 16:37:47

标签: gwt gxt gwt-editors

我正在尝试使用子编辑器运行编辑器的示例。 刷新父级时,子编辑器的值为null。 课程是人员和地址。 主编是:

    // 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 个答案:

答案 0 :(得分:1)

AddressEditor需要映射到Address模型 - 目前似乎没有,除非Address只有一个getter / setter,称为getAddress()并且setAddress(Address),这真的没有多大意义。

如果您只想要一个ComboBox<Address>(已经实现Editor<Address>),请考虑直接将该组合放入PersonEditor。否则,您需要将@Path("")添加到AddressEditor.address字段,以表明它应该直接编辑值本身,而不是子属性(即person.getAddress().getAddress())。

构建地址编辑器的另一种方法是列出AddressAddressEditor类型的每个属性。这是默认情况下驱动程序所期望的,因此当它看到一个名为“地址”的字段时会感到困惑。

关于代码本身的两个快速思考:没有必要将一个人传递到PersonEditor - 这就是驱动程序本身的工作。其次,您的编辑器字段不一定是public,它们不能是private