Friday, April 1, 2011

Using GWT DockLayoutPanel in Model-View-Presenter custom activities

Most of the MVP GWT examples are made using SimplePanel. In case you want to create something more complicated using the GWT 2.x layout system the simple panel will not work and you will get problems developing an designing your layouts. In my case I wanted to use the DockLayoutPanel and as I mention I wasn’t able to work with it. The problem was very simple to solve, you have to take care of the following points:

  • Use RootLayoutPanel instead of RootPanel
  • To be able to use layouts like presented in GWT 2.1 use LayoutPanel which implements the AcceptsOneWidget

First issue is easy to solve, the second one needs a little bit more knowledge but if you did it once you can use it everywhere in your application. This is the code shows you how to implement the interface:

1 public class SimpleWidgetPanel extends LayoutPanel implements AcceptsOneWidget {
2
3 private Widget widget;
4
5 @Override
6 public void setWidget(IsWidget w) {
7 setOneWidget(asWidgetOrNull(w));
8 }
9
10 private void setOneWidget(Widget w) {
11 // validate
12 if (w == widget) {
13 return;
14 }
15
16 // remove the old widget
17 if (widget != null) {
18 super.remove(widget);
19 }
20
21 // logical attach
22 widget = w;
23
24 if (w != null) {
25 super.add(w);
26 }
27 }
28 }

Using this panel you can attach it now to your activity mapper like this:


1 private SimpleWidgetPanel appWidget = new SimpleWidgetPanel();
2
3 // Start ActivityManager for the main widget with our ActivityMapper
4 ActivityMapper activityMapper = new AppActivityMapper(clientFactory);
5 ActivityManager activityManager = new ActivityManager(activityMapper,
6 eventBus);
7 activityManager.setDisplay(appWidget);
8

cheers

8 comments:

  1. AnonymousMay 15, 2011

    Thanks!

    ReplyDelete
  2. well thanks a lot! I am so happy that i found this :)

    ReplyDelete
  3. wonderful! thanks!!!

    ReplyDelete
  4. You f&^kn rock dude!!!

    ReplyDelete
  5. I think the built-in SimpleLayoutPanel will do pretty much the same thing as the class above by now

    ReplyDelete
  6. Hi Dirk,

    I didn't check this but I think your are right. Since then a lot has been change for better in GWT.

    ReplyDelete
  7. Nice elegant solution! I first tested your solution, and then tried the new SimpleLayoutPanel as suggested by dirk. Both approaches work, so it would seem that your solution is now obviated. Given that the issue was subtle (and not resolved in earlier GWT), I just wanted to say thanks for taking the time to publish your solution.

    ReplyDelete