net.sf.jeppers.grid
Class JGrid

java.lang.Object
  extended byjava.awt.Component
      extended byjava.awt.Container
          extended byjavax.swing.JComponent
              extended bynet.sf.jeppers.grid.JGrid
All Implemented Interfaces:
javax.swing.event.CellEditorListener, java.util.EventListener, GridModelListener, java.awt.image.ImageObserver, java.awt.MenuContainer, javax.swing.Scrollable, java.io.Serializable
Direct Known Subclasses:
JGridHeader

public class JGrid
extends javax.swing.JComponent
implements javax.swing.Scrollable, javax.swing.event.CellEditorListener, GridModelListener

The JGrid is used to display and edit regular two-dimensional grid of cells. Unlike JTable which is fundamentally vertical in that the structure is determined in the columns, while the rows contain the data. JGrid is symmetrical with respect to the vertical and the horizontal orientations. JGrid also allows for cells to be merged into bigger rectangular arrays, called spans.

Author:
Cameron Zemek
See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Field Summary
 
Fields inherited from class javax.swing.JComponent
TOOL_TIP_TEXT_KEY, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
JGrid()
           
JGrid(GridModel gridModel, SpanModel spanModel, StyleModel styleModel)
           
JGrid(GridModel gridModel, SpanModel spanModel, StyleModel styleModel, RulerModel rowModel, RulerModel columnModel, SelectionModel selectionModel)
           
JGrid(int rows, int columns)
           
 
Method Summary
 void changeSelection(int row, int column, boolean toggle, boolean extend)
           
 JGrid cloneView()
           
 int columnAtPoint(java.awt.Point point)
          Return the column at the specified point
 boolean editCellAt(int row, int column)
           
 boolean editCellAt(int row, int column, java.util.EventObject e)
          Programmatically starts editing the cell at row and column, if the cell is editable.
 void editingCanceled(javax.swing.event.ChangeEvent e)
          Invoked when editing is canceled.
 void editingStopped(javax.swing.event.ChangeEvent e)
          Invoked when editing is finished.
 void ensureCellInVisibleRect(int row, int column)
           
 java.awt.Rectangle getCellBounds(int row, int column)
          Get cell bounds of (row, column)
 GridCellEditor getCellEditor(int row, int column)
           
 GridCellRenderer getCellRenderer(int row, int column)
           
 int getColumnCount()
          Returns the number of columns in this grid's model.
 RulerModel getColumnModel()
           
 int getColumnPosition(int column)
          Return the left horizontal coordinate (in pixels) of column
 int getColumnWidth(int column)
          Returns the width (in pixels) of column.
 GridCellEditor getCurrentCellEditor()
          Return the current cell editor
 int getEditingColumn()
          Returns the index of the column that contains the cell currently being edited.
 int getEditingRow()
          Returns the index of the row that contains the cell currently being edited.
 java.awt.Component getEditorComponent()
          Returns the component that is handling the editing session.
 java.awt.Color getFocusBackgroundColor()
          Returns the background color for cells with focus.
 java.awt.Color getFocusForegroundColor()
          Returns the foreground color for cells with focus.
 java.awt.Color getGridColor()
          Returns the color used to draw grid lines.
 GridModel getGridModel()
           
 java.awt.Dimension getPreferredScrollableViewportSize()
          Returns the preferred size of the viewport for this table.
 int getRowCount()
          Returns the number of rows in this grid's model.
 int getRowHeight(int row)
          Returns the height (in pixels) of row.
 RulerModel getRowModel()
           
 int getRowPosition(int row)
          Return the top vertical coordinate (in pixels) of row
 int getScrollableBlockIncrement(java.awt.Rectangle visibleRect, int orientation, int direction)
          Returns visibleRect.height or visibleRect.width, depending on this spreadsheet's orientation.
 boolean getScrollableTracksViewportHeight()
          Returns false to indicate that the height of the viewport does not determine the height of the spreadsheet.
 boolean getScrollableTracksViewportWidth()
          Returns false to indicate that the width of the viewport does not determine the width of the spreadsheet.
 int getScrollableUnitIncrement(java.awt.Rectangle visibleRect, int orientation, int direction)
          Returns the scroll increment (in pixels) that completely exposes one new row or column (depending on the orientation).
 java.awt.Color getSelectionBackgroundColor()
          Returns the background color for selected cells.
 java.awt.Color getSelectionForegroundColor()
          Returns the foreground color for selected cells.
 SelectionModel getSelectionModel()
           
 boolean getShowGrid()
          Returns true if the grid draws grid lines.
 SpanModel getSpanModel()
           
 StyleModel getStyleModel()
           
 GridUI getUI()
          Returns the L&F object that renders this component.
 java.lang.String getUIClassID()
          Returns a string that specifies the name of the l&f class that renders this component.
 java.lang.Object getValueAt(int row, int column)
           
 void gridChanged(GridModelEvent event)
          Sync row and column sizes between models
 boolean isCellSpan(int row, int column)
           
 boolean isEditing()
          Returns true if a cell is being edited.
 boolean isSelected(int row, int column)
           
 java.awt.Component prepareEditor(GridCellEditor editor, int row, int column)
          Prepares the editor for cell(row, column)
 java.awt.Component prepareRenderer(GridCellRenderer renderer, int row, int column)
          Prepares the renderer for painting cell(row,column)
 void removeEditor()
          Discards the editor object and frees the real estate it used for cell rendering.
 void resizeAndRepaint()
           
 int rowAtPoint(java.awt.Point point)
          Return the row at the specified point
 void setColumnModel(RulerModel model)
           
 void setColumnWidth(int column, int width)
          Sets the width for column to width.
 void setEditingColumn(int aColumn)
          Sets the editingColumn variable.
 void setEditingRow(int aRow)
          Sets the editingRow variable.
 void setFocusBackgroundColor(java.awt.Color focusBackgroundColor)
          Sets the background color for cells with focus.
 void setFocusForegroundColor(java.awt.Color focusForegroundColor)
          Sets the foreground color for cells with focus.
 void setGridColor(java.awt.Color gridColor)
          Sets the color used to draw grid lines.
 void setGridModel(GridModel model)
           
 void setPreferredScrollableViewportSize(java.awt.Dimension size)
          Sets the preferred size of the viewport for this table.
 void setRowHeight(int row, int height)
          Sets the height for row to height.
 void setRowModel(RulerModel model)
           
 void setSelectionBackgroundColor(java.awt.Color selectionBackgroundColor)
          Sets the background color for selected cells.
 void setSelectionForegroundColor(java.awt.Color selectionForegroundColor)
          Sets the foreground color for selected cells.
 void setSelectionModel(SelectionModel model)
           
 void setShowGrid(boolean show)
          Sets wether grid lines should be drawn.
 void setSpanModel(SpanModel model)
           
 void setStyleModel(StyleModel model)
           
 void setUI(GridUI ui)
          Sets the L&F object that renders this component.
 void setValueAt(java.lang.Object value, int row, int column)
           
 void updateUI()
          Notification from the UIFactory that the L&F has changed.
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paint, paintImmediately, paintImmediately, print, printAll, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setLayout, transferFocusBackward, transferFocusDownCycle, validate
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, contains, createImage, createImage, createVolatileImage, createVolatileImage, dispatchEvent, enable, enableInputMethods, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JGrid

public JGrid()

JGrid

public JGrid(int rows,
             int columns)

JGrid

public JGrid(GridModel gridModel,
             SpanModel spanModel,
             StyleModel styleModel)

JGrid

public JGrid(GridModel gridModel,
             SpanModel spanModel,
             StyleModel styleModel,
             RulerModel rowModel,
             RulerModel columnModel,
             SelectionModel selectionModel)
Method Detail

cloneView

public JGrid cloneView()

getFocusForegroundColor

public java.awt.Color getFocusForegroundColor()
Returns the foreground color for cells with focus.

Returns:
the Color used for the foreground of focused cells

setFocusForegroundColor

public void setFocusForegroundColor(java.awt.Color focusForegroundColor)
Sets the foreground color for cells with focus.

Parameters:
focusForegroundColor - the Color to use for foreground of focused cells

getFocusBackgroundColor

public java.awt.Color getFocusBackgroundColor()
Returns the background color for cells with focus.

Returns:
the Color used for the background of focused cells

setFocusBackgroundColor

public void setFocusBackgroundColor(java.awt.Color focusBackgroundColor)
Sets the background color for cells with focus.

Parameters:
focusBackgroundColor - the Color to use for background of focused cells

getSelectionForegroundColor

public java.awt.Color getSelectionForegroundColor()
Returns the foreground color for selected cells.

Returns:
the Color used for the foreground of selected cells.

setSelectionForegroundColor

public void setSelectionForegroundColor(java.awt.Color selectionForegroundColor)
Sets the foreground color for selected cells.

Parameters:
selectionForegroundColor - the Color used for the foreground of selected cells

getSelectionBackgroundColor

public java.awt.Color getSelectionBackgroundColor()
Returns the background color for selected cells.

Returns:
the Color used for the background of selected cells.

setSelectionBackgroundColor

public void setSelectionBackgroundColor(java.awt.Color selectionBackgroundColor)
Sets the background color for selected cells.

Parameters:
selectionBackgroundColor - the Color used for the background of selected cells

getGridColor

public java.awt.Color getGridColor()
Returns the color used to draw grid lines.

Returns:
the Color used to draw grid lines

setGridColor

public void setGridColor(java.awt.Color gridColor)
Sets the color used to draw grid lines.

Parameters:
gridColor - the new Color of the grid lines

getShowGrid

public boolean getShowGrid()
Returns true if the grid draws grid lines.

Returns:
true if the grid draws grid lines.

setShowGrid

public void setShowGrid(boolean show)
Sets wether grid lines should be drawn.


getRowCount

public int getRowCount()
Returns the number of rows in this grid's model.

Returns:
the number of rows in this grid's model.

getColumnCount

public int getColumnCount()
Returns the number of columns in this grid's model.

Returns:
the number of columns in this grid's model.

getRowHeight

public int getRowHeight(int row)
Returns the height (in pixels) of row.

Parameters:
row - the row whose height is to be returned
Returns:
the height (in pixels) of row

getColumnWidth

public int getColumnWidth(int column)
Returns the width (in pixels) of column.

Parameters:
column - the column whose width is to be returned
Returns:
the width (in pixels) of column

setRowHeight

public void setRowHeight(int row,
                         int height)
Sets the height for row to height.

Parameters:
row - the row whose height is to be changed
height - new row height (in pixels)

setColumnWidth

public void setColumnWidth(int column,
                           int width)
Sets the width for column to width.

Parameters:
column - the column whose width is to be changed
width - new column width (in pixels)

getRowPosition

public int getRowPosition(int row)
Return the top vertical coordinate (in pixels) of row


getColumnPosition

public int getColumnPosition(int column)
Return the left horizontal coordinate (in pixels) of column


getCellBounds

public java.awt.Rectangle getCellBounds(int row,
                                        int column)
Get cell bounds of (row, column)


rowAtPoint

public int rowAtPoint(java.awt.Point point)
Return the row at the specified point


columnAtPoint

public int columnAtPoint(java.awt.Point point)
Return the column at the specified point


isCellSpan

public boolean isCellSpan(int row,
                          int column)

getCellRenderer

public GridCellRenderer getCellRenderer(int row,
                                        int column)

getCellEditor

public GridCellEditor getCellEditor(int row,
                                    int column)

prepareRenderer

public java.awt.Component prepareRenderer(GridCellRenderer renderer,
                                          int row,
                                          int column)
Prepares the renderer for painting cell(row,column)


prepareEditor

public java.awt.Component prepareEditor(GridCellEditor editor,
                                        int row,
                                        int column)
Prepares the editor for cell(row, column)


isSelected

public boolean isSelected(int row,
                          int column)

ensureCellInVisibleRect

public void ensureCellInVisibleRect(int row,
                                    int column)

changeSelection

public void changeSelection(int row,
                            int column,
                            boolean toggle,
                            boolean extend)

getGridModel

public GridModel getGridModel()

setGridModel

public void setGridModel(GridModel model)

getSelectionModel

public SelectionModel getSelectionModel()

setSelectionModel

public void setSelectionModel(SelectionModel model)

getSpanModel

public SpanModel getSpanModel()

setSpanModel

public void setSpanModel(SpanModel model)

getStyleModel

public StyleModel getStyleModel()

setStyleModel

public void setStyleModel(StyleModel model)

getRowModel

public RulerModel getRowModel()

setRowModel

public void setRowModel(RulerModel model)

getColumnModel

public RulerModel getColumnModel()

setColumnModel

public void setColumnModel(RulerModel model)

gridChanged

public void gridChanged(GridModelEvent event)
Sync row and column sizes between models

Specified by:
gridChanged in interface GridModelListener

editCellAt

public boolean editCellAt(int row,
                          int column)

editCellAt

public boolean editCellAt(int row,
                          int column,
                          java.util.EventObject e)
Programmatically starts editing the cell at row and column, if the cell is editable.

Parameters:
row - the row to be edited
column - the column to be edited
e - event to pass into shouldSelectCell
Returns:
false if for any reason the cell cannot be edited
Throws:
java.lang.IllegalArgumentException - If row or column is not in the valid range

removeEditor

public void removeEditor()
Discards the editor object and frees the real estate it used for cell rendering.


setEditingColumn

public void setEditingColumn(int aColumn)
Sets the editingColumn variable.

Parameters:
aColumn - the column of the cell to be edited
See Also:
#editingColumn

setEditingRow

public void setEditingRow(int aRow)
Sets the editingRow variable.

Parameters:
aRow - the row of the cell to be edited
See Also:
#editingRow

isEditing

public boolean isEditing()
Returns true if a cell is being edited.

Returns:
true if the table is editing a cell
See Also:
#editingColumn, #editingRow

getEditorComponent

public java.awt.Component getEditorComponent()
Returns the component that is handling the editing session. If nothing is being edited, returns null.

Returns:
Component handling editing session

getEditingColumn

public int getEditingColumn()
Returns the index of the column that contains the cell currently being edited. If nothing is being edited, returns -1.

Returns:
the index of the column that contains the cell currently being edited; returns -1 if nothing being edited
See Also:
#editingRow

getEditingRow

public int getEditingRow()
Returns the index of the row that contains the cell currently being edited. If nothing is being edited, returns -1.

Returns:
the index of the row that contains the cell currently being edited; returns -1 if nothing being edited
See Also:
#editingColumn

getCurrentCellEditor

public GridCellEditor getCurrentCellEditor()
Return the current cell editor


editingStopped

public void editingStopped(javax.swing.event.ChangeEvent e)
Invoked when editing is finished. The changes are saved and the editor is discarded.

Application code will not use these methods explicitly, they are used internally by JSpread.

Specified by:
editingStopped in interface javax.swing.event.CellEditorListener
Parameters:
e - the event received
See Also:
CellEditorListener

editingCanceled

public void editingCanceled(javax.swing.event.ChangeEvent e)
Invoked when editing is canceled. The editor object is discarded and the cell is rendered once again. Application code will not use these methods explicitly, they are used internally by JSpread.

Specified by:
editingCanceled in interface javax.swing.event.CellEditorListener
Parameters:
e - the event received
See Also:
CellEditorListener

setPreferredScrollableViewportSize

public void setPreferredScrollableViewportSize(java.awt.Dimension size)
Sets the preferred size of the viewport for this table.

Parameters:
size - a Dimension object specifying the preferredSize of a JViewport whose view is this spreadsheet
See Also:
Scrollable.getPreferredScrollableViewportSize()

getPreferredScrollableViewportSize

public java.awt.Dimension getPreferredScrollableViewportSize()
Returns the preferred size of the viewport for this table.

Specified by:
getPreferredScrollableViewportSize in interface javax.swing.Scrollable
Returns:
a Dimension object containing the preferredSize of the JViewport which displays this table
See Also:
Scrollable.getPreferredScrollableViewportSize()

getScrollableUnitIncrement

public int getScrollableUnitIncrement(java.awt.Rectangle visibleRect,
                                      int orientation,
                                      int direction)
Returns the scroll increment (in pixels) that completely exposes one new row or column (depending on the orientation). This method is called each time the user requests a unit scroll.

Specified by:
getScrollableUnitIncrement in interface javax.swing.Scrollable
Parameters:
visibleRect - the view area visible within the viewport
orientation - either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL
direction - less than zero to scroll up/left, greater than zero for down/right
Returns:
the "unit" increment for scrolling in the specified direction
See Also:
Scrollable.getScrollableUnitIncrement(java.awt.Rectangle, int, int)

getScrollableBlockIncrement

public int getScrollableBlockIncrement(java.awt.Rectangle visibleRect,
                                       int orientation,
                                       int direction)
Returns visibleRect.height or visibleRect.width, depending on this spreadsheet's orientation.

Specified by:
getScrollableBlockIncrement in interface javax.swing.Scrollable
Returns:
visibleRect.height or visibleRect.width per the orientation
See Also:
Scrollable.getScrollableBlockIncrement(java.awt.Rectangle, int, int)

getScrollableTracksViewportWidth

public boolean getScrollableTracksViewportWidth()
Returns false to indicate that the width of the viewport does not determine the width of the spreadsheet.

Specified by:
getScrollableTracksViewportWidth in interface javax.swing.Scrollable
Returns:
false
See Also:
Scrollable.getScrollableTracksViewportWidth()

getScrollableTracksViewportHeight

public boolean getScrollableTracksViewportHeight()
Returns false to indicate that the height of the viewport does not determine the height of the spreadsheet.

Specified by:
getScrollableTracksViewportHeight in interface javax.swing.Scrollable
Returns:
false
See Also:
Scrollable.getScrollableTracksViewportHeight()

getUI

public GridUI getUI()
Returns the L&F object that renders this component.

Returns:
SpreadsheetUI object

setUI

public void setUI(GridUI ui)
Sets the L&F object that renders this component.

Parameters:
ui - the SpreadsheetUI L&F object
See Also:
UIDefaults.getUI(javax.swing.JComponent)

updateUI

public void updateUI()
Notification from the UIFactory that the L&F has changed.

See Also:
JComponent.updateUI()

getUIClassID

public java.lang.String getUIClassID()
Returns a string that specifies the name of the l&f class that renders this component.

Returns:
String "SpreadsheetUI"
See Also:
JComponent.getUIClassID(), UIDefaults.getUI(javax.swing.JComponent)

resizeAndRepaint

public void resizeAndRepaint()

getValueAt

public java.lang.Object getValueAt(int row,
                                   int column)

setValueAt

public void setValueAt(java.lang.Object value,
                       int row,
                       int column)