net.livesite.swing.table
Class XMLTableModel

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by net.livesite.swing.table.XMLTableModel
All Implemented Interfaces:
Serializable, TableModel

public class XMLTableModel
extends AbstractTableModel

This table model adapts a element of an XMLDocument to a JTable. Before this model can be applied to a JTable, four things must be set:

  • An array of column names
  • The XML Document
  • Tag name for children which are to be considered rows. Default is "Item".
  • An array of tag names which correspond to the column names Either use the four-parameter constructor, or set each individually:
  • setColumnNames
  • setXML
  • setRowTag
  • setTagNames

    Author:
    Ryan Gies
    See Also:
    Serialized Form

    Nested Class Summary
    protected  class XMLTableModel.SortListItem
              Compare one parent XML Element to another.
     
    Field Summary
    protected  Map columnComparators
               
    protected  XMLColumnInfo[] columns
               
    static Comparator COMPARABLE_COMAPRATOR
               
    protected  Document doc
               
     boolean isDirty
               
    static Comparator LEXICAL_COMPARATOR
               
    protected  Element root
               
    protected  String rowTag
               
    protected  int sortColumn
               
    protected  ArrayList sortList
               
     
    Fields inherited from class javax.swing.table.AbstractTableModel
    listenerList
     
    Constructor Summary
    XMLTableModel()
               
    XMLTableModel(Document doc, XMLColumnInfo[] columns, String rowTag)
              Proper constructor.
     
    Method Summary
     void addKeyListener(Component component)
              Prevents Alt+ and Ctrl+ events from starting a cell edit.
    protected  int appendElement(Element elem)
               
     Object createObject(Element elem, int col)
               
     Class getColumnClass(int c)
               
     int getColumnCount()
               
     String getColumnName(int col)
               
     XMLColumnInfo[] getColumns()
              Gets the columns for this instance.
     XMLColumnInfo getColumns(int index)
              Gets the columns for this instance.
    protected  Comparator getComparator(int col)
               
     Element getElementAt(int row)
              Get an XML element which corresponds to the row/column.
     Element getElementAt(int row, int col)
              Get an XML element which corresponds to the row/column.
     int getRealRow(int row)
               
     int getRowCount()
               
     int getSortedRow(int xmlIndex)
               
     Object getValueAt(int row, int col)
              In order to convert from the XML element to the table class, the table class must have a public constructor which takes a single String parameter.
     boolean isCellEditable(int row, int col)
               
     boolean isLocked()
               
     void lock()
               
    protected  void removeRow(Element row)
              Remove a row from the table model.
     void removeRow(int row)
              Remove a row from the table model.
     void setColumns(int index, XMLColumnInfo columns)
              Sets the columns for this instance.
     void setColumns(XMLColumnInfo[] columns)
              Sets the columns for this instance.
     void setRowTag(String rowTag)
              Set the tag name for items.
     void setValueAt(Object value, int row, int col)
               
     void setXML(Document doc)
              Set the XML document
    protected  void sortElements()
               
     void unlock()
               
     
    Methods inherited from class javax.swing.table.AbstractTableModel
    addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getListeners, getTableModelListeners, removeTableModelListener
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     

    Field Detail

    isDirty

    public boolean isDirty

    columns

    protected XMLColumnInfo[] columns

    rowTag

    protected String rowTag

    root

    protected Element root

    doc

    protected Document doc

    sortColumn

    protected int sortColumn

    sortList

    protected ArrayList sortList

    columnComparators

    protected Map columnComparators

    COMPARABLE_COMAPRATOR

    public static final Comparator COMPARABLE_COMAPRATOR

    LEXICAL_COMPARATOR

    public static final Comparator LEXICAL_COMPARATOR
    Constructor Detail

    XMLTableModel

    public XMLTableModel()

    XMLTableModel

    public XMLTableModel(Document doc,
                         XMLColumnInfo[] columns,
                         String rowTag)
    Proper constructor.

    Parameters:
    doc - XML document
    columns - column meta-data
    rowTag - tag name which identifies a row
    Method Detail

    setColumns

    public void setColumns(XMLColumnInfo[] columns)
    Sets the columns for this instance.

    Parameters:
    columns - The columns.

    setColumns

    public void setColumns(int index,
                           XMLColumnInfo columns)
    Sets the columns for this instance.

    Parameters:
    index - The index to set.
    columns - The columns.

    setXML

    public void setXML(Document doc)
    Set the XML document

    Parameters:
    doc - the XML document

    getColumns

    public XMLColumnInfo[] getColumns()
    Gets the columns for this instance.

    Returns:
    The columns.

    getColumns

    public XMLColumnInfo getColumns(int index)
    Gets the columns for this instance.

    Parameters:
    index - The index to get.
    Returns:
    The columns.

    setRowTag

    public void setRowTag(String rowTag)
    Set the tag name for items.

    Parameters:
    rowTag - the tag name (case sensitive!)

    getColumnCount

    public int getColumnCount()

    getRowCount

    public int getRowCount()

    getColumnName

    public String getColumnName(int col)
    Specified by:
    getColumnName in interface TableModel
    Overrides:
    getColumnName in class AbstractTableModel

    getColumnClass

    public Class getColumnClass(int c)
    Specified by:
    getColumnClass in interface TableModel
    Overrides:
    getColumnClass in class AbstractTableModel

    addKeyListener

    public void addKeyListener(Component component)
    Prevents Alt+ and Ctrl+ events from starting a cell edit.

    Parameters:
    component - a JTable compoenent

    isLocked

    public boolean isLocked()

    lock

    public void lock()

    unlock

    public void unlock()

    isCellEditable

    public boolean isCellEditable(int row,
                                  int col)
    Specified by:
    isCellEditable in interface TableModel
    Overrides:
    isCellEditable in class AbstractTableModel

    getElementAt

    public Element getElementAt(int row)
    Get an XML element which corresponds to the row/column. Will create elements which do not exist!

    Parameters:
    row - table row
    Returns:
    An existing or newly created element.

    appendElement

    protected int appendElement(Element elem)

    getElementAt

    public Element getElementAt(int row,
                                int col)
    Get an XML element which corresponds to the row/column.

    Parameters:
    row - table row
    col - table column
    Returns:
    An existing or newly created element.

    getValueAt

    public Object getValueAt(int row,
                             int col)
    In order to convert from the XML element to the table class, the table class must have a public constructor which takes a single String parameter. The table class should also have a default constructor which will be called when the aforementioned constructor fails.

    See Also:
    TableModel.getValueAt(int,int)

    createObject

    public Object createObject(Element elem,
                               int col)

    setValueAt

    public void setValueAt(Object value,
                           int row,
                           int col)
    Specified by:
    setValueAt in interface TableModel
    Overrides:
    setValueAt in class AbstractTableModel

    removeRow

    public void removeRow(int row)
                   throws ArrayIndexOutOfBoundsException
    Remove a row from the table model.

    Parameters:
    row -
    Throws:
    ArrayIndexOutOfBoundsException

    removeRow

    protected void removeRow(Element row)
    Remove a row from the table model.

    Parameters:
    row - element
    Throws:
    ArrayIndexOutOfBoundsException

    getSortedRow

    public int getSortedRow(int xmlIndex)

    getRealRow

    public int getRealRow(int row)

    sortElements

    protected void sortElements()

    getComparator

    protected Comparator getComparator(int col)