Reputation: 819
I have a bit of a problem. Like in the code I'm trying to create a dynamic MenuItem List. Which is no problem, but I want to have the item which is selected/clicked. But I'm not sure how to correctly implement that. all of this is happening in my View and i want to set the value to continue in the Presenter. thx for any help.
chooseProfileMenuBar = new MenuBar(true);
for (int i = 0; i<names().size(); i++) {
final MenuItem menuItemChoose = new MenuItem(names.get(i), new Command(){
@Override
public void execute() {
// TODO set the onClicked value/name at index i
}
});
chooseProfileMenuBar.addItem(menuItemChoose);
}
return chooseProfileMenuBar;
Upvotes: 2
Views: 713
Reputation: 122008
Use the benefits of UIHandler
Concept to Communicate between the view and presenter.
Create an interface:
import com.gwtplatform.mvp.client.UiHandlers;
public interface MyUiHandler extends UiHandlers {
void onMenuSelected(MenuItem menuitem);
}
Then in your view
public class MyMVPView extends ViewWithUiHandlers<MyUiHandler> implements
MyMvPPresenter.MyView {
.
.
.
.
And implement that uihandler interface with Presenter
public class MyMvPPresenter
extends
Presenter<MyMVPPresenter.MyView, MyMVPPresenter.MyProxy>
implements MyUiHandler {
//in the same presenter
public interface MyView extends View, HasUiHandlers<UserDashboardUiHandler> {
//
.
.
.
Then you will get the override method in presenter like
@Override
public voidonMenuSelected(MenuItem menuitem) {
//Your implementation here
}
Usage
@Override
public void execute() {
getUiHandlers().showReportPanel(menuItemChoose );
}
Upvotes: 1
Reputation: 1565
you can do one more thing if possible, i have download menu that creates runtime so i have add HashMap and in that HashMap added names.get(i) and download link so it looks like
map.put(names.get(i),"link"),
now in execute(), i added Window.open(map.get(menuItem.gettext()),"_blank");
one more menu i have in my project that is report menu so from database report list is coming and
for(final Temp report : listReports){
MenuItem menuItem = new MenuItem(report.getType().toString(), new Command() {
@Override
public void execute() {
getUiHandlers().showReportPanel(report);
}
});
reportMenuBar.addItem(menuItem);
}
Upvotes: 2
Reputation: 1565
you are right vicR, it correct to get the value from the menuItemChoose. you can do like
public void execute() {
onclickMenu(menuItemChoose);
}
void onclickMenu(MenuItem menuItemChoose){
if(menuItemChoose.gettext().equals("abc"){
//call handler method that implemented you in presenter
}
}
Upvotes: 0