2012-04-13 82 views
5

有沒有辦法獲得編輯器正在編輯的代理?GWT編輯器框架

正常的工作流程是:

public class Class implments Editor<Proxy>{ 
    @Path("") 
    @UiField AntoherClass subeditor; 


    void someMethod(){ 
    Proxy proxy = request.create(Proxy.class); 
    driver.save(proxy); 
    driver.edit(proxy,request); 
} 
} 

現在,如果我得到了相同的代理服務器的副主編

public class AntoherClass implements Editor<Proxy>{ 
    someMethod(){ 
    // method to get the editing proxy ? 
    } 
} 

是的,我知道我可以只設置代理與setProxy兒童編輯器( )創建後,但我想知道是否有像HasRequestContext但編輯的代理。

在非UI對象中使用ListEditor時有用。

謝謝。

回答

7

有兩種方法可以獲取給定編輯器正在處理的對象的引用。首先,一些簡單的數據和簡單編輯:

public class MyModel { 
    //sub properties... 

} 

public class MyModelEditor implements Editor<MyModel> { 
    // subproperty editors... 

} 

第一:無需實現Editor的,我們可以挑選也延伸編輯器,但允許子編輯器的另一個接口(LeafValueEditor不允許亞編輯)。讓我們試着ValueAwareEditor

public class MyModelEditor2 implements ValueAwareEditor<MyModel> { 
    // subproperty editors... 

    // ValueAwareEditor methods: 
    public void setValue(MyModel value) { 
    // This will be called automatically with the current value when 
    // driver.edit is called. 
    } 
    public void flush() { 
    // If you were going to make any changes, do them here, this is called 
    // when the driver flushes. 
    } 
    public void onPropertyChange(String... paths) { 
    // Probably not needed in your case, but allows for some notification 
    // when subproperties are changed - mostly used by RequestFactory so far. 
    } 
    public void setDelegate(EditorDelegate<MyModel> delegate) { 
    // grants access to the delegate, so the property change events can 
    // be requested, among other things. Probably not needed either. 
    } 
} 

這需要你實現的各種方法,如上面的例子,但主要一個你感興趣的將是setValue。你不需要自己調用這些,他們將被司機及其代表調用。如果您打算更改對象,那麼flush方法也很好用 - 在刷新之前進行這些更改意味着您要在預期的驅動程序生命週期之外修改對象 - 而不是世界末日,但可能會在稍後出現意外。

二:使用SimpleEditor副主編:

public class MyModelEditor2 implements ValueAwareEditor<MyModel> { 
    // subproperty editors... 

    // one extra sub-property: 
    @Path("")//bound to the MyModel itself 
    SimpleEditor self = SimpleEditor.of(); 

    //... 
} 

利用這一點,你可以調用self.getValue()讀出電流值是什麼。

編輯:看着你已經實現了AnotherEditor,它看起來像你已經開始做類似的GWT SimpleEditor類,儘管你可能在想其他子編輯器,以及:如果我

現在得到了相同的代理服務器的副主編

public class AntoherClass implements Editor<Proxy>{ 
    someMethod(){ 
    // method to get the editing proxy ? 
    } 
} 

這個副主編可以實現ValueAwareEditor<Proxy>代替Editor<Proxy>,並保證其setValue米編輯開始時,將使用Proxy實例調用ethod。

+0

是的它的工作原理,我只需要實現ValueAwareEditor 和setValue自動設置代理。從API「編輯器的行爲會根據所編輯的值進行更改,以實現此界面。」那是我的情況。 =) – 2012-04-14 00:12:24

+0

感謝Colin的幫助(特別是@Path(「」):我被誘惑做了@Path(「this」),但那需要特殊的處理......) 現在我想知道如何根據數據值從編輯器切換到另一個編輯器。我有一個選擇框應該改變形式(許多常見的領域,但佈局和一些領域appaer /消失)。我爲每種類型都有一個UiBinder,我想在用戶選擇時從一個切換到另一個。我不喜歡根據情況創建它們並使其可見或不可見的想法。我想創建一個新的編輯器並對其進行歸類 – 2014-06-12 10:09:21

2

在你的孩子編輯器類中,你可以實現另一個接口TakesValue,你可以在setValue方法中獲得編輯代理。

ValueAwareEditor的工作原理也是如此,但擁有所有您不需要的額外方法。