如何在GWT或GWTP中检索当前Presenter?

时间:2013-10-25 12:09:57

标签: gwt gwtp

这是我的问题。我的标题演示者中有很多链接。

Link1 - Link2 - Link3 - GuideDialogLink

当用户点击Link1或Link2或Link3时,它将打开一个新的浏览器。

但是,当用户点击GuideDialogLink时,它会在当前链接的顶部弹出一个DialogBox。

GuideDialog的设计方式是根据用户所在的链接显示不同的Gui。它的代码是这样的:

 class GuideDialog extends DialogBox{
    public GuideDialog(int whichLink){
       if(whichLink==0){
           //show Gui 1 
       }
       else if(whichLink==2){
           //show Gui 2 
       }
       else if(whichLink==3){
           //show Gui 3 
       }
    }
 }

例如,当用户留在Link3&如果他们单击GuideDialogLink,它将显示一个DialogBox,如果用户停留在Link1或Link2上,Gui3与Gui1或Gui2不同。

所以,我的问题是:

有没有办法检索当前演示者?

如果我们知道哪个Presenter是当前的,那么我们可以相应地调用GuideDialog ex:GuideDialog myGD=new GuideDialog(0);

- 其他解决方案是在public static int currentLink课程中存储Utility。当用户点击link1或link2或link3时,我们可以使用EventBus设置public static currentLink来相应地设置&最终GuideDialog myGD=new GuideDialog(Utility.currentLink);

但我不认为这个解决方案很优雅,因为Google必须有一些功能让我们知道我们当前所在的页面。

那么我怎样才能优雅地解决我的问题?

修改

好的,让我澄清一下。我正在使用GWTP创建我的webapp。

-1st,我使用eclipse生成HeaderPresenter。 eclipse将创建(HeaderPresenter.java,HeaderView.java和HeaderView.ui.xml)。 HeaderPresenter有4个超链接:link1,link2,link3,GuideDialogLink。

- 2,我创建了Link1Presenter(Link1Presenter.java,Link1View.java,Link1View.ui.xml)。然后我创建了Link2Presenter(Link2Presenter.java,Link2View.java,Link2View.ui.xml)。然后我创建了Link3Presenter(Link3Presenter.java,Link3View.java,Link3View.ui.xml)

-3rd,我使用setInSlot使Link1Presenter,Link2Presenter,Link3Presenter嵌入或嵌套在HeaderPresenter中。这意味着当用户转到link1(例如:abc.com#link1)时,他们将看到4个超链接(link1,link2,link3,GuideDialogLink)。如果他们转到link2(例如:abc.com#link2)或link3,他们也会看到4个超链接。

-4,我创建了GuideDialog.java扩展DialogBox&在HeaderPresenter中,我有guideDialogLink.addClickHandler(new ClickHandler(){ GuideDialog myGD=new GuideDialog (int whichLink); });

因此,当用户处于link1,2或3时,他们会看到GuideDialogLink吗? &安培;当他们点击GuideDialog时,会弹出一个DialogBox吗?

现在我的要求是当用户在Link1&如果他们单击GuideDialogLink,将弹出一个对话框&显示一个Gui(上面有两个文本框)。当用户在Link2&如果他们单击GuideDialogLink,将弹出一个对话框&展示另一个Gui(上面有2个标签)。当用户在Link3&如果他们单击GuideDialogLink,将弹出一个对话框&显示另一个不同的Gui(上面有2个复选框)

3 个答案:

答案 0 :(得分:0)

考虑我们有这样的事情:

一般观点

// defines what you can access from a View
interface GeneralDisplay extends Display {
}

// implements a View
class abstract GeneralView implements GeneralDisplay {

    // **Edit**: here you code the corresponding GUI
    public abstract Composite getDialogGUI();
}

关联特定观点

// additional, if you need specific behavior in your linkX view
interface LinkDisplay1 extends GeneralDisplay {
}

// if all views are the same there's no need to implement LinkDisplayX
class LinkView1 extends GeneralView implements LinkDisplay1 {

    @Override
    public Composite getDialogGUI () {
        // **Edit** here you code the DialogGUI
    }
}

// other LinkViews ...

<强>主持人

// a more generic presenter for all Links
class GeneralPresenter<D extends GeneralDisplay> extends DefaultPresenter<D> {
    // some code here ...

    // constructor, here you associate the corresponding display
    public GeneralPresenter (D display, EventBus eventBus, DispatchAsync dispatch) {
        super(display,eventBus,dispatch);  
        // some code here ...
    }

    public void onBind () {
        registerHandler(display.getDialogBoxLink().addClickHandler ( new ClickHandler () {
             doShowDialogGUI();
        }));
    }

    public void doShowDialogGUI () {
        // here you show the dialog box, you can access the corresponding view by:          
        // this.display
        // **Edit** add some code here
        display.getDialogBox().setWidget(display.getDialogGUI());

    }

}

// Then you have the follogiwng
class LinkPresenter1<D extends GeneralDisplay> extends GeneralPresenter<D> {
    // some code here ...

    @Override 
    public void onBind () {
        super.onBind(); // here is where the "real stuff" is binded
    }
}

有关详细信息,请阅读GWT-MVP (Model-View-Presenter, a variant from MVC) architecture,这是一个很好的信息,向您展示MVP架构的工作原理。

* 编辑:* 我添加了一些额外的代码片段来检索相应的DialogGUIs视图。

答案 1 :(得分:0)

我不明白你的问题:

当您点击链接时,您的地点会发生变化(PlaceTokenizer,控制器等等)。所以信息在PlaceController(当前位置url)中。

你可以从那里获取信息。如果你有一个ClientFactory,你可以从中获取PlaceController并询问哪个是当前的Place对象。

例如:

public P getCurrentPlace() {
    return (P) commonClientFactory.getPlaceController().getWhere();
}

...

注意: 该信息也在ActivityManager中(我不建议在您的情况下使用,因为您对更大的范围(地点)感兴趣,而不是:当前活动)

答案 2 :(得分:0)

我找到了答案,非常简单。

1,打开新链接时,我们可以添加一个类似which链接的参数。前my.com#link1;whichLink=link1

然后在HeaderPresenter中,当调用GuideDialog时,我们这样做

GuideDialogBox gDialogBox=new GuideDialogBox(placeManager.getCurrentPlaceRequest().getParameter("whichLink", "home"));