2013-05-03 27 views
0

我有一項服務,它使用戶能夠用許多標準搜索某些項目。編輯器:自動映射包含對象的ID

我代表這些指標分析類:

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

讓你的編輯器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; 
    ... 

} 
+0

謝謝:)我編輯揭露使用LeafValueEditor解決的問題。如果有一些可能的改進,請不要猶豫。 – 2013-05-03 10:24:58

+1

當我第一次回覆時,我一直在移動。我只是用我想到的樣本代碼編輯了我的答案。 – 2013-05-03 10:35:44

+0

我刪除了我的解決方案:你看起來更簡潔:) 我仍然有一個關於ValueAwareEditor的問題:你是否應該映射setValue()中的所有值,還是僅僅映射那些不由編輯器處理的值? – 2013-05-03 13:21:27

相關問題