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

Java tutorial

Introduction

Here is the source code for org.antbear.jee.wicket.DemoPanel.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.Date;
import java.util.List;
import org.apache.wicket.event.Broadcast;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A panel with a list, where items can be added. The list wants to anchor itself to
 * be visible when changed.
 *
 * @author Marcus Geiger
 */
class DemoPanel extends Panel {

    private static final long serialVersionUID = 1L;
    private static Logger log = LoggerFactory.getLogger(DemoPanel.class);

    public DemoPanel(String id) {
        super(id);

        final Form<Void> form = new Form<Void>("form");
        form.setMarkupId("form_" + id);

        Label myid = new Label("myid", form.getMarkupId());
        final DemoStringListView listView = new DemoStringListView("listItem");

        SubmitLink addLink = new SubmitLink("addLink", form) {
            private static final long serialVersionUID = 1L;

            @Override
            public void onSubmit() {
                System.out.println("onSubmit " + this.getParent().getMarkupId());
                listView.addItem("Another item, added @ " + new Date());

                // Now comes the interresting logic as of wicket 1.5
                // .. we want to anchor the page to our #ID. Send an event to our parent
                // with the ID to be anchored.
                AnchorPayload event = new AnchorPayload(form.getMarkupId());
                log.info("Sending event " + event);

                send(getPage(), Broadcast.BREADTH, event);
            }
        };

        add(form);
        form.add(myid);
        form.add(addLink);
        form.add(listView);
    }

    // A trivial list view with demo items
    private static class DemoStringListView extends PropertyListView<String> {
        private static final long serialVersionUID = 1L;

        public DemoStringListView(String id) {
            super(id, getDemoData());
        }

        @Override
        protected void populateItem(ListItem<String> item) {
            item.add(new Label("text", item.getDefaultModelObjectAsString()));
        }

        public void addItem(String text) {
            @SuppressWarnings("unchecked")
            List<String> modelObject = (List<String>) getList();
            modelObject.add(text);
        }
    }

    // Create some data for filling up a list view
    private static List<? extends String> getDemoData() {
        List<String> r = new ArrayList<String>();
        for (int n = 0; n < 25; n++) {
            r.add("Item " + n);
        }
        return r;
    }
}