org.geotoolkit.coverage.sql
Class CoverageTableModel

Object
  extended by AbstractTableModel
      extended by CoverageTableModel
All Implemented Interfaces:
Serializable, TableModel

public class CoverageTableModel
extends AbstractTableModel

A Swing Table Model listing coverages entries. An instance of CoverageTableModel typically contains the coverages available in a particular layer, but this is not required.

This class also provides support for undo/redo operations, and can render the table cells in different colors according whatever the coverage file exists (the name of missing files are written in red) and if the coverage has been previously visited.

The code below shows how to use this table model in a Swing application:

Layer layer = coverageDatabase.getLayer("MyLayer");
CoverageTableModel model = new CoverageTableModel(layer.getCoverageReferences(null), null);
JTable table = new JTable(model);

// Enable cell coloring (optional).
TableCellRenderer renderer = new CoverageTableModel.CellRenderer();
view.setDefaultRenderer(String.class, renderer);
view.setDefaultRenderer(Date.class,   renderer);

// Enable undo/redo manager (optional).
UndoManager undoManager = new UndoManager();
model.addUndoableEditListener(undoManager);

// Show the table in a frame.
JFrame frame = new JFrame(layer.getName());
frame.add(table);
frame.pack();
frame.setVisible(true);
If a undo manager has been setup, the application can invoke the UndoManager.undo() and UndoManager.redo() methods.


Multi-threading
Like most Swing class, this class shall not be assumed thread-safe. This class is designed for usage from the event dispatcher thread, unless otherwise specified.

Since:
3.11 (derived from Seagis)
Version:
3.11
Author:
Martin Desruisseaux (IRD, Geomatys)
See Also:
CoverageList, Serialized Form
Module:
coverage/geotk-coverage-sql (download)    View source code for this class

Nested Class Summary
static class CoverageTableModel.CellRenderer
          A Table Cell Renderer for coloring the cells in a table using the Coverage Table Model.
 
Field Summary
 
Fields inherited from class AbstractTableModel
listenerList
 
Constructor Summary
CoverageTableModel(Collection<? extends GridCoverageReference> refs, Locale locale)
          Creates a new table model initialized to the content of the given collection.
CoverageTableModel(CoverageTableModel table)
          Creates a new table initialized to the content of the given model.
CoverageTableModel(Locale locale)
          Creates a new, initially empty, table model.
 
Method Summary
 void addUndoableEditListener(UndoableEditListener listener)
          Adds a new object to inform every time a undoable action has been performed.
 Transferable copy(int[] rows)
          Returns a transferable which can be set to the clipboard.
 Class<?> getColumnClass(int column)
          Returns the type of cell values in the column at the given index.
 int getColumnCount()
          Returns the number of columns in the model.
 String getColumnName(int column)
          Returns the column name at the given index.
protected  String getCoverageName(GridCoverageReference entry)
          Returns the coverage name to write in the table cell.
 String[] getCoverageNames()
          Returns the name of all coverages in this table model.
 String[] getCoverageNames(int... rows)
          Returns the name of coverages at the given row indices in this table model.
 GridCoverageReference getCoverageReferenceAt(int row)
          Returns the coverage reference at the given row index.
 GridCoverageReference[] getCoverageReferences()
          Returns all coverage references listed by this table model.
 int getRowCount()
          Returns the number of rows in the model.
 TimeZone getTimeZone()
          Returns the current timezone used for formatting dates.
 Object getValueAt(int row, int column)
          Returns the cell value at the given row and column.
 int[] indexOf(String... names)
          Returns the row indices of coverage references having the given name.
 void remove(GridCoverageReference... toRemove)
          Removes one or many coverage references from this table.
 void remove(int... rows)
          Removes one or many rows from this table.
 void removeUndoableEditListener(UndoableEditListener listener)
          Removes an object from the list of listeners to inform about undoable actions.
 void setCoverageReferences(Collection<? extends GridCoverageReference> references)
          Sets the content of this table model to the given collection of coverage references.
 void setTimeZone(TimeZone timezone)
          Sets the timezone to use for formatting dates.
 
Methods inherited from class AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getListeners, getTableModelListeners, isCellEditable, removeTableModelListener, setValueAt
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CoverageTableModel

public CoverageTableModel(Locale locale)
Creates a new, initially empty, table model. The initial timezone for the dates column is the local timezone, but this can be changed by a call to setTimeZone(TimeZone) after construction.

Parameters:
locale - The locale for column titles and cell formatting, or null for the default locale.

CoverageTableModel

public CoverageTableModel(Collection<? extends GridCoverageReference> refs,
                          Locale locale)
Creates a new table model initialized to the content of the given collection. The initial timezone for the date column is the local timezone, but this can be changed by a call to setTimeZone(TimeZone) after construction.

Parameters:
refs - References to the coverages to put in the list, or null if none.
locale - The locale for column titles and cell formatting, or null for the default locale.

CoverageTableModel

public CoverageTableModel(CoverageTableModel table)
Creates a new table initialized to the content of the given model. This is a copy constructor, except for the listener lists which are not copied.

Parameters:
table - The table model to copy.
Method Detail

setCoverageReferences

public void setCoverageReferences(Collection<? extends GridCoverageReference> references)
Sets the content of this table model to the given collection of coverage references. Any coverage references previously listed will be removed from this table model.

Parameters:
references - The new collection of coverage references.

getCoverageReferences

public GridCoverageReference[] getCoverageReferences()
Returns all coverage references listed by this table model.

Note: If a call to GridCoverageReference.read is planed, consider using the reference returned by getCoverageReferenceAt(int) instead.

Returns:
The coverage references listed by this table model, or an empty array if none (never null).

getCoverageReferenceAt

public GridCoverageReference getCoverageReferenceAt(int row)
Returns the coverage reference at the given row index. This method returns a special reference which will take trace of whatever a call to GridCoverageReference.read succeed or not, and will apply a color on the corresponding table cell accordingly.

Parameters:
row - The index of the desired coverage reference.
Returns:
The coverage reference at the given row.

getCoverageNames

public String[] getCoverageNames()
Returns the name of all coverages in this table model. The names are computed by getCoverageName(GridCoverageReference) and are usually unique for a given layer.

Returns:
The coverage names listed by this table model, or an empty array if none (never null).

getCoverageNames

public String[] getCoverageNames(int... rows)
Returns the name of coverages at the given row indices in this table model. The names are computed by getCoverageName(GridCoverageReference) and are usually unique for a given layer.

Parameters:
rows - The rows for which to get the coverage names.
Returns:
The coverage names at the given indices. The length of this array is equals to rows.length.

indexOf

public int[] indexOf(String... names)
Returns the row indices of coverage references having the given name. The given names shall be built in the same way than what the getCoverageName(GridCoverageReference) method do. This method is the converse of getCoverageNames(int[]).

If no image is found for a given name, the indices of the corresponding element in the returned array is set to -1.

Parameters:
names - The coverage reference names.
Returns:
Row indices of named coverages. The length of this array is equals to names.length. The array may contains -1 values for image not found.

remove

public void remove(int... rows)
Removes one or many rows from this table.

Parameters:
rows - The rows to remove.

remove

public void remove(GridCoverageReference... toRemove)
Removes one or many coverage references from this table. If a given coverage reference is not found in this table, then that reference is ignored.

Parameters:
toRemove - The coverage references to remove.

copy

public Transferable copy(int[] rows)
Returns a transferable which can be set to the clipboard. A Copy action in a Swing application could be implemented as below:
Transferable tr = model.copy(rows);
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
cb.setContents(tr, owner);

Parameters:
rows - The indices of the rows to copy in the transferable object.
Returns:
A copy of the specified rows as a transferable object.

getRowCount

public int getRowCount()
Returns the number of rows in the model.


getColumnCount

public int getColumnCount()
Returns the number of columns in the model.


getColumnName

public String getColumnName(int column)
Returns the column name at the given index.

Specified by:
getColumnName in interface TableModel
Overrides:
getColumnName in class AbstractTableModel

getColumnClass

public Class<?> getColumnClass(int column)
Returns the type of cell values in the column at the given index.

Specified by:
getColumnClass in interface TableModel
Overrides:
getColumnClass in class AbstractTableModel
Parameters:
column - The column index.

getValueAt

public Object getValueAt(int row,
                         int column)
Returns the cell value at the given row and column.

Parameters:
row - The 0-based row index.
column - The 0-based column index.
Returns:
The cell value at the given location.

getCoverageName

protected String getCoverageName(GridCoverageReference entry)
Returns the coverage name to write in the table cell. The default implementation returns GridCoverageReference.getName(). Subclasses can override this method in order to build the name differently.

Parameters:
entry - The coverage reference for which to get the name.
Returns:
The coverage name to write in the table cell.

getTimeZone

public TimeZone getTimeZone()
Returns the current timezone used for formatting dates.

Returns:
The timezone used for formatting dates.

setTimeZone

public void setTimeZone(TimeZone timezone)
Sets the timezone to use for formatting dates.

Parameters:
timezone - The new timezone to use.

addUndoableEditListener

public void addUndoableEditListener(UndoableEditListener listener)
Adds a new object to inform every time a undoable action has been performed.

Parameters:
listener - The listener to add.

removeUndoableEditListener

public void removeUndoableEditListener(UndoableEditListener listener)
Removes an object from the list of listeners to inform about undoable actions.

Parameters:
listener - The listener to remove.


Copyright © 2009-2012 Geotoolkit.org. All Rights Reserved.