be.ugent.caagt.swirl.lists

Class CellList

Implemented Interfaces:
ListDataListener, Scrollable

public class CellList
extends JComponent
implements Scrollable, ListDataListener

List component in which list items are layed out as cells in a grid. The functionality is the same as that of javax.swing.JList except for the following: This list component uses a cellRenderer delegate of type CellListCellRenderer to paint the visible cells in the list and to obtain the correct tool tip information for that cell.

Constructor Summary

CellList(ListModel dataModel, int cellWidth, int cellHeight, int numberOfColumns)
Construct a list of this type for the given model, with the given cell size.

Method Summary

void
contentsChanged(ListDataEvent e)
void
ensureIndexIsVisible(int index)
Scrolls the viewport to make the specified cell completely visible.
Rectangle
getCellBounds(int index0, int index1)
Returns the bounds of the specified range of items in component coordinates.Returns null if index isn't valid.
int
getCellHeight()
Height of a grid cell.
CellListCellRenderer
getCellRenderer()
Return the cell renderer used by this list.
int
getCellWidth()
Width of a grid cell.
Point
getDropLocation()
Return the last drag-and-drop location where this component is a target.
Dimension
getMinimumSize()
ListModel
getModel()
Return the current data model.
Dimension
getPreferredScrollableViewportSize()
Dimension
getPreferredSize()
int
getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
Returns the distance to scroll vertically to expose the next or previous block.
boolean
getScrollableTracksViewportHeight()
boolean
getScrollableTracksViewportWidth()
int
getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
Returns the distance to scroll vertically to expose the next or previous row.
Object
getSelectedValue()
Returns the first selected value, or null if the selection is empty.
Object[]
getSelectedValues()
Returns an array of the values for the selected cells.
ListSelectionModel
getSelectionModel()
Return the current selection model.
String
getToolTipText(MouseEvent event)
Overrides JComponent's getToolTipText method in order to allow the renderer's tips to be used if it has text set.
Point
indexToLocation(int index)
Return the top left coordinate of the cell with given index, or null if the index is not valid.
void
initActionMap()
Initialize the action map.
void
intervalAdded(ListDataEvent e)
void
intervalRemoved(ListDataEvent e)
boolean
isDragEnabled()
Is dragging on this component enabled?
boolean
isDropInProgress()
Is a drag-and-drop operation in progress where this component is a target?
int
locationToIndex(Point location)
Convert a point in component coordinates to the index of the cell at that location, or -1 if there is no such cell.
void
setCellHeight(int cellHeight)
Set the height of a grid cell.
void
setCellRenderer(CellListCellRenderer cellRenderer)
Set the cell renderer.
void
setCellSize(int cellWidth, int cellHeight)
Set width and height of a grid cell.
void
setCellWidth(int cellWidth)
Set the width of a grid cell.
void
setDragEnabled(boolean dragEnabled)
Indicate whether dragging on this component should be enabled.
void
setDropTarget(DropTarget dt)
void
setModel(ListModel model)
Sets a new data model and clears the selection.
void
setNumberOfColumns(int numberOfColumns)
Set the number of columns to use in this list.
void
setSelectionModel(ListSelectionModel selectionModel)
Set or change the selection model.
@Override
void doLayout()
protected @Override
void paintComponent(Graphics g)

Constructor Details

CellList

public CellList(ListModel dataModel,
                int cellWidth,
                int cellHeight,
                int numberOfColumns)
Construct a list of this type for the given model, with the given cell size.
Parameters:
cellWidth - width of every grid cell
cellHeight - height of every grid cell
numberOfColumns - initial number of columns

Method Details

contentsChanged

public void contentsChanged(ListDataEvent e)

ensureIndexIsVisible

public void ensureIndexIsVisible(int index)
Scrolls the viewport to make the specified cell completely visible. Only works when the list is in a scroll pane.
Parameters:
index - the index of the cell to make visible

getCellBounds

public Rectangle getCellBounds(int index0,
                               int index1)
Returns the bounds of the specified range of items in component coordinates.Returns null if index isn't valid.
Parameters:
index0 - the index of the first cell in the range
index1 - the index of the last cell in the range
Returns:
the bounds of the indexed cells in pixels

getCellHeight

public int getCellHeight()
Height of a grid cell.
Returns:
Value of property cellHeight.

getCellRenderer

public CellListCellRenderer getCellRenderer()
Return the cell renderer used by this list.

getCellWidth

public int getCellWidth()
Width of a grid cell.
Returns:
Value of property cellWidth.

getDropLocation

public Point getDropLocation()
Return the last drag-and-drop location where this component is a target.

getMinimumSize

public Dimension getMinimumSize()

getModel

public ListModel getModel()
Return the current data model.

getPreferredScrollableViewportSize

public Dimension getPreferredScrollableViewportSize()

getPreferredSize

public Dimension getPreferredSize()

getScrollableBlockIncrement

public int getScrollableBlockIncrement(Rectangle visibleRect,
                                       int orientation,
                                       int direction)
Returns the distance to scroll vertically to expose the next or previous block. We are using the follows rules:
  • If scrolling down, the last visible row should become the first completely visible row.
  • If scrolling up, the first visible row should become the last completely visible row.

getScrollableTracksViewportHeight

public boolean getScrollableTracksViewportHeight()

getScrollableTracksViewportWidth

public boolean getScrollableTracksViewportWidth()

getScrollableUnitIncrement

public int getScrollableUnitIncrement(Rectangle visibleRect,
                                      int orientation,
                                      int direction)
Returns the distance to scroll vertically to expose the next or previous row. If the top (resp. bottom) row was only partially exposed it now becomes fully exposed.

getSelectedValue

public Object getSelectedValue()
Returns the first selected value, or null if the selection is empty.

getSelectedValues

public Object[] getSelectedValues()
Returns an array of the values for the selected cells. The returned values are sorted in increasing index order.

getSelectionModel

public ListSelectionModel getSelectionModel()
Return the current selection model.

getToolTipText

public String getToolTipText(MouseEvent event)
Overrides JComponent's getToolTipText method in order to allow the renderer's tips to be used if it has text set.
See Also:
JComponent.getToolTipText

indexToLocation

public Point indexToLocation(int index)
Return the top left coordinate of the cell with given index, or null if the index is not valid.

initActionMap

public final void initActionMap()
Initialize the action map.

intervalAdded

public void intervalAdded(ListDataEvent e)

intervalRemoved

public void intervalRemoved(ListDataEvent e)

isDragEnabled

public boolean isDragEnabled()
Is dragging on this component enabled?

isDropInProgress

public boolean isDropInProgress()
Is a drag-and-drop operation in progress where this component is a target?

locationToIndex

public int locationToIndex(Point location)
Convert a point in component coordinates to the index of the cell at that location, or -1 if there is no such cell.

setCellHeight

public void setCellHeight(int cellHeight)
Set the height of a grid cell.
Parameters:
cellHeight - New value of property cellHeight.

setCellRenderer

public void setCellRenderer(CellListCellRenderer cellRenderer)
Set the cell renderer.

setCellSize

public void setCellSize(int cellWidth,
                        int cellHeight)
Set width and height of a grid cell.
Parameters:
cellWidth - New value of property cellWidth.
cellHeight - New value of property cellHeight.

setCellWidth

public void setCellWidth(int cellWidth)
Set the width of a grid cell.
Parameters:
cellWidth - New value of property cellWidth.

setDragEnabled

public void setDragEnabled(boolean dragEnabled)
Indicate whether dragging on this component should be enabled.

setDropTarget

public void setDropTarget(DropTarget dt)

setModel

public void setModel(ListModel model)
Sets a new data model and clears the selection.

setNumberOfColumns

public void setNumberOfColumns(int numberOfColumns)
Set the number of columns to use in this list.
Parameters:
numberOfColumns - New value of property numberOfColumns.

setSelectionModel

public final void setSelectionModel(ListSelectionModel selectionModel)
Set or change the selection model.

void doLayout

public @Override void doLayout()

void paintComponent

protected @Override void paintComponent(Graphics g)