编辑器:自动映射包含对象的id

时间:2013-05-03 08:01:43

标签: gwt

我有一项服务,使用户能够搜索一些具有许多标准的项目。

我有一个代表那些标准的课程:

public class ItemFilter{

  private Integer idCountry;
  private Integer minPrice;
  private Integer maxPrice;
  ...

  //Getters and Setters
}

然后,我使用编辑器编辑此过滤器的属性:

public class ItemFilterEditor extends Composite implements Editor<ItemFilter> {

  ComboBox<Country> country;
  NumberField<Integer> minPrice;
  NumberField<Integer> maxPrixe;
  ...

}

这里的问题是我需要一个ComboBox来让用户选择一个国家,但是ItemFilter类只接受国家的ID。

问题:有没有办法在刷新编辑器时自动设置ItemFilter的idCountry?

我找到的唯一解决方案是创建一个中间类并进行一些映射......

1 个答案:

答案 0 :(得分:1)

让您的编辑器实施ValueAwareEditor并在setValueflush中进行映射。

public class ItemFilterEditor extends Composite implements ValueAwareEditor<ItemFilter> {

  @Editor.Ignore ComboBox<Country> country;
  NumberField<Integer> minPrice;
  NumberField<Integer> maxPrixe;
  ...

  private ItemFilter value;

  @Override
  public void setValue(ItemFilter value) {
    this.value = value;
    // select the item in country with ID equal to value.getIdCountry()
  }

  @Override
  public void flush() {
    this.value.setIdCountry(/* get the ID of the selected country */);
  }
}

或者使用LeafValueEditor<Integer>进行映射:

public class ItemFilterEditor extends Composite implements Editor<ItemFilter> {

  @Editor.Ignore ComboBox<Country> country;
  final LeafValueEditor<Integer> idCountry = new LeafValueEditor<Integer>() {
    @Override
    public void setValue(Integer value) {
      // select the item in country with ID equal to value
    }

    @Override
    public Integer getValue() {
      return /* get the ID of the selected country */);
    }
  };

  NumberField<Integer> minPrice;
  NumberField<Integer> maxPrixe;
  ...

}
相关问题