Hi Eric,
The MVP pattern typically breaks a panel down into a view and a presenter where an interface defines the boundary between the two classes. For example, the following presenter has a simple view containing a text widget and a button:
- Code: Select all
public class GreetingPresenter extends WidgetPresenter {
public interface Display extends WidgetDisplay {
public HasValue getName();
public HasClickHandlers getSend();
}
@Inject
public GreetingPresenter(final Display display,
final EventBus eventBus) {
super(display, eventBus);
bind();
}
@Override
protected void onBind() {
// 'display' is a final global field containing the Display passed into the constructor.
display.getSend().addClickHandler(new ClickHandler() {
public void onClick(final ClickEvent event) {
doSend();
}
});
}
protected void onSend() {
// do something here
}
public void refreshDisplay() {
display.getName().setValue("some value");
}
}
- Code: Select all
public class GreetingView implements GreetingPresenter.Display {
private final TextBox name;
private final Button sendButton;
public GreetingView() {
final FlowPanel panel = new FlowPanel();
name = new TextBox();
panel.add(name);
sendButton = new Button("Go");
panel.add(sendButton);
// Add the nameField and sendButton to the RootPanel
// Use RootPanel.get() to get the entire body element
RootPanel.get("nameFieldContainer").add(name);
RootPanel.get("sendButtonContainer").add(sendButton);
}
public HasValue getName() {
return name;
}
public HasClickHandlers getSend() {
return sendButton;
}
public Widget asWidget() {
return panel;
}
}
An Eclipse project for a very basic MVP application is available from
here.
Particular points of interest are in the presenter's refreshDisplay (value pushed to the view) and the onBind method where a button event handler is registered. Additionally, the view and presenter instances are constructed and associated using Google Injector (GIN). Is GWT Designer able to handle code where a panel has been broken out into view construction and associated event handling + other business logic in this way?
Best regards,
Chris.