CubeTwister 2.0alpha142 2012-02-11

ch.randelshofer.gui.list
Class DefaultMutableListModel<T>

java.lang.Object
  extended by javax.swing.AbstractListModel
      extended by ch.randelshofer.gui.list.DefaultMutableListModel<T>
All Implemented Interfaces:
MutableListModel, java.io.Serializable, javax.swing.ListModel

public class DefaultMutableListModel<T>
extends javax.swing.AbstractListModel
implements MutableListModel

Default implementation of a MutableListModel.

Version:
3.0 2007-07-06 Added setter for importable Flavors. Turned class into a template.
2.0 2003-11-01 Revised.
1.0 2002-11-20 Created.
Author:
Werner Randelshofer
See Also:
Serialized Form

Field Summary
 
Fields inherited from class javax.swing.AbstractListModel
listenerList
 
Constructor Summary
DefaultMutableListModel()
          Creates a new instance of DefaultMutableListModel
DefaultMutableListModel(java.util.Collection<T> data)
           
DefaultMutableListModel(T[] data)
           
 
Method Summary
 void add(int index, T element)
          Inserts the specified element at the specified position in this list Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
 void add(T element)
          Adds the element to the end of the list.
 int addAll(int index, java.util.List l)
           
 void create(int index, java.lang.Object type)
          Creates the specified element type at the specified position in this list Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
 java.awt.datatransfer.Transferable exportTransferable(int[] indices)
          Creates a Transferable to use as the source for a data transfer of the specified elements.
 javax.swing.Action[] getActions(int[] indices)
          Gets actions for the specified elements.
 java.lang.Object getCreatableType(int index)
          Returns the default type of elements that can be created at the specified index of the list.
 java.lang.Object[] getCreatableTypes(int index)
          Returns the types of elements that can be created at the specified index of the list.
 T getElementAt(int index)
          Returns the value at the specified index.
 java.util.List<java.awt.datatransfer.DataFlavor> getImportableFlavors()
          Gets the importable data flavors for this list model.
 int getSize()
          Returns the length of the list.
 int importTransferable(java.awt.datatransfer.Transferable t, int action, int index, boolean asChild)
          Causes a transfer to the model from the specified transferable.
 boolean isAddable(int index)
          Returns true if an element can be added at the specified index.
 boolean isEditable(int index)
          Returns true if the value of the specified element may be changed using setElementAt(Object,int).
 boolean isImportable(java.awt.datatransfer.DataFlavor[] transferFlavors, int action, int index, boolean asChild)
          Indicates whether the model would accept an import of the given set of data flavors prior to actually attempting to import it.
 boolean isRemovable(int index)
          Returns true if the specified element may be removed.
 java.util.Iterator iterator()
           
 java.lang.Object remove(int index)
          Removes an element from the model.
 void set(int index, T value)
          Sets the value of an element at the given index.
 void setElementAt(java.lang.Object value, int index)
          Sets the value of an element at the given index.
 void setImportableFlavors(java.util.List<java.awt.datatransfer.DataFlavor> newValue)
          Sets the importable data flavors for this list model.
 
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
 
Methods inherited from interface javax.swing.ListModel
addListDataListener, removeListDataListener
 

Constructor Detail

DefaultMutableListModel

public DefaultMutableListModel()
Creates a new instance of DefaultMutableListModel


DefaultMutableListModel

public DefaultMutableListModel(java.util.Collection<T> data)

DefaultMutableListModel

public DefaultMutableListModel(T[] data)
Method Detail

getCreatableTypes

public java.lang.Object[] getCreatableTypes(int index)
Returns the types of elements that can be created at the specified index of the list.

Specified by:
getCreatableTypes in interface MutableListModel
Parameters:
index - The insertion point. 0 <= index <= getSize()
Returns:
an array of Object's that specify element types that can be inserted at the insertion point. Returns an empty array if no elements can be inserted here. Never returns null.

getCreatableType

public java.lang.Object getCreatableType(int index)
Returns the default type of elements that can be created at the specified index of the list.

Specified by:
getCreatableType in interface MutableListModel
Parameters:
index - The insertion point. 0 <= index <= getSize()
Returns:
an Object that specifies the default element type that can be inserted at the insertion point. Returns null if no elements can be inserted here.

create

public void create(int index,
                   java.lang.Object type)
Creates the specified element type at the specified position in this list Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Specified by:
create in interface MutableListModel
Parameters:
index - index at which the specified element is to be inserted.
type - element type to be inserted.
Throws:
java.lang.IllegalArgumentException - if the type is not contained in the array returned by getInsertableTypes(int).
java.lang.IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size()).

isAddable

public boolean isAddable(int index)
Returns true if an element can be added at the specified index.

Specified by:
isAddable in interface MutableListModel
Parameters:
index - index of the element. 0 <= index <= getSize()
See Also:
add(int, Object)

add

public void add(int index,
                T element)
Inserts the specified element at the specified position in this list Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Parameters:
index - index at which the specified element is to be inserted.
element - element to be inserted.
Throws:
java.lang.IllegalArgumentException - if the element is not of an acceptable type returned by getInsertableTypes(int).
java.lang.IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size()).

add

public void add(T element)
Adds the element to the end of the list.

Parameters:
element - the element.
Throws:
IllegalStateExcepton - if isElementInsertableAt(getSize()) returns false.

isRemovable

public boolean isRemovable(int index)
Returns true if the specified element may be removed.

Specified by:
isRemovable in interface MutableListModel
Parameters:
index - index of the element. 0 <= index <= getSize()
See Also:
remove(int)

remove

public java.lang.Object remove(int index)
Removes an element from the model.

Specified by:
remove in interface MutableListModel
Parameters:
index - index of the element. 0 <= index <= getSize()
Returns:
The removed Object.
Throws:
java.lang.IllegalStateException - if the element may not be removed.
See Also:
isRemovable(int)

isEditable

public boolean isEditable(int index)
Returns true if the value of the specified element may be changed using setElementAt(Object,int).

Specified by:
isEditable in interface MutableListModel
Parameters:
index - index of the element. 0 <= index <= getSize()
See Also:
setElementAt(Object, int)

getElementAt

public T getElementAt(int index)
Returns the value at the specified index.

Specified by:
getElementAt in interface javax.swing.ListModel
Parameters:
index - the requested index
Returns:
the value at index

setElementAt

public void setElementAt(java.lang.Object value,
                         int index)
Sets the value of an element at the given index.

Specified by:
setElementAt in interface MutableListModel
Parameters:
value - - the new value
index - - the index whose value is to be changed. 0 <= index < getSize()
Throws:
java.lang.IllegalStateException - if the element is not editable.
See Also:
isEditable(int)

set

public void set(int index,
                T value)
Sets the value of an element at the given index.

Parameters:
index - - the index whose value is to be changed. 0 <= index < getSize()
value - - the new value
Throws:
java.lang.IllegalStateException - if the element is not editable.
See Also:
isEditable(int)

getActions

public javax.swing.Action[] getActions(int[] indices)
Gets actions for the specified elements.

Specified by:
getActions in interface MutableListModel
Parameters:
indices - The elements.

getSize

public int getSize()
Returns the length of the list.

Specified by:
getSize in interface javax.swing.ListModel
Returns:
the length of the list

exportTransferable

public java.awt.datatransfer.Transferable exportTransferable(int[] indices)
Creates a Transferable to use as the source for a data transfer of the specified elements. Returns the representation of the elements to be transferred, or null if transfer is not possible.

Specified by:
exportTransferable in interface MutableListModel
Parameters:
indices - Element indices.
Returns:
A Transferable representing the elements to be transferred, or null if transfer is not possible.

isImportable

public boolean isImportable(java.awt.datatransfer.DataFlavor[] transferFlavors,
                            int action,
                            int index,
                            boolean asChild)
Indicates whether the model would accept an import of the given set of data flavors prior to actually attempting to import it.

Specified by:
isImportable in interface MutableListModel
Parameters:
transferFlavors - the data formats available
action - The action DnDConstants.ACTION_COPY, .ACTION_MOVE or .ACTION_LINK.
index - The insertion point. 0 <= index <= getSize()
asChild - True if the Transferable is dropped as a child of the element.
Returns:
true if the data can be imported.
See Also:
DnDConstants

importTransferable

public int importTransferable(java.awt.datatransfer.Transferable t,
                              int action,
                              int index,
                              boolean asChild)
                       throws java.awt.datatransfer.UnsupportedFlavorException,
                              java.io.IOException
Causes a transfer to the model from the specified transferable.

Specified by:
importTransferable in interface MutableListModel
Parameters:
t - The transfer data.
action - The action DnDConstants.ACTION_COPY, .ACTION_MOVE or .ACTION_LINK.
index - The insertion point. 0 <= index <= getSize()
asChild - True if the Transferable is dropped as a child of the element.
Returns:
The number of imported elements.
Throws:
java.awt.datatransfer.UnsupportedFlavorException
java.io.IOException

addAll

public int addAll(int index,
                  java.util.List l)

iterator

public java.util.Iterator iterator()

setImportableFlavors

public void setImportableFlavors(java.util.List<java.awt.datatransfer.DataFlavor> newValue)
Sets the importable data flavors for this list model.

Parameters:
newValue - A list which contains the data flavors.

getImportableFlavors

public java.util.List<java.awt.datatransfer.DataFlavor> getImportableFlavors()
Gets the importable data flavors for this list model.

Returns:
An immutable list which contains the data flavors.

(c) Werner Randelshofer.
All rights reserved.