2012-05-22 10 views
8

私はgwt-platformを使用していて、GWTのエディタフレームワークを実装しようとしました。しかし、私は発表者の中からそれを働かせることはできません。私は成功せず、これを試してみました瞬間があり、私はプレゼンターに何とかEditorDriverを注入しなければならないと言うウェブの周りのいくつかの答えは、ですが、私はこれを行う方法がわからない...gwt-editorでGWTのエディタフレームワークを使用するには?

public class MyPresenter extends Presenter<MyPresenter.MyView, MyPresenter.MyProxy> implements MyUiHandlers { 
    public interface MyView extends View, HasUiHandlers<MyUiHandlers>, Editor<MyModel> {} 

    @ProxyStandard 
    @NameToken(NameTokens.myPage) 
    @NoGatekeeper 
    public interface MyProxy extends ProxyPlace<MyPresenter> {} 

    interface Driver extends SimpleBeanEditorDriver<MyModel, MyView> {} 
    private Driver editorDriver; 
    DispatchAsync dispatcher; 

    @Inject 
    public MyPresenter(EventBus eventBus, MyView view, MyProxy proxy, DispatchAsync dispatcher) { 
     super(eventBus, view, proxy); 
     getView().setUiHandlers(this); 
     this.dispatcher = dispatcher; 

     MyModel m = new MyModel(); 
     m.setId(1L); 
     m.setUsername("username"); 
     m.setPassword("password"); 

     editorDriver = GWT.create(Driver.class); 
     editorDriver.initialize(this.getView()); 
     editorDriver.edit(m); 
    } 

    ... 
} 

私は明示的にViewImplementationを指定した場合、それは動作しますが、それはMVPが動作するはずな方法ではありません。

interface Driver extends SimpleBeanEditorDriver<MyModel, MyViewImpl> {} 

... 

editorDriver.initialize((MyViewImpl) this.getView()); 

誰かが私に右のそれを行う方法の例を与えることができれば、私はいいだろう。

おかげ

答えて

7

Expenses sampleの以前のバージョンで使用されたものと同様のアプローチ私の仕事:

インタフェースビューを実装する必要があります。

public class MyPresenter extends Presenter<MyPresenter.MyView, MyPresenter.MyProxy> implements MyUiHandlers { 
    public interface MyView extends BeanEditView<MyModel>, HasUiHandlers<MyUiHandlers> {} 

    @ProxyStandard 
    @NameToken(NameTokens.myPage) 
    @NoGatekeeper 
    public interface MyProxy extends ProxyPlace<MyPresenter> {} 

    private SimpleBeanEditorDriver<MyModel, ?> editorDriver; 
    DispatchAsync dispatcher; 

    @Inject 
    public MyPresenter(EventBus eventBus, MyView view, MyProxy proxy, DispatchAsync dispatcher) { 
     super(eventBus, view, proxy); 
     getView().setUiHandlers(this); 
     this.dispatcher = dispatcher; 

     MyModel m = new MyModel(); 
     m.setId(1L); 
     m.setUsername("username"); 
     m.setPassword("password"); 

     editorDriver = getView().createEditorDriver(); 
    } 

    ... 
} 

とビューimplmementation:

public class MyViewImpl extends ViewWithUiHandlers<MyUiHandlers> implements 
    MyPresenter.MyView { 

    public interface Binder extends UiBinder<Widget, MyViewImpl> { } 
    private static Binder uiBinder = GWT.create(Binder.class); 

    /** 
    * The driver to link the proxy bean with the view. 
    */ 
    public interface EditorDriver extends SimpleBeanEditorDriver<MyModel, MyViewImpl> { } 

    private final Widget widget; 

    public MyViewImpl() { 
    widget = uiBinder.createAndBindUi(this); 
    } 

    @Override 
    public SimpleBeanEditorDriver<MyModel, ?> createEditorDriver() { 
    EditorDriver driver = GWT.create(EditorDriver.class); 
    driver.initialize(this); 
    return driver; 
    } 

    @Override 
    public Widget asWidget() { 
    return widget; 
    } 

    ... 
} 

import com.google.gwt.editor.client.Editor; 
import com.gwtplatform.mvp.client.View; 

/** 
* Implemented by views that edit beans. 
* 
* @param <B> the type of the bean 
*/ 
public interface BeanEditView<B> extends View, Editor<B> { 

    /** 
    * @return a new {@link SimpleBeanEditorDriver} initialized to run 
    *   this editor 
    */ 
    SimpleBeanEditorDriver<B, ?> createEditorDriver(); 
} 

あなたのプレゼンターは、次のようになります。プレゼンターは知っている具体的なビューの実装を必要としないように、ワイルドカードが使用されています

これは、GWTのエディタフレームワークでMVPに近づくほど近いです。私はビューの実装がモデルを知らないための方法を見つけることができませんでしたが、それは本当に必要とは思わない。

これについて改善を加えてもらえれば、嬉しいです。


GWTエディターに関する追加のコメントがあります。モデルを完全に分離することは不可能かもしれないようです。トーマスBroyerは別の編集者の質問にhis answerにそれを置くよう:

MVPは石で設定されていない」(それも定義されていない。それはMartin Fowler氏によって鋳造されたが、彼はさらに2つの特定のパターンを支持して用語を引退しました)別の言い方をすれば、エディタフレームワーク全体がMVPに違反していると見なすことができます。エディタはモデルを知っています(ValueAwareEditorやLeafValueのように)少なくともオブジェクトの種類は編集者だ」と語った。

+0

にドライバの参照を渡すために使用することができますありがとうございました:)たぶん、あなたは正しいとビューがモデルを知っている場合、それは悪くはありません、 "私はViewInterfaceで多くのセッターとゲッターを設定しなければなりません。これは、Viewがそのモデルも知っていることを意味します。 –

+0

これはすばらしい解決策です。ありがとうございました! – confile

+0

ありがとうございます。編集者とGWTPとの2日間の戦いの後、ついにそれを効かせます。あなたがこれについていくつかの改善を行ったなら、私に知らせてください。 – masterdany88

0

MVPでは、プレゼンターを使用してモデルをビューから完全に分離すると言います。問題はDriver.classが

editorDriver = GWT.create(Driver.class); 

GWT.create

に渡されるということです

2

);また、私は別の解決策があることを願ってい ...あなたのアプローチは、ビュー内のロジックを置くことを言うだろうすべてのサブエディタを保持する具象クラス、つまりすべてのuibindedウィジェットでなければなりません。

一つの解決策は、以下の通りである:

ビュー・インターフェースは、モデルオブジェクト

public interface MyView extends View, ..., Editor<MyModel> 

のエディタインターフェースを拡張ビュー実装MyViewImplドライバは、ドライバタイプ

interface MyDriverImpl extends SimpleBeanEditorDriver<MyModel,MyViewImpl> 

を定義しますMyViewImplでインスタンス化:

SimpleBeanEditorDriver<MyModel,MyView> driver = GWT.create(MyDriverImpl.class); 

親タイプ

SimpleBeanEditorDriver<MyModel,MyView> 

がプレゼンター

関連する問題