package org.mastodon.trackmate.ui.wizard;

import java.util.List;
import java.util.ListIterator;

/**
 * A simple {@link WizardSequence} based on a {@link List}.
 *
 * @author Jean-Yves Tinevez
 */
public class ListWizardSequence implements WizardSequence
{

	private ListIterator< WizardPanelDescriptor > it;

	private WizardPanelDescriptor current;

	private final List< WizardPanelDescriptor > list;

	public ListWizardSequence( final List< WizardPanelDescriptor > list )
	{
		this.list = list;
		assert !list.isEmpty(): "Sequence list is empty.";
		init();
	}

	@Override
	public WizardPanelDescriptor current()
	{
		return current;
	}

	@Override
	public WizardPanelDescriptor next()
	{
		if ( !it.hasNext() )
			return null;
		current = it.next();
		System.out.println( "Moving to next: " + current ); // DEBUG
		return current;
	}

	@Override
	public boolean hasNext()
	{
		return it.hasNext();
	}

	@Override
	public WizardPanelDescriptor previous()
	{
		if ( !it.hasPrevious() )
			return null;
		current = it.previous();
		System.out.println( "Moving to previous: " + current ); // DEBUG
		return current;
	}

	@Override
	public boolean hasPrevious()
	{
		return it.hasPrevious();
	}

	@Override
	public WizardPanelDescriptor init()
	{
		it = list.listIterator();
		current = it.next();
		return current;
	}
}

@@ -53,20 +53,27 @@
 va.util.
-List
+Collections
 ;%0Aimport
@@ -87,24 +87,16 @@
 til.List
-Iterator
 ;%0A%0A/**%0A 
@@ -256,29 +256,34 @@
 %7B%0A%0A%09
-private ListIterator%3C
+/**%0A%09 * The list of %7B@link
  Wiz
@@ -304,108 +304,320 @@
 ptor
- %3E it;%0A%0A%09private WizardPanelDescriptor current;%0A%0A%09private final List%3C WizardPanelDescriptor %3E list;%0A
+%7Ds iterated in this sequence.%0A%09 */%0A%09protected final List%3C WizardPanelDescriptor %3E list;%0A%0A%09/**%0A%09 * The current position in the sequence.%0A%09 */%0A%09protected int pos;%0A%0A%09/**%0A%09 * Instantiates a new sequence with a copy of the specified list.%0A%09 * %0A%09 * @param list%0A%09 *            the list to iterate in this sequence.%0A%09 */
 %0A%09pu
@@ -705,65 +705,60 @@
 t = 
-list;%0A%09%09assert !list.isEmpty(): %22Sequence list is empty.%22
+Collections.unmodifiableList( list );%0A%09%09this.pos = 0
 ;%0A%09%09
@@ -824,39 +824,47 @@
 t()%0A%09%7B%0A%09%09return 
-current
+list.get( pos )
 ;%0A%09%7D%0A%0A%09@Override
@@ -915,142 +915,75 @@
 f ( 
-!it.hasNext() )%0A%09%09%09return null;%0A%09%09current = it.next();%0A%09%09System.out.println( %22Moving to next: %22 + current ); // DEBUG%0A%09%09return current
+pos %3E= list.size() - 1 )%0A%09%09%09return null;%0A%09%09return list.get( ++pos )
 ;%0A%09%7D
@@ -1037,20 +1037,29 @@
 urn 
-it.hasNext()
+pos %3C list.size() - 1
 ;%0A%09%7D
@@ -1126,25 +1126,16 @@
 f ( 
-!it.hasPrevious()
+pos %3C= 0
  )%0A%09
@@ -1155,116 +1155,32 @@
 ;%0A%09%09
-current = it.previous();%0A%09%09System.out.println( %22Moving to previous: %22 + current ); // DEBUG%0A%09%09return current
+return list.get( --pos )
 ;%0A%09%7D
@@ -1238,24 +1238,15 @@
 urn 
-it.hasPrevious()
+pos %3E 0
 ;%0A%09%7D
@@ -1304,55 +1304,15 @@
 %7B%0A%09%09
-it = list.listIterator();%0A%09%09current = it.next()
+pos = 0
 ;%0A%09%09
@@ -1325,15 +1325,17 @@
  current
+()
 ;%0A%09%7D%0A%7D%0A

Fix the ListWizardSequence.