Alvin Baena
Alvin Baena

Reputation: 903

Wicket inheritance issue

I'm developing a Wicket application for college and I ran into (a somewhat tricky) problem.

To put some context: I'm making an application that uses a HTML5 player, my solution to this was to give the constructor of the page that contains the player some PageParamameters to make a query and retrieve the sources for the media.

Now my problem is that I'm using a template, so I made a page that puts the Header and the footer automatically, but my player needs the PageParameters to work and I'm trying to make it so it can extend from BasePage, but the constructor from base page also has some parameters so it can dynamically change the text on the header based on the page it is currently on. This makes putting the super() method tricky:

protected BasePage(String tabHeader, String header) {
    add(new Label("tab_title", tabHeader));
    add(new Header("header", header));
    add(new UserPanel("user_panel"));
    add(new Footer("footer"));
}

And my player page:

public PlayerPage(PageParameters params) {
    String name = params.get("name").toString();
    MediaItem item = getItem(name.trim());

    add(new Label("tab_title", "MaeGûl - " + name)); //trying to get rid of this
    add(new Header("header", item.getName())); //and this
    add(new UserPanel("user_panel")); //and this

    if (item.getType().equals(ItemTypes.MUSIC)) {
        add(new AudioPlayer("player", item.getMediaSources()));
    } else if (item.getType().equals(ItemTypes.SERIES)
            || item.getType().equals(ItemTypes.MOVIES))
        add(new VideoPlayer("player", item.getMediaSources()));

    add(new Footer("footer")); //and this
}

As you may see, if I put the super(tabHeader, header) in my PlayerPage constructor it needs the "name" parameter to update the header and tabHeader of the BasePage, so I'm stupmed...

Any solutions to this?

Upvotes: 1

Views: 261

Answers (2)

jbrookover
jbrookover

Reputation: 5150

There is, of course, a method that can be used by any component to get the PageParameters. It is..

getPage().getPageParameters();

For getPageParameters() to return a non-null value, however, you must have called the Wicket Page constructor with super(params), therefore insisting that your BasePage have a constructor BasePage(PageParameters params).

In fact, after playing around with this for awhile, if you use BookmarkablePageLink and PageParameters for most of your pages, then anything extending Page should only implement the constructor Page(PageParameters params). This doesn't apply if you use session relative pages and a lot of new PlayerPage() type of code.

Upvotes: 0

Heri
Heri

Reputation: 2124

You may change the BasePage to accept models instead of strings:

protected BasePage(IModel<String> tabHeader, IModel<String> header)

and delegate the retrieval of the values to dedicated models, e.g. derived from AbstractReadOnlyModel:

public PlayerPage(PageParameters params) {
    super(new TabHeaderModel(params), new HeaderModel(params));
    ...
}

Upvotes: 2

Related Questions