2013-05-22 23 views
0

我的問題幾乎是相同的,因爲這一個: ListEditor with polymorphic types 托馬斯有一個很好的答案,但它是不夠的我。GWT ListEditor與多態類型 - 無法更新

我想顯示可能不同的對象列表,因此每個編輯器可以以不同的方式顯示。 它幾乎工作,除了我找不到問題:不可能獲得當前值並保存它。 (我認爲setValue方法必須被自動調用,但我有打電話給我自己)

下面是一些代碼:

ShapeListEditor:

public class ShapeListEditor extends VerticalLayoutContainer implements IsEditor<ListEditor<ShapeProxy, ShapeEditorWrapper>> { 
public class ESource extends EditorSource<ShapeEditorWrapper> { 

    @Override 
    public ShapeEditorWrapper create(int index) { 
     ShapeEditorWrapper subEditor = new ShapeEditorWrapper(index); 
     subEditor.setLayoutData(new VerticalLayoutData(1, -1, new Margins(20, 20, 40, 20))); 
     insert(subEditor, index); 
     return subEditor; 
    } 

    @Override 
    public void dispose(ShapeEditorWrapper subEditor) { 
     remove(subEditor); 
    } 

    @Override 
    public void setIndex(ShapeEditorWrapper editor, int index) { 
     insert(editor, index); 
    } 
} 

private ListEditor<ShapeProxy, ShapeEditorWrapper> editor = ListEditor.of(new ESource()); 

@Override 
public ListEditor<ShapeProxy, ShapeEditorWrapper> asEditor() { 
    return editor; 
} 
} 

ShapeEditorWrapper:

public class ShapeEditorWrapper extends Composite implements ValueAwareEditor<ShapeProxy> { 

private SimplePanel panel = new SimplePanel(); 

@Path("") 
ShapeEditor ShapeEditor; 

public ShapeEditorWrapper(int index) { 
    super(); 
    initWidget(panel); 
} 


@Override 
public void setValue(ShapeProxy value) { 
    if (ShapeEditor == null) { 
     if (value == null) { 

     } else if (value.getShapeType() == ShapeTypeEnum.SQUARE) { 
      ShapeEditor = new ShapeSquareEditor(); 
      ShapeEditor.setValue(value); 
      panel.setWidget(ShapeEditor); 
     } else if (value.getShapeType() == ShapeTypeEnum.TRIANGLE) { 
      ShapeEditor = new ShapeTriangleEditor(); 
      panel.setWidget(ShapeEditor); 
     } 
    } 

} 
} 

ShapeEditor:

public class ShapeEditor extends VerticalLayoutContainer implements ValueAwareEditor<ShapeProxy> { 

public ShapeEditor() { 
    super(); 
} 

@Override 
public void setValue(ShapeProxy value) { 
} 

@Override 
public void setDelegate(EditorDelegate<ShapeProxy> delegate) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void flush() { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onPropertyChange(String... paths) { 
    // TODO Auto-generated method stub 

} 

} 

ShapeTriangleEditor和ShapeSquareEditor具有相同的屬性,但我不想擁有相同的顯示。這些類覆蓋setValue(),創建和顯示字段,這些字段以良好的值出現。但是,如果我更改了值,則不會檢測到任何修改。

誰能告訴我我錯過了什麼?

在此先感謝

回答

0

我有類似的問題。作爲一個建議,試試這個:

1.在ShapeEditorWrapper

看來你錯過了設置ShapeTypeEnum.TRIANGLE值。

@Override 
public void setValue(ShapeProxy value) { 
    if (shapeEditor == null) { 
     ... 
     } else if (value.getShapeType() == ShapeTypeEnum.TRIANGLE) { 
      ... 
      ShapeEditor.setValue(value); // ===> It seems you forgot this. 
      ... 
     } 
    } 
} 

沖洗ShapeEditor每當包裝進行沖洗。

@Override 
public void flush() 
{ 
    shapeEditor.flush(); 
} 

2.在ShapeEditor,ShapeTriangleEditor和ShapeSquareEditor

保證不對的setValue()實際更新一個bean,如有必要,初始化任何小組件與bean值。

private ShapeProxy shape; 
@Override 
public void setValue(ShapeProxy value) { 
    this.shape = value 
    // intialize your widget fields here. 
} 

沖洗部件值回豆。編輯器框架應該爲您處理其餘的問題。

@Override 
public void flush() 
{ 
    //Flush your widget values to the proxy 
    shape.setXXX(...); 
    //... 
} 

我希望它爲你工作。它爲我做了!