GWT:从MenuBar获取对DockLayoutPanel的引用

时间:2011-02-07 19:21:22

标签: gwt

我是一个新手试图使用MenuBar来交换DeckPanel中显示的面板。

我有2个类和2个相关的uibinder XML文件:

  • ApplicationUi.java
  • ApplicationUi.ui.xml
  • ApplicationMenu.java
  • ApplicationMenu.ui.xml

在ApplicationUi.java和UI XML中,根绑定到DockLayoutPanel。 ApplicationMenu应位于DockLayoutPanel的North部分。 MenuBar选项将影响Center部分中的DeckPanel。

在ApplicationMenu中,如何获取对DeckPanel的引用,以便我可以调用showWidget()来交换显示的面板?

此外,由于我是新手,欢迎对此代码提出任何建议或评论。我已经在谷歌上做到了最好,但很多我正在寻找的东西似乎并不存在。

(这是Replace GWT DockLayoutPanel Contents)的后续内容。

来源: ApplicationUi.java

import org.jason.datacenter.client.forms.NewRequirementForm;

public class ApplicationUi extends Composite {

private static final Binder binder = GWT.create(Binder.class);

interface Binder extends UiBinder<Widget, ApplicationUi> {
}

@UiField DockLayoutPanel dlp;
@UiField VerticalSplitPanel headerPanel;
@UiField DeckPanel deckPanel;

public ApplicationUi() {
    initWidget(binder.createAndBindUi(this));

    // add the NewRequirementForm to the deckpanel as index #0
    deckPanel.add(new NewRequirementForm());
}

public void switchDeck(int newIndex) {
    deckPanel.showWidget(newIndex);
}
}

ApplicationUi.ui.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'  xmlns:g='urn:import:com.google.gwt.user.client.ui'>
<ui:style>
    .panel {
        background-color: ivory;
    }
</ui:style>


<g:DockLayoutPanel ui:field="dlp">
    <g:north size="800">
        <g:VerticalSplitPanel ui:field="headerPanel">

        </g:VerticalSplitPanel>
    </g:north>
    <g:center>
        <g:DeckPanel ui:field="deckPanel" />
    </g:center>
</g:DockLayoutPanel>

</ui:UiBinder>

ApplicationMenu.java:

public class ApplicationMenu extends Composite {

private static final Binder binder = GWT.create(Binder.class);

interface Binder extends UiBinder<Widget, ApplicationMenu> {
}

@UiField MenuBar applicationMenu;
@UiField MenuItem mitmNewPower;

public ApplicationMenu() {
    initWidget(binder.createAndBindUi(this));

    mitmNewPower.setCommand(new Command() {

        @Override
        public void execute() {
            RootLayoutPanel rlp = RootLayoutPanel.get();
            DockLayoutPanel dlp = (DockLayoutPanel) rlp.getWidget(0);

        }
    });
}

}

ApplicationMenu.ui.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder' xmlns:g='urn:import:com.google.gwt.user.client.ui'>
<ui:style>
    .panel {
        background-color: ivory;
    }
</ui:style>

<g:MenuBar ui:field="applicationMenu">
    <g:MenuItem>
        Process
        <g:MenuBar>
            <g:MenuItem ui:field="mitmNewPower" />
        </g:MenuBar>
    </g:MenuItem>

</g:MenuBar>

</ui:UiBinder>

1 个答案:

答案 0 :(得分:2)

你可以这样做的一种方法是使用EventBus。创建一个事件类型,并在单击一个菜单项时让ApplicationMenu触发该类型的事件。 ApplicationUi对象可以订阅该事件,并通过更新DeckPanel的内容来响应它。这样可以避免菜单对象需要了解DeckPanel