Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Corrected the sort method so it would work when the data doesn't fit on one page; generified

...

Code Block
titleUserProvider.java
package com.adamkoch.learning.wicket;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

public class UserProvider extends SortableDataProvider {

	class SortableDataProviderComparator implements  List listComparator<Contact>, Serializable {
		public int compare(final Contact o1, final Contact o2) {
			PropertyModel<Comparable> model1 = new ArrayList( PropertyModel<Comparable>(o1, getSort().getProperty());

			PropertyModel<Comparable> model2 = new public UserProvider() {
        // important or you'll get a null pointer on line 40
        PropertyModel<Comparable>(o2, getSort().getProperty());

			int result = model1.getObject().compareTo(model2.getObject());

			if (!getSort().isAscending()) {
				result = -result;
			}

			return result;
		}

	}

	private List<Contact> list = new ArrayList<Contact>();
	private SortableDataProviderComparator comparator = new SortableDataProviderComparator();

	public UserProvider() {
		// The default sorting
		setSort("name.first", true);

        		list.add(new Contact(new Name("Abby", "Zerind")));
        		list.add(new Contact(new Name("Bernard", "Youst")));
        		list.add(new Contact(new Name("Charlie", "Xerg")));
        		list.add(new Contact(new Name("Deitri", "West")));
        		list.add(new Contact(new Name("Ernie", "Vuntang")));
        		list.add(new Contact(new Name("Frank", "Unter")));
    	}

    	public IteratorIterator<Contact> iterator(final int first, final int count) {
		// In this example the whole list  List newList = new ArrayList();
        newList.addAll(list.subList(first, first + count));

        final String sortColumn = this.getSort().getProperty();
        final boolean ascending = this.getSort().isAscending();

        Collections.sort(newList, new Comparator() {

            public int compare(Object obj1, Object obj2) {
                PropertyModel model1 = new PropertyModel(obj1, sortColumn);
                PropertyModel model2 = new PropertyModel(obj2, sortColumn);

                Object modelObject1 = model1.getObject();
                Object modelObject2 = model2.getObject();

                int compare = ((Comparable) modelObject1).compareTo(modelObject2);

                if (!ascending)
                    compare *= -1;

                return compare;
            }
        });

        return newList.iterator();
    }

    public int size() {
        return list.size();
    }

    public IModel model(final Object object) {
        return new AbstractReadOnlyModel() {
            public Object getObject() {
                return object;
            }
        };
    gets copied, sorted and sliced; in real applications typically your database would deliver a sorted and limited list 

		// Get the data
		List<Contact> newList = new ArrayList<Contact>(list);

		// Sort the data
		Collections.sort(newList, comparator);

		// Return the data for the current page - this can be determined only after sorting
		return newList.subList(first, first + count).iterator();
	}

	public IModel<Contact> model(final Contact object) {
		return new AbstractReadOnlyModel<Contact>() {
			@Override
			public Contact getObject() {
				return object;
			}
		};
	}

	public int size() {
		return list.size();
	}

}

class Contact implements Serializable {

    	private final Name name;

    	public Contact(final Name name) {
        		this.name = name;
    	}

    	public Name getName() {
        		return name;
    	}
}

class Name implements Serializable {

    	private String firstName;
    	private String lastName;

    	public Name(final String fName, final String lName) {
        		firstName = fName;
        		lastName = lName;
    	}

    	public String getFirst() {
        		return firstName;
    	}

    	public voidString setFirstgetLast(String firstName) {
        this.firstName = firstName;
    		return lastName;
	}

	public void setFirst(final  public String getLast(firstName) {
		this.firstName        return lastName;
    = firstName;
	}

    	public void setLast(final String lastName) {
        		this.lastName = lastName;
    	}
}

Code Block
titleDataTablePage.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
	<title>DataTable Example</title>
</head>

<body>
<table wicket:id="datatable"></table>
</body>
</html>

...