com.anthonyeden.lib.util
Class XTreeMap

java.lang.Object
  extended byjava.util.AbstractMap
      extended byjava.util.TreeMap
          extended bycom.anthonyeden.lib.util.XTreeMap
All Implemented Interfaces:
java.lang.Cloneable, javax.swing.ListModel, java.util.Map, java.io.Serializable, java.util.SortedMap, javax.swing.table.TableModel

public class XTreeMap
extends java.util.TreeMap
implements javax.swing.table.TableModel, javax.swing.ListModel

An implementation of a TreeMap which can be placed in a JTable or in a JList. When used in a JTable the table will have two columns: column 1 for the key and column 2 for the value. Whan used in a JList the keys will be used by default for display.

Note that since this class extends from the TreeMap class the keys will be sorted in natural order as described in the TreeMap documentation.

Author:
Anthony Eden
See Also:
Serialized Form

Constructor Summary
XTreeMap()
           
 
Method Summary
 void addListDataListener(javax.swing.event.ListDataListener l)
          Add a ListDataListener.
 void addTableModelListener(javax.swing.event.TableModelListener l)
          Add a TableModelListener.
 void clear()
          Clear the map.
protected  java.lang.Object elementAt(int index, java.util.Collection c)
          Get the element at the given index in the given collection.
protected  void fireContentsChanged(int index0, int index1)
           
protected  void fireIntervalAdded(int index0, int index1)
           
protected  void fireIntervalRemoved(int index0, int index1)
           
protected  void fireRowsDeleted(int index0, int index1)
           
protected  void fireRowsInserted(int index0, int index1)
           
protected  void fireTableChanged(javax.swing.event.TableModelEvent evt)
           
 java.lang.Class getColumnClass(int column)
          Get the given column's class.
 int getColumnCount()
          Get the total number of columns.
 java.lang.String getColumnName(int column)
          Get the column name for the given column.
 java.lang.Object getElementAt(int index)
          Get the element at the given index.
 boolean getListUsesKey()
          Return true if the map keys are returned in the ListModel getElementAt() method.
 int getRowCount()
          Get the total number of rows.
 int getSize()
          Get the list size.
 java.lang.Object getValueAt(int row, int column)
          Get the value at the given row and column.
 boolean isCellEditable(int row, int column)
          Return true if the cell is editable.
 java.lang.Object keyAt(int index)
          Get the key at the given index.
 int keyIndex(java.lang.Object key)
          Get the index of the given key or -1 if there is no matching key.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Put the given object into the map using the given key.
 void putAll(java.util.Map map)
          Put all of the given map into this map.
 java.lang.Object remove(java.lang.Object key)
          Remove the object for the given key.
 void removeListDataListener(javax.swing.event.ListDataListener l)
          Remove a ListDataListener.
 void removeTableModelListener(javax.swing.event.TableModelListener l)
          Remove a TableModelListener.
 void setColumnClass(int column, java.lang.Class columnClass)
           
 void setColumnName(int column, java.lang.String name)
          Set the column name for the given column index.
 void setListUsesKey(boolean listUsesKey)
          Set to true if the map keys should be returned in the ListModel getElementAt() method.
 void setValueAt(java.lang.Object value, int row, int column)
          Set the value at the given row and column.
 java.lang.Object valueAt(int index)
          Get the value at the given index.
 
Methods inherited from class java.util.TreeMap
clone, comparator, containsKey, containsValue, entrySet, firstKey, get, headMap, keySet, lastKey, size, subMap, tailMap, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, isEmpty, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode, isEmpty
 

Constructor Detail

XTreeMap

public XTreeMap()
Method Detail

clear

public void clear()
Clear the map.

Specified by:
clear in interface java.util.Map

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Put the given object into the map using the given key. If there is already an object in the map with the given key then the object being replaced will be returned. If there is not value for the given key then the return value will be null.

Specified by:
put in interface java.util.Map
Parameters:
key - The key
value - The value
Returns:
The previous object

putAll

public void putAll(java.util.Map map)
Put all of the given map into this map.

Specified by:
putAll in interface java.util.Map
Parameters:
map - The map to insert

remove

public java.lang.Object remove(java.lang.Object key)
Remove the object for the given key. This method will return the removed object or null if no object is removed.

Specified by:
remove in interface java.util.Map
Parameters:
key - The key
Returns:
The removed object or null

keyIndex

public int keyIndex(java.lang.Object key)
Get the index of the given key or -1 if there is no matching key.

Parameters:
key - The key
Returns:
The index

keyAt

public java.lang.Object keyAt(int index)
Get the key at the given index.

Parameters:
index - The index
Returns:
The key at the given index

valueAt

public java.lang.Object valueAt(int index)
Get the value at the given index.

Parameters:
index - The index
Returns:
The value at the given index

elementAt

protected java.lang.Object elementAt(int index,
                                     java.util.Collection c)
Get the element at the given index in the given collection.

Parameters:
index - The index
c - The collection
Returns:
The element at index in the collection

getListUsesKey

public boolean getListUsesKey()
Return true if the map keys are returned in the ListModel getElementAt() method. If this method returns false then the getElementAt() method returns map values.

Returns:
True if getElementAt() returns keys

setListUsesKey

public void setListUsesKey(boolean listUsesKey)
Set to true if the map keys should be returned in the ListModel getElementAt() method. Set to false if the getElementAt() method should return map values.

Parameters:
listUsesKey - True if getElementAt() should return keys

getRowCount

public int getRowCount()
Get the total number of rows.

Specified by:
getRowCount in interface javax.swing.table.TableModel
Returns:
The total number of rows

getColumnCount

public int getColumnCount()
Get the total number of columns.

Specified by:
getColumnCount in interface javax.swing.table.TableModel
Returns:
The column count

getColumnName

public java.lang.String getColumnName(int column)
Get the column name for the given column.

Specified by:
getColumnName in interface javax.swing.table.TableModel
Parameters:
column - The column index
Returns:
The column name

setColumnName

public void setColumnName(int column,
                          java.lang.String name)
Set the column name for the given column index.

Parameters:
column - The column index
name - The new name

getColumnClass

public java.lang.Class getColumnClass(int column)
Get the given column's class.

Specified by:
getColumnClass in interface javax.swing.table.TableModel
Parameters:
column - The column
Returns:
The column class

setColumnClass

public void setColumnClass(int column,
                           java.lang.Class columnClass)

isCellEditable

public boolean isCellEditable(int row,
                              int column)
Return true if the cell is editable. The default behavior allows the UI user to change the key value.

Specified by:
isCellEditable in interface javax.swing.table.TableModel
Parameters:
row - The row
column - The column
Returns:
True if the column is editable

getValueAt

public java.lang.Object getValueAt(int row,
                                   int column)
Get the value at the given row and column.

Specified by:
getValueAt in interface javax.swing.table.TableModel
Parameters:
row - The row
column - The column
Returns:
The value

setValueAt

public void setValueAt(java.lang.Object value,
                       int row,
                       int column)
Set the value at the given row and column.

Specified by:
setValueAt in interface javax.swing.table.TableModel
Parameters:
value - The new value
row - The row
column - The column

addTableModelListener

public void addTableModelListener(javax.swing.event.TableModelListener l)
Add a TableModelListener.

Specified by:
addTableModelListener in interface javax.swing.table.TableModel
Parameters:
l - The TableModelListener to add

removeTableModelListener

public void removeTableModelListener(javax.swing.event.TableModelListener l)
Remove a TableModelListener.

Specified by:
removeTableModelListener in interface javax.swing.table.TableModel
Parameters:
l - The TableModelListener to remove

getElementAt

public java.lang.Object getElementAt(int index)
Get the element at the given index.

Specified by:
getElementAt in interface javax.swing.ListModel
Parameters:
index - The index
Returns:
The element

getSize

public int getSize()
Get the list size.

Specified by:
getSize in interface javax.swing.ListModel
Returns:
The list size

addListDataListener

public void addListDataListener(javax.swing.event.ListDataListener l)
Add a ListDataListener.

Specified by:
addListDataListener in interface javax.swing.ListModel
Parameters:
l - The ListDataListener

removeListDataListener

public void removeListDataListener(javax.swing.event.ListDataListener l)
Remove a ListDataListener.

Specified by:
removeListDataListener in interface javax.swing.ListModel
Parameters:
l - The ListDataListener

fireTableChanged

protected void fireTableChanged(javax.swing.event.TableModelEvent evt)

fireRowsInserted

protected void fireRowsInserted(int index0,
                                int index1)

fireRowsDeleted

protected void fireRowsDeleted(int index0,
                               int index1)

fireIntervalAdded

protected void fireIntervalAdded(int index0,
                                 int index1)

fireIntervalRemoved

protected void fireIntervalRemoved(int index0,
                                   int index1)

fireContentsChanged

protected void fireContentsChanged(int index0,
                                   int index1)


Copyright (c) 2000-2003 Anthony Eden. All Rights Reserved.