ListSelect item -moving the item up and down using button

I have an event of a Button click on which I want to move the selected (single) item in the ListSelect to on position up/down.
I am unable to find a way to insert an item in ListSelect at a particular index.

Could anybody have experinced similar issue? Any help is appreciated.

-Bala

Had exactly the same problem.
Solved it by using the Container.Indexed datasource of the ListSelect.


	private void moveUp(final ListSelect listBox) {
		final Object selectedItemId = listBox.getValue();			
		if (selectedItemId != null) {
			final Container.Indexed container = (Indexed) listBox.getContainerDataSource();			
			final Object prevItemId = container.prevItemId(selectedItemId);
			if (prevItemId != null) {
				swap(container, selectedItemId, prevItemId);
			}
		}
	}

	private void moveDown(final ListSelect listBox) {
		final Object selectedItemId = listBox.getValue();	
		
		if (selectedItemId != null) {
			final Container.Indexed container = (Indexed) listBox.getContainerDataSource();			
			final Object nextItemId = container.nextItemId(selectedItemId);
			if (nextItemId != null) {
				swap(container, selectedItemId, nextItemId);
			}
		}
	}

	private void swap(
			final Container.Indexed container, 
			final Object itemId1, 
			final Object itemId2) {

		final int itemId1Index = container.indexOfId(itemId1);
		if (itemId1Index == -1) throw new IllegalArgumentException("There is no item id " + itemId1 + " in container!");
		
		final int itemId2Index = container.indexOfId(itemId2);
		if (itemId2Index == -1) throw new IllegalArgumentException("There is no item id " + itemId2 + " in container!");
		
		container.removeItem(itemId1);
		container.removeItem(itemId2);
		
		if (itemId1Index < itemId2Index) {
			container.addItemAt(itemId1Index, itemId2);
			container.addItemAt(itemId2Index, itemId1);
		} else {			
			container.addItemAt(itemId2Index, itemId1);
			container.addItemAt(itemId1Index, itemId2);
		}
	}

Hi

Can you do it for multiple selection?