Oracle Fusion Middleware Java API Reference for Oracle Extension SDK
11g Release 2 (11.1.2.1.0)

E17493-02

oracle.javatools.ui.list
Class SimpleListModel<T>

java.lang.Object
  extended by javax.swing.AbstractListModel
      extended by oracle.javatools.ui.list.SimpleListModel<T>
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable<T>, javax.swing.ListModel
Direct Known Subclasses:
SimpleComboBoxModel, SimpleListModel

public class SimpleListModel<T>
extends javax.swing.AbstractListModel
implements java.lang.Iterable<T>

A simple list model based on a delegate java.util.List. The model contains utility methods for moving single elements up and down in the list and adding / removing elements firing the necessary events.

Also very useful is the removeIndices(int[]) method. This is a highly optimized way to remove a set of items from the model firing only the minimum necessary events for performance reasons. It's handy to use this if you have a JList which supports removal of a multiple selection.

The asList() method returns an object that implements the List interface. Changes you make to this list will automatically fire events to listeners of this list model. This is convenient if you want to pass a list model down to code that uses the collections framework.

See Also:
Serialized Form

Field Summary
 
Fields inherited from class javax.swing.AbstractListModel
listenerList
 
Constructor Summary
SimpleListModel()
          Create a simple list model based on a new ArrayList.
SimpleListModel(java.util.List<T> list)
          Create a simple list model based on the specified delegate list.
 
Method Summary
 void addElement(T o)
          Add an element, firing an intervalAdded event.
 void addElements(java.util.Iterator<T> i)
          Deprecated. use asList().addAll( Collection ).
 java.util.List<T> asList()
          Returns a List implementation that is bound to this list model.
 void changed()
          Fire a contentsChanged event for the whole list.
 void changed(int index)
          Fire a contentsChanged event for the element at the specified index.
 T getElementAt(int i)
          Get an element in the model by its index.
 int getSize()
          Get the number of elements in the model.
 void insertAt(int index, T o)
          Insert the specified element at the specified index in the list.
 java.util.Iterator<T> iterator()
          Get an iterator over all items in the model.
 void moveBottom(int index)
          Move the element at the specified index to the end of the list, firing the necessary intervalRemoved and intervalAdded events.
 void moveDown(int index)
          Move the element at the specified index down one position, firing the necessary intervalRemoved and intervalAdded events.
 void moveTop(int index)
          Move the element at the specified index to the start of the list, firing the necessary intervalRemoved and intervalAdded events.
 void moveUp(int index)
          Move the element at the specified index up one position, firing the necessary intervalRemoved and intervalAdded events.
 void removeAll()
          Remove all elements in the list, firing an intervalRemoved event.
 void removeElement(T o)
          Remove a single element, firing an intervalRemoved event.
 void removeIndices(int[] indices)
          Remove the specified indices from the model efficiently.
 
Methods inherited from class javax.swing.AbstractListModel
addListDataListener, fireContentsChanged, fireIntervalAdded, fireIntervalRemoved, getListDataListeners, getListeners, removeListDataListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleListModel

public SimpleListModel()
Create a simple list model based on a new ArrayList.


SimpleListModel

public SimpleListModel(java.util.List<T> list)
Create a simple list model based on the specified delegate list.

Parameters:
list - the delegate list to base the model on.
Throws:
java.lang.NullPointerException - if list is null.
Method Detail

asList

public java.util.List<T> asList()
Returns a List implementation that is bound to this list model.

The binding behavior means that you can safely call methods on the returned List that make changes to the list, and they will be reflected in the model, firing appropriate events.

Returns:
a List that you can use to manipulate the model, firing appropriate events. Will never return null.
Since:
11.0

getSize

public int getSize()
Get the number of elements in the model.

Specified by:
getSize in interface javax.swing.ListModel
Returns:
the number of elements in the model.

getElementAt

public T getElementAt(int i)
Get an element in the model by its index.

Specified by:
getElementAt in interface javax.swing.ListModel
Parameters:
i - the index of an element in the model. Must be >= 0 and < this.getSize().
Returns:
the element at the specified position in this list model.
Throws:
java.lang.IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).

iterator

public java.util.Iterator<T> iterator()
Get an iterator over all items in the model. The iterator is not modifiable: calls to Iterator.remove() will result in an UnsupportedOperationException. For a modifiable iterator, use asList().iterator().

Specified by:
iterator in interface java.lang.Iterable<T>
Returns:
an iterator over items in the list model.

addElements

@Deprecated
public void addElements(java.util.Iterator<T> i)
Deprecated. use asList().addAll( Collection ).

Add all the elements in the specified iterator to the list, firing a single intervalAdded event. If you are adding a number of elements to the list at once, it's more efficient to use this than calling addElement( Object ) multiple times.

Parameters:
i - the iterator over elements to add to the list.
Throws:
java.lang.NullPointerException - if i is null.

addElement

public void addElement(T o)
Add an element, firing an intervalAdded event.

Parameters:
o - the element to add.

removeIndices

public void removeIndices(int[] indices)
Remove the specified indices from the model efficiently. This is useful for e.g. removing all selected items in a JList that supports multiple selection. i.e. model.removeIndices( list.getSelectedIndices() );

Parameters:
indices - the indices to remove. The indices must be sorted. i.e. for all x : indices[x] < indices[x+1].
Throws:
java.lang.NullPointerException - if indices is null.

removeElement

public void removeElement(T o)
Remove a single element, firing an intervalRemoved event.

Parameters:
o - the element to remove.

removeAll

public void removeAll()
Remove all elements in the list, firing an intervalRemoved event.


changed

public void changed(int index)
Fire a contentsChanged event for the element at the specified index.

Parameters:
index - the index of an element that has changed. Must be >=0 and <getSize().
Throws:
java.lang.IndexOutOfBoundsException - if index is out of bounds.

changed

public void changed()
Fire a contentsChanged event for the whole list.


moveUp

public void moveUp(int index)
Move the element at the specified index up one position, firing the necessary intervalRemoved and intervalAdded events. This method will do nothing if the specified index is the first index in the list (0).

Parameters:
index - the index of an element to move up. Must be >=0 and <getSize(). If index is 0, this method does nothing.
Throws:
java.lang.IndexOutOfBoundsException - if index is out of bounds.

moveDown

public void moveDown(int index)
Move the element at the specified index down one position, firing the necessary intervalRemoved and intervalAdded events. This method will do nothing if the specified index is the last index in the list.

Parameters:
index - the index of an element to move down. Must be >=0 and <getSize(). If index is getSize()-1, this method does nothing.
Throws:
java.lang.IndexOutOfBoundsException - if index is out of bounds.

moveTop

public void moveTop(int index)
Move the element at the specified index to the start of the list, firing the necessary intervalRemoved and intervalAdded events. This method will do nothing if the specified index is the first index in the list (0).

Parameters:
index - the index of an element to move to the top. Must be >=0 and <getSize(). If index is 0, this method does nothing.
Throws:
java.lang.IndexOutOfBoundsException - if index is out of bounds.

moveBottom

public void moveBottom(int index)
Move the element at the specified index to the end of the list, firing the necessary intervalRemoved and intervalAdded events. This method will do nothing if the specified index is the last index in the list.

Parameters:
index - the index of an element to move to the end. Must be >=0 and <getSize(). If index is getSize()-1, this method does nothing.
Throws:
java.lang.IndexOutOfBoundsException - if index is out of bounds.

insertAt

public void insertAt(int index,
                     T o)
Insert the specified element at the specified index in the list.

Parameters:
index - the index to insert at.
o - the object to insert.

Oracle Fusion Middleware Java API Reference for Oracle Extension SDK
11g Release 2 (11.1.2.1.0)

E17493-02

Copyright © 1997, 2011, Oracle. All rights reserved.