org.antbear.jee.wicket.PlainPage.java Source code

Java tutorial

Introduction

Here is the source code for org.antbear.jee.wicket.PlainPage.java

Source

/*
 * Copyright 2011 Marcus Geiger.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.antbear.jee.wicket;

import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.Component;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.string.StringValue;

/**
 * Link anchors example page using plain lists.
 *
 * @author Marcus Geiger
 */
public class PlainPage extends WebPage {
    private static final long serialVersionUID = 1L;
    private static final String PAGE_PARAM_LINK_ID = "linkId";

    public PlainPage(final PageParameters parameters) {
        super(parameters);

        add(new FeedbackPanel("feedback").setOutputMarkupId(true));
        StringValue linkId = parameters.get(PAGE_PARAM_LINK_ID);
        if (!linkId.isEmpty()) {
            info("You called us with the page parameter " + PAGE_PARAM_LINK_ID + " and value " + linkId.toString());
        }

        final Form<?> form = new Form<Void>("form");
        add(form);

        // The model of the ListView is a List<Integer>
        form.add(new PropertyListView<Integer>("list", getList()) {
            private static final long serialVersionUID = 1L;

            // The model of a ListItem<Integer> is one Integer of the ListView<Integer>
            @Override
            protected void populateItem(ListItem<Integer> item) {
                // The Clou: set the markupId of the item to be the integer of our list
                item.setMarkupId(item.getDefaultModelObjectAsString());
                item.setOutputMarkupId(true);

                PageParameters parameters = new PageParameters();
                parameters.add(PAGE_PARAM_LINK_ID, item.getModelObject());

                // Create link and its text
                Link<Integer> link = new BookmarkablePageLink<Integer>("link", getWebPage().getClass(), parameters);
                link.setAnchor(item); // The anchor of the link now refers to the component of the ListItem (i.e. <li />)
                Component text = new Label("text", item.getDefaultModelObjectAsString()).setRenderBodyOnly(true);

                item.add(link.add(text));
            }
        });
    }

    private List<Integer> getList() {
        List<Integer> list = new ArrayList<Integer>();
        for (int n = 0; n < 256; n++)
            list.add(n);
        return list;
    }
}