Wicket Component – Simple Breadcrumbs

No Comments »

A very simple simple wicket component to display links in an hierarchical order. You can see it live here

Full sources are available here.

This is how you add it to the page:

Java

		add(new Breadcrumbs("breadcrumbs",
			Arrays.asList(new WCLink[]{
					new WCLink("Web application section 1", "http://www.1.com"),
					new WCLink("Web application section 11" , "http://www.11.com"),
					new WCLink("Web application section 111", "http://www.111.com")
			}),this));

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns:wicket>
<body>
    <div wicket:id="breadcrumbs"/>
</body>
</html>

Wicket component code:
Breadcrumbs.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns:wicket>
<head>
</head>
<body>
<wicket:panel>
    <!--
    Wicket component:   JQuery Accordion Menu
    Author:             Nova Creator Software
    Website:            www.novacreator.com
    Software provided as is, without any guarantees.
    Free for any type of usage. We just kindly ask to keep this message.
    -->
    <ul id="breadcrumbs">
        <li wicket:id="repeater"><a wicket:id="link"><span wicket:id="linkName"></span></a></li>
    </ul>
</wicket:panel>
</body>
</html>

Breadcrumbs.java

package com.novacreator.wicketcomponents.breadcrumbs;

import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.behavior.HeaderContributor;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.html.resources.CompressedResourceReference;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.Model;

import com.novacreator.wicketcomponents.common.WCLink;

/*
Wicket component:   Breadcrumbs
Author:             Nova Creator Software
Website:            www.novacreator.com
Software provided as is, without any guarantees.
Free for any type of usage. We just kindly ask to keep this message.
*/
@SuppressWarnings("serial")
public class Breadcrumbs extends Panel {
	private static final ResourceReference CSS =
		new CompressedResourceReference(Breadcrumbs.class, "res/breadcrumbs.css");

	public Breadcrumbs(String id, List<WCLink> links, final BreadcrumbsListener listener) {
		super(id);

		add(HeaderContributor.forCss(CSS));

		RepeatingView rv = new RepeatingView("repeater");
		add(rv);
		AjaxFallbackLink afl = null;
		for(final WCLink link : links) {
			WebMarkupContainer wmc = new WebMarkupContainer(rv.newChildId());
			rv.add(wmc);
			afl = new AjaxFallbackLink("link"){
				@Override
				public void onClick(AjaxRequestTarget target) {
					if(link.getLink() != null && !link.getLink().isEmpty()) {
						listener.onBreadcrumbsClick(link.getLink(), target);
					}
				}
			};
			wmc.add(afl);
			afl.add(new Label("linkName", link.getDisplayName()));
		}
		afl.add(new AttributeModifier("style", true, new Model("background:none;")));
	}
}

BreadcrumbsListener.java

package com.novacreator.wicketcomponents.breadcrumbs;

import org.apache.wicket.ajax.AjaxRequestTarget;

/*
Wicket component:   Breadcrumbs
Author:             Nova Creator Software
Website:            www.novacreator.com
Software provided as is, without any guarantees.
Free for any type of usage. We just kindly ask to keep this message.
*/
public interface BreadcrumbsListener {
	public void onBreadcrumbsClick(String link, AjaxRequestTarget target);
}

Post a Comment