Sorting of new added items

Hello all.
I’m trying to make new elements added to a Table appear in the right order. So far, I’ve tried:

-To set SortContainerPropertyId to my Table subclass.
-To set an itemSorter into my Container (BeanItemContainer subclass).

But new elements keep being added at the end of the table.

I’m using DefaultItemSorter instantiated on a comparator for String as:

public class CarrierComparator implements Comparator<Object> {
	public int compare(Object o1, Object o2) {
		if (o1 == null) {
			if (o2 == null) {
				return 0;
			} else {
				return -1;
			}
		}
		if (o2 == null) {
			return 1;
		}
		String c1 = (String) o1;
		String c2 = (String) o2;
		return c1.compareTo(c2);
	}
}

This comparator is not being called when a new Item is added to the container through addItem(Object itemId) so, I suspect I need to make something else.

Best regards.

Going deeper into this problem, I’ve tried two approaches:

  • To override BeanItem addItem(Object itemId) in my BeanItemContainer subclass, as:
	public BeanItem<Carrier> addItem(Object itemId) {
		BeanItem<Carrier> bi = super.addItem(itemId);
		doSort();
		return bi;
	}

Doesn’t look very efficient, to sort the whole collection every time an element is added, but since the collection is always sorted, perhaps it’s not too hard. Unfortunately, this solution doesn’t work, resulting into the following exception:

ERROR: Error in getCarrierList
java.lang.NullPointerException
	at com.vaadin.data.util.DefaultItemSorter.compare(DefaultItemSorter.java:77)
	at java.util.Arrays.mergeSort(Arrays.java:1284)
	at java.util.Arrays.sort(Arrays.java:1223)
	at java.util.Collections.sort(Collections.java:159)
	at com.vaadin.data.util.BeanItemContainer.doSort(BeanItemContainer.java:491)
	at es.tid.spci.test.data.CarrierContainer.addItem(CarrierContainer.java:88)
	at es.tid.spci.test.server.CarrierService.fetchCarriers(CarrierService.java:138)
	at es.tid.spci.test.server.CarrierService.getData(CarrierService.java:64)
	at es.tid.spci.test.ui.CarrierList.<init>(CarrierList.java:19)
	at es.tid.spci.test.PoC.getCarrierList(PoC.java:98)
	at es.tid.spci.test.PoC.getLeftFrame(PoC.java:88)
	at es.tid.spci.test.PoC.init(PoC.java:115)
	at com.vaadin.Application.start(Application.java:545)
	at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.startApplication(AbstractApplicationServlet.java:1152)
	at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:465)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
	at java.lang.Thread.run(Thread.java:595)

It fails while adding the second item to the container.

  • The other approach is to calculate the position to insert the new item, something like:
public BeanItem<Carrier> addItem(Object itemId) {
		Carrier thisCarrier = (Carrier)itemId;
		Collection<Carrier> items = this.getItemIds();
		BeanItem<Carrier> bi = null;
		boolean added = false;
		Object previousItem = null;
		for (Carrier item : items) {
			if (item.getName().compareTo(thisCarrier.getName()) > 0) {
				log.info("Adding " + thisCarrier.getName() 
						+ " before " + item.getName());
				bi = addItemAfter(previousItem, itemId);
				added = true;
				break;
			}
			previousItem = item;
		}

		if (!added) {
			bi = super.addItem(itemId);
		}
		return bi;
	}

It gets a little complex, but works.

Any comments, suggestions, about the approach to follow, the error I got…

Best regards.