31 grudnia 2010

Custom PagingNavigator with changing items per page in Wicket

As I am migrating my blog to my own domain, a few posts in the future will be published in both places to allow You, reader, to change address and rss smoothly :)

New blog address: http://tomaszdziurko.pl
New RSS link: http://www.tomaszdziurko.pl/feed

This post can be found at
http://tomaszdziurko.pl/2010/12/custom-pagingnavigator-with-changing-items-per-page-in-wicket/



In one of my recent projects I had to create Wicket pagination component with one additional functionality allowing user to dynamically change maximum number of items presented on each page.

Finished component will look like below:



Of course I was not going to implement this from the scratch, because most of work had been already done by Wicket authors and commiters in component PagingNavigator. Source of this component as a reference to changes I made can be found here.

The first steps in creating our component:
- add List itemsPerPageValues holding numbers for 'items per page' which will be shown to the user allowing him to click and change this value.
- provide default List DEFAULT_ITEMS_PER_PAGE_VALUES, which will be used (what a surprise!) as a default :)
- give our component reference to the DataView object on which we will execute change items per page method.

Below we can see changes in class fields and constructors:

public class CustomPagingNavigator extends Panel {

public static final String NAVIGATION_ID = "navigation";
public static final List<Integer> DEFAULT_ITEMS_PER_PAGE_VALUES = Arrays.asList(5, 10, 50);

private PagingNavigation pagingNavigation;
private final DataView<?> dataView;
private final IPagingLabelProvider labelProvider;
private final List<Integer> itemsPerPageValues;
private WebMarkupContainer pagingLinksContainer;

public CustomPagingNavigator(final String id, final DataView<?> dataView) {
this(id, dataView, null, DEFAULT_ITEMS_PER_PAGE_VALUES);
}

public CustomPagingNavigator(final String id, final DataView<?> dataView, List<Integer> itemsPerPageValues) {
this(id, dataView, null, itemsPerPageValues);
}

public CustomPagingNavigator(final String id, final DataView<?> dataView, final IPagingLabelProvider labelProvider) {
this(id, dataView, labelProvider, DEFAULT_ITEMS_PER_PAGE_VALUES);
}

public CustomPagingNavigator(final String id, final DataView<?> dataView, final IPagingLabelProvider labelProvider,
List<Integer> itemsPerPageValues) {
super(id);
this.dataView = dataView;
this.labelProvider = labelProvider;
this.itemsPerPageValues = itemsPerPageValues;

// these methods will be described later in this post
addContainerWithPagingLinks();
addLinksChangingItemsPerPageNumber();
}
(...)
}


First new method is addContainerWithPagingLinks based mainly of onBeforeRender from PagingNavigator. This method adds paging links to the component to allow user to change number of viewed page. One small addition is an overriden isVisible which will hide paging fragment when there is only one page to show.

private void addContainerWithPagingLinks() {

pagingLinksContainer = new WebMarkupContainer("pagingLinksContainer") {
@Override
public boolean isVisible() {
return dataView.getPageCount() > 1;
}
}

pagingNavigation = newNavigation(dataView, labelProvider);
pagingLinksContainer.add(pagingNavigation);

// Add additional page links
pagingLinksContainer.add(newPagingNavigationLink("first", dataView, 0).add(
new TitleAppender("PagingNavigator.first")));
pagingLinksContainer.add(newPagingNavigationIncrementLink("prev", dataView, -1).add(
new TitleAppender("PagingNavigator.previous")));
pagingLinksContainer.add(newPagingNavigationIncrementLink("next", dataView, 1).add(
new TitleAppender("PagingNavigator.next")));
pagingLinksContainer.add(newPagingNavigationLink("last", dataView, -1).add(
new TitleAppender("PagingNavigator.last")));

add(pagingLinksContainer);
}

Next step is to override isVisible method in our component which will hide it completely when DataView has no items to render:

@Override
public boolean isVisible() {
return dataView.getItemCount() > 0;
}

And now it's time to create the core of our component: a place where items per page can be changed. This is done in method:

private void addLinksChangingItemsPerPageNumber() {
ListView<Integer> itemsPerPageList = new ListView<Integer>("itemsPerPage", itemsPerPageValues) {
@Override
protected void populateItem(ListItem<Integer> item) {
Link<Void> itemPerPageLink = new ItemPerPageLink<Void>("itemPerPageLink", dataView,
pagingLinksContainer, item.getModelObject());
itemPerPageLink.add(new Label("itemsValue", item.getModel()));
item.add(itemPerPageLink);
}
};

add(itemsPerPageList);
}

In the above method we create ListView for Integers from itemsPerPageValues and for each number we add link (new class ItemPerPageLink explained below) which will change DataView itemsPerPage property. Except reference to DataView we also give to our link reference to pagingLinksContainer to hide it after user changes itemsPerPage and there will be only one page to show.

Complete class ItemPerPageLink source code:

public class ItemPerPageLink<T> extends Link<T> {

private final int itemsPerPage;
private final DataView<?> dataView;
private final WebMarkupContainer pagingLinksContainer;

public ItemPerPageLink(final String id, final DataView<?> dataView, WebMarkupContainer pagingLinksContainer, int itemsPerPageValue) {
super(id);
this.dataView = dataView;
this.pagingLinksContainer = pagingLinksContainer;
this.itemsPerPage = itemsPerPageValue;
setEnabled(itemsPerPageValue != dataView.getItemsPerPage());

}

@Override
public void onClick() {
dataView.setItemsPerPage(itemsPerPage);
pagingLinksContainer.setVisible(dataView.getPageCount() > 1);
}

@Override
protected void onComponentTag(ComponentTag tag) {
super.onComponentTag(tag);
tag.put("title", itemsPerPage);
}

}

In this class we:
1. Disable link for number which is current dataView.itemsPerPage value.
2. Hide pagingLinksContainer when there is only one page.
3. Change itemsPerPage property in onClick method.
4. Set link title to the value of items per page.

And that's all. For those who want complete solution in one place there is complete source of the class CustomPagingNavigator with its markup:


import java.util.Arrays;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.behavior.AbstractBehavior;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.AbstractLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.navigation.paging.IPageable;
import org.apache.wicket.markup.html.navigation.paging.IPagingLabelProvider;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigation;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigationIncrementLink;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigationLink;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.data.DataView;

public class CustomPagingNavigator extends Panel {

public static final String NAVIGATION_ID = "navigation";
public static final List<Integer> DEFAULT_ITEMS_PER_PAGE_VALUES = Arrays.asList(5, 25, 50);

private PagingNavigation pagingNavigation;
private final DataView<?> dataView;
private final IPagingLabelProvider labelProvider;
private final List<Integer> itemsPerPageValues;
private WebMarkupContainer pagingLinksContainer;

public CustomPagingNavigator(final String id, final DataView<?> dataView) {
this(id, dataView, null, DEFAULT_ITEMS_PER_PAGE_VALUES);
}

public CustomPagingNavigator(final String id, final DataView<?> dataView, List<Integer> itemsPerPageValues) {
this(id, dataView, null, itemsPerPageValues);
}

public CustomPagingNavigator(final String id, final DataView<?> dataView, final IPagingLabelProvider labelProvider) {
this(id, dataView, labelProvider, DEFAULT_ITEMS_PER_PAGE_VALUES);
}

public CustomPagingNavigator(final String id, final DataView<?> dataView, final IPagingLabelProvider labelProvider,
List<Integer> itemsPerPageValues) {
super(id);
this.dataView = dataView;
this.labelProvider = labelProvider;
this.itemsPerPageValues = itemsPerPageValues;

addContainerWithPagingLinks();
addLinksChangingItemsPerPageNumber();
}

@Override
public boolean isVisible() {
return dataView.getItemCount() > 0;
}

private void addContainerWithPagingLinks() {

pagingLinksContainer = new WebMarkupContainer("pagingLinksContainer") {
@Override
public boolean isVisible() {
return dataView.getPageCount() > 1;
}
};

pagingNavigation = newNavigation(dataView, labelProvider);
pagingLinksContainer.add(pagingNavigation);

// Add additional page links
pagingLinksContainer.add(newPagingNavigationLink("first", dataView, 0).add(
new TitleAppender("PagingNavigator.first")));
pagingLinksContainer.add(newPagingNavigationIncrementLink("prev", dataView, -1).add(
new TitleAppender("PagingNavigator.previous")));
pagingLinksContainer.add(newPagingNavigationIncrementLink("next", dataView, 1).add(
new TitleAppender("PagingNavigator.next")));
pagingLinksContainer.add(newPagingNavigationLink("last", dataView, -1).add(
new TitleAppender("PagingNavigator.last")));

add(pagingLinksContainer);
}

protected PagingNavigation newNavigation(final IPageable pageable, final IPagingLabelProvider labelProvider) {
return new PagingNavigation(NAVIGATION_ID, pageable, labelProvider);
}

protected AbstractLink newPagingNavigationIncrementLink(String id, IPageable pageable, int increment) {
return new PagingNavigationIncrementLink<Void>(id, pageable, increment);
}

protected AbstractLink newPagingNavigationLink(String id, IPageable pageable, int pageNumber) {
return new PagingNavigationLink<Void>(id, pageable, pageNumber);
}

private void addLinksChangingItemsPerPageNumber() {
ListView<Integer> itemsPerPageList = new ListView<Integer>("itemsPerPage", itemsPerPageValues) {
@Override
protected void populateItem(ListItem<Integer> item) {
Link<Void> itemPerPageLink = new ItemPerPageLink<Void>("itemPerPageLink", dataView,
pagingLinksContainer, item.getModelObject());
itemPerPageLink.add(new Label("itemsValue", item.getModel()));
item.add(itemPerPageLink);
}
};

add(itemsPerPageList);
}

public final PagingNavigation getPagingNavigation() {
return pagingNavigation;
}

private final class TitleAppender extends AbstractBehavior {
private static final long serialVersionUID = 1L;

private final String resourceKey;

public TitleAppender(String resourceKey) {
this.resourceKey = resourceKey;
}

@Override
public void onComponentTag(Component component, ComponentTag tag) {
tag.put("title", CustomPagingNavigator.this.getString(resourceKey));
}
}
}


And its markup:

<?xml version="1.0" encoding="UTF-8" ?>
<html xmlns:wicket>
<body>
<wicket:panel>

<div>
<div>
Items per page:
<span wicket:id="itemsPerPage">
<a wicket:id="itemPerPageLink"><span wicket:id="itemsValue"></span></a>
</span>
</div>
<div wicket:id="pagingLinksContainer">
<table>

<tbody>
<tr valign="top">
<td>
<a wicket:id="first" href="">
<span>First</span>
</a>
</td>
<td>
<a wicket:id="prev" href="">
<span>Previous</span>
</a>
</td>
<td wicket:id="navigation">
<a wicket:id="pageLink"><span
wicket:id="pageNumber">5</span></a>
</td>

<td>
<a wicket:id="next" href="#">
<span>Next</span></a>
</td>
<td>
<a wicket:id="last" href="#">
<span>Last</span></a>
</td>
</tr>
</tbody>
</table>

</div>
</div>

</wicket:panel>
</body>
</html>


Usage of the component

Our newly created component can be used in a very similar way to the standard Wicket PagingNavigator:


public class DataViewPanel extends Panel {

public DataViewPanel(String id) {
super(id);
DataView dataView = new DataView("dataView", new CustomDataProvider());
dataView.setItemsPerPage(5);
add(dataView);

CustomPagingNavigator customPagingNavigator = new CustomPagingNavigator("paginator", dataView);
add(customPagingNavigator);
}
}

19 grudnia 2010

What would you do if your boss give you one week for self-education?

Let’s suppose following scenario:

You are somewhat experienced Java Developer working on a very sophisticated project in the science sector. You create very, very complicated module using very, very time-consuming algorithm which can not be multithreaded. And when you are ready, tests with small data portions went fine, your boss comes to your desk and clicks big, red button on your monitor to proudly start the whole process. Of course nothing bad happens, you just can observe growing CPU and memory usage. Your team expect that those calculation will take about one week to finish and give you results which are indispensable for you next tasks. It’s JIRA blocker status and until it finishes, you had nothing to do. Your boss asks you to visit him in his room and sais:

- Tom, you did a great job with this module. Everything seems to run smoothly and I can not wait to see the results! We must wait one week and it’s a spare time which I would like you to use for you self-education and self-development. What you are going to do and learn?

Of course we don’t know whether this boss is going to pay Tom for this time or not :) But it’s not the case. Tom now has a problem: 40 hours of free time which he can spend on any technology/methodology/language/etc he wants to learn and this opportunity comes to him so unexpectedly that he can not make up his mind.

Please help him! What would YOU do with these 40 hours? :)

PS: As I am migrating my blog to my own domain, a few posts in the future will be published in both places to allow You, reader, to change address and rss smoothly :)

New blog address: http://tomaszdziurko.pl
New RSS link: http://www.tomaszdziurko.pl/feed

16 grudnia 2010

Zmiana silnika bloga i przenosiny na własną domenę

Drodzy czytelnicy.

Zdecydowałem się przenieść bloga na silnik Wordpressa, który daje dużo większe możliwości i znacznie większą elastyczność w dostosowaniu strony do moich wymagań. A skoro powiedziałem A to trzeba było powiedzieć i B, więc blog w nowej postaci jest dostępny na mojej własnej, brandowanej moim nazwiskiem domenie :)

Zapraszam serdecznie do odwiedzania bloga pod nowym adresem http://tomaszdziurko.pl, a korzystających z RSS'ów proszę o przepięcie się na http://www.tomaszdziurko.pl/feed.

Oczywiście będę wdzięczny za wszelkie uwagi odnośnie wyglądu i działania nowego bloga.

Do zobaczenia w nowej lokalizacji :)