0

I have the following parent container:

public class ParentContainer extends Composite {
    // Contains a bunch of TextButtons (RedButton, GreenButton, etc.).
    private LayoutPanel buttonPanel;

    // When user clicks a TextButton inside the buttonPanel,
    // it changes the content of this contentPanel.
    private LayoutPanel contentPanel;
}

So when the user clicks one of the TextButtons inside the buttonPanel, the contentPanel's contents change. I am trying to get each TextButton click to be remembered in history, using the Activities/Places framework. So, if the user clicks the "Red", "Green" and "Blue" buttons respectively, the contentPanel will change three times, and then they can click the Back/Forward browser history buttons and keep moving back and forth in history (and "replaying" the button clicks over and over again, etc.).

I also have the following classes:

com.mywebapp
    MainModule.gwt.xml
com.mywebapp.client
    MainModule
com.mywebapp.client.places
    RedButtonPlace
    GreenButtonPlace
    BlueButtonPlace
    ... 1 place for all buttons
com.mywebapp.client.activities
    RedButtonActivity
    GreenButtonActivity
    BlueButtonActivity
    ... 1 activity for all buttons
com.mywebapp.client.ui
    ParentContainer
    RedButton
    GreenButton
    BlueButton
    BlackButton
    PurpleButton
    OrangeButton

I am planning on wiring things up such that:

  • PlaceController.goTo(new RedButtonPlace()) eventually routes to the RedButtonActivity
  • PlaceController.goTo(new GreenButtonPlace()) eventually routes to the GreenButtonActivity
  • etc. (every button has a place and activity per its color)

What I'm stuck on is: if I call PlaceController.goTo(new RedButtonPlace()) from inside a RedButton click handler, how and where do I instruct RedButtonActivity to update contentPanel? For instance:

public class RedButton extends TextButton {
    // ... bunch of stuff, nevermind why I am extending TextButton
    // this is just to help me connect all the major dots of GWT!

    public RedButton() {
        this.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                // If the RedButton is clicked, we want all the content in RedButtonActivity#RedButtonView
                // to go inside ParentContainer#contentPanel.
                PlaceController.goto(new RedButtonPlace());
            }
        });
    }
}

public class RedButtonActivity extends AbstractActivity {
    public interface RedButtonView extends IsWidget {
        // Whatever the RedButton expects to be able to display.
    }

    private RedButtonView view;

    @Override
    public void start(AcceptsOneWidget panel, EventBus eventBus) {
        // Probably injected via GIN.
        view = somehowInjectTheView();

        panel.setWidget(view);
    }
}

That last line is the key here: panel.setWidget(view). How do we make sure that panel is the ParentContainer#contentPanel? Thanks in advance!

Edit: Per one answer suggests, here is a code update:

public class ParentContainer extends Composite {
    // All the stuff that's up above in the first parent container.

    public ParentContainer() {
        super();

        // Again, via GIN.
        ActivityManager redButtonActivityManager = getSomehow();
        redButtonActivityManager.setDisplay(contentPanel);
    }
}

If this is the correct way, then I assume when the start(AcceptsOneWidget panel, EventBus eventBus) method is called, the redButtonActivityManager knows to inject the correct display for the panel argument?

1 Answers1

1

You would pass the ParentContainer#contentPanel to the setDisplay() method of your ActivityManager as part of the manager's initialization.

Boris Brudnoy
  • 2,405
  • 18
  • 28
  • Thanks @Boris Brudnoy (+1) - I added an edit to the end of my question. Can you please read it and make sure I am understanding your answer correctly? If not, please correct me! I'll happily give you the "green check" if you can just clarify this for me! Thanks again! –  Nov 07 '12 at 22:30
  • You are correct in your edit: the `ActivityManager` would call the `start()` method of any activity it manages with the panel given to it via `setDisplay()`. – Boris Brudnoy Nov 07 '12 at 22:47
  • Boris you have absolutely no idea how big your answer to this question is for me. I have been trying to wrap my head around how GWT's Activities/Places framework, general MVP, and Widgets all tie together for several weeks now, and your answer here was the last little piece of the puzzle. The lightbulb is now fully on. Thank you, I wish I could upvote this more. –  Nov 07 '12 at 22:59