Extending SpinnerListModel
If you want functionality for looping the values of the JSpinner use this class:
package mseries.utils;
import java.util.List;
import javax.swing.SpinnerListModel;
import java.util.Arrays;
/**
*
Title: RespinnerListModel
*
Description: Extends SpinnerListModel with looping functionality.
*
* @author Victor Tsonkov
* @version 11.12.2006
*/
public class RespinnerListModel extends SpinnerListModel
{
/**
* Indicates if the control supports wrapping.
*/
private boolean isWrap = false;
/**
* Create a reference to the list because the base list is private.
*/
private List values = null;
/**
* The index of the current element.
*/
private int curIndex = 0;
/**
* The index of the previous element.
*/
private int prevIndex = -1;
/**
* Initializes a new instance of the RespinnerListModel class.
*/
public RespinnerListModel()
{
}
/**
* Initializes a new instance of the RespinnerListModel class.
*/
public RespinnerListModel(List<?> values)
{
super(values);
this.values = values;
}
/**
* Initializes a new instance of the RespinnerListModel class.
*/
public RespinnerListModel(Object[] values)
{
super(values);
this.values = Arrays.asList(values);
}
/**
* Sets the wrapping of values in the list.
* @param isWrap boolean
*/
public void setWrap(boolean isWrap)
{
this.isWrap = isWrap;
}
/**
* Gets the wrapping of values in the list.
* @param isWrap boolean
*/
public boolean isWrap()
{
return isWrap;
}
/**
* Returns the next legal value of the underlying sequence or
* <code>null</code> if value is already the last element.
*
* @return the next legal value of the underlying sequence or
*<code>null</code> if value is already the last element
* @see SpinnerModel#getNextValue
* @see #getPreviousValue
*/
public Object getNextValue()
{
Object result = null;
result = super.getNextValue();
if (isWrap)
{
if (result == null)
result = values.get(0);
}
return result;
}
/**
* Returns the previous element of the underlying sequence or
* <code>null</code> if value is already the first element.
*
* @return the previous element of the underlying sequence or
*<code>null</code> if value is already the first element
* @see SpinnerModel#getPreviousValue
* @see #getNextValue
*/
public Object getPreviousValue()
{
Object result = null;
result = super.getPreviousValue();
if (isWrap)
{
if (result == null)
result = values.get(values.size()-1);
}
return result;
}
/**
* Gets the first value of the list.
* @return Object
*/
public Object getFirstValue()
{
return values.get(0);
}
/**
* Gets the last value of the list.
* @return Object
*/
public Object getLastValue()
{
return values.get(values.size()-1);
}
/**
* Changes the list that defines this sequence and resets the index
* of the models <code>value</code> to zero. Note that <code>list</code>
* is not copied, the model just stores a reference to it.
*
* This method fires a <code>ChangeEvent</code> if <code>list</code> is
* not equal to the current list.
*
* @param list the sequence that this model represents
* @throws IllegalArgumentException if <code>list</code> is
*<code>null</code> or zero length
* @see #getList
*/
public synchronized void setList(List<?> values)
{
super.setList(values);
this.values = values;
this.prevIndex = 0;
}
/**
* Gets the index of the previous element.
* @return Object
*/
public Object getPreviouslySetValue()
{
Object result = null;
if (prevIndex > -1)
result = values.get(prevIndex);
return result;
}
/**
* Sets the value of the list.
* @param value Object
*/
public void setValue(Object value)
{
int index = values.indexOf(value);
if (index > -1 && index != this.curIndex)
{
prevIndex = this.curIndex;
this.curIndex = index;
}
super.setValue(value);
}
}

