2011-02-07 87 views
1

我是一个尝试使用MenuBar在DeckPanel中交换显示的面板的新手。GWT:从MenuBar获取对DockLayoutPanel的引用

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

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

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

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

此外,由于我是一个新手,欢迎任何对此代码的建议或评论。我在Google上尽了最大的努力,但我所寻找的许多东西似乎并没有出现在那里。

(这是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); 

     } 
    }); 
} 

}

鸭plicationMenu.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> 
+0

我写了一篇关于在GWT中使用MVP的长篇回答,并且使用其中的一些想法可以帮助你实现你想要做的事情。 http://stackoverflow.com/questions/2832779/is-there-a-recommended-way-to-use-the-observer-pattern-in-mvp-using-gwt/2832919#2832919 – 2011-02-07 21:05:09

回答

2

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