net.sf.jga.swing.spreadsheet
Class Spreadsheet

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JTable
                  extended by net.sf.jga.swing.spreadsheet.Spreadsheet
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible, javax.swing.event.CellEditorListener, javax.swing.event.ListSelectionListener, javax.swing.event.RowSorterListener, javax.swing.event.TableColumnModelListener, javax.swing.event.TableModelListener, javax.swing.Scrollable

public class Spreadsheet
extends javax.swing.JTable

Table that contains a sparse-matrix of functors (Generators, specifically). Each cell in the table can contain a formula, and the table can generate references to cells to be used in formulas in other cells. Cells in the matrix can contain constant values and arbitrary Generators, but to support editing, the calling program must pass an expression that can be parsed via a JFXGParser.

Author:
David A. Hall
See Also:

Copyright © 2004-2005 David A. Hall, Serialized Form


Nested Class Summary
 class Spreadsheet.Reader
           
 class Spreadsheet.Writer
           
 
Nested classes/interfaces inherited from class javax.swing.JTable
javax.swing.JTable.AccessibleJTable, javax.swing.JTable.DropLocation, javax.swing.JTable.PrintMode
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
 
Fields inherited from class javax.swing.JTable
AUTO_RESIZE_ALL_COLUMNS, AUTO_RESIZE_LAST_COLUMN, AUTO_RESIZE_NEXT_COLUMN, AUTO_RESIZE_OFF, AUTO_RESIZE_SUBSEQUENT_COLUMNS, autoCreateColumnsFromModel, autoResizeMode, cellEditor, cellSelectionEnabled, columnModel, dataModel, defaultEditorsByColumnClass, defaultRenderersByColumnClass, editingColumn, editingRow, editorComp, gridColor, preferredViewportSize, rowHeight, rowMargin, rowSelectionAllowed, selectionBackground, selectionForeground, selectionModel, showHorizontalLines, showVerticalLines, tableHeader
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, 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
Spreadsheet(int rows, int cols)
           
 
Method Summary
 void clear()
           
 void clearCellAt(int row, int col)
          Discards all information about the given cell.
 void columnSelectionChanged(javax.swing.event.ListSelectionEvent e)
           
protected  void configureEnclosingScrollPane()
          In addition to the JTable behaviour of this method (which takes care to add the table's columnheader to an enclosing scrollpane's viewport), add a rowheader to the enclosing scrollpane's viewport as well.
 Cell getCellAt(int row, int col)
          Returns the cell at the given address, creating one if one does not already exist.
 Cell getCellByName(java.lang.String name)
          Returns the cell with the given name, or null if no cell has the given name.
 javax.swing.table.TableCellEditor getCellEditor(int row, int col)
           
 javax.swing.table.TableCellRenderer getCellRenderer(int row, int col)
           
 java.lang.Class<?> getDefaultCellType()
          Returns the type of cells that have not be initialized.
 java.lang.Object getDefaultCellValue()
          Returns the default value of cells that have not been initialized.
 FunctorParser getParser()
          Returns the parser used by the spreadsheet.
<T> Generator<T>
getReference(java.lang.Class<T> type, int row, int col)
          Returns a reference to the contents of a given cell
 javax.swing.JComponent getRowHeader()
          Returns the component used as a row header
 boolean isEditableByDefault()
          Returns true if empty and newly created cells are editable.
 boolean isStrictlyTyped()
          Returns true if cells strictly enforce types once a type has been set, or if they allow their types to be changed once set.
 void readSpreadsheet(java.io.InputStream is)
           
<T> Cell
setCellAt(java.lang.Class<T> type, Generator<T> gen, int row, int col)
          Builds a cell to hold the given value
<T> Cell
setCellAt(java.lang.Class<T> type, T value, int row, int col)
          Builds a read-only cell to hold the given value.
 Cell setCellAt(java.lang.String formula, int row, int col)
          Builds a possibly editable cell to hold the given formula.
 Cell setCellAt(java.lang.String formula, int row, int col, boolean editable)
          Builds a possibly editable cell to hold the given formula.
 Cell setCellName(java.lang.String name, int row, int col)
          Sets the name of the given cell.
 void setColumnCount(int width)
           
 void setDefaultCellType(java.lang.Class<?> type)
          Sets the type returned by cells that have not been initialized.
 void setDefaultCellValue(java.lang.Object value)
          Sets the value returned by cells that have not been initialized.
 void setEditableByDefault(boolean b)
          Sets the option that determines if empty cells and newly created cells are editable.
<T> void
setFormatAt(UnaryFunctor<T,java.lang.String> formatter, int row, int col)
          Replaces the CellRenderer for the given cell such that the given formatting functor is used to present the contents of the cell.
 void setModel(javax.swing.table.TableModel model)
           
 void setRowCount(int height)
           
 void setStatus(java.lang.String status)
          Updates the spreadsheet's status message.
 void setStatusHandler(UnaryFunctor<java.lang.String,?> fn)
          Sets the functor used by the spreadsheet to display status information.
 void setStrictTyping(boolean b)
          Sets the option that controls whether cells will be strongly or weakly typed.
 void setUpdateHandler(Generator<?> fn)
           
 void valueChanged(javax.swing.event.ListSelectionEvent e)
           
 void writeSpreadsheet(java.io.OutputStream os)
           
 
Methods inherited from class javax.swing.JTable
addColumn, addColumnSelectionInterval, addNotify, addRowSelectionInterval, changeSelection, clearSelection, columnAdded, columnAtPoint, columnMarginChanged, columnMoved, columnRemoved, convertColumnIndexToModel, convertColumnIndexToView, convertRowIndexToModel, convertRowIndexToView, createDefaultColumnModel, createDefaultColumnsFromModel, createDefaultDataModel, createDefaultEditors, createDefaultRenderers, createDefaultSelectionModel, createDefaultTableHeader, createScrollPaneForTable, doLayout, editCellAt, editCellAt, editingCanceled, editingStopped, getAccessibleContext, getAutoCreateColumnsFromModel, getAutoCreateRowSorter, getAutoResizeMode, getCellEditor, getCellRect, getCellSelectionEnabled, getColumn, getColumnClass, getColumnCount, getColumnModel, getColumnName, getColumnSelectionAllowed, getDefaultEditor, getDefaultRenderer, getDragEnabled, getDropLocation, getDropMode, getEditingColumn, getEditingRow, getEditorComponent, getFillsViewportHeight, getGridColor, getIntercellSpacing, getModel, getPreferredScrollableViewportSize, getPrintable, getRowCount, getRowHeight, getRowHeight, getRowMargin, getRowSelectionAllowed, getRowSorter, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getScrollableUnitIncrement, getSelectedColumn, getSelectedColumnCount, getSelectedColumns, getSelectedRow, getSelectedRowCount, getSelectedRows, getSelectionBackground, getSelectionForeground, getSelectionModel, getShowHorizontalLines, getShowVerticalLines, getSurrendersFocusOnKeystroke, getTableHeader, getToolTipText, getUI, getUIClassID, getUpdateSelectionOnSort, getValueAt, initializeLocalVars, isCellEditable, isCellSelected, isColumnSelected, isEditing, isRowSelected, moveColumn, paramString, prepareEditor, prepareRenderer, print, print, print, print, print, processKeyBinding, removeColumn, removeColumnSelectionInterval, removeEditor, removeNotify, removeRowSelectionInterval, resizeAndRepaint, rowAtPoint, selectAll, setAutoCreateColumnsFromModel, setAutoCreateRowSorter, setAutoResizeMode, setCellEditor, setCellSelectionEnabled, setColumnModel, setColumnSelectionAllowed, setColumnSelectionInterval, setDefaultEditor, setDefaultRenderer, setDragEnabled, setDropMode, setEditingColumn, setEditingRow, setFillsViewportHeight, setGridColor, setIntercellSpacing, setPreferredScrollableViewportSize, setRowHeight, setRowHeight, setRowMargin, setRowSelectionAllowed, setRowSelectionInterval, setRowSorter, setSelectionBackground, setSelectionForeground, setSelectionMode, setSelectionModel, setShowGrid, setShowHorizontalLines, setShowVerticalLines, setSurrendersFocusOnKeystroke, setTableHeader, setUI, setUpdateSelectionOnSort, setValueAt, sizeColumnsToFit, sizeColumnsToFit, sorterChanged, tableChanged, unconfigureEnclosingScrollPane, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, 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
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Spreadsheet

public Spreadsheet(int rows,
                   int cols)
Method Detail

setColumnCount

public void setColumnCount(int width)

setRowCount

public void setRowCount(int height)

getParser

public FunctorParser getParser()
Returns the parser used by the spreadsheet.


getDefaultCellType

public java.lang.Class<?> getDefaultCellType()
Returns the type of cells that have not be initialized.


setDefaultCellType

public void setDefaultCellType(java.lang.Class<?> type)
Sets the type returned by cells that have not been initialized. By default, this value is java.lang.Integer. If the default value is non-null and is not an instance of the given type, then the default value will be set to null as a side-effect of setting the type.


getDefaultCellValue

public java.lang.Object getDefaultCellValue()
Returns the default value of cells that have not been initialized.


setDefaultCellValue

public void setDefaultCellValue(java.lang.Object value)
Sets the value returned by cells that have not been initialized. By default, this value is a java.lang.Integer.ZERO. When called, if the new value is not an instance of the existing default type, then the default type will be changed to the class of the new value as a side-effect.


setEditableByDefault

public void setEditableByDefault(boolean b)
Sets the option that determines if empty cells and newly created cells are editable.


isEditableByDefault

public boolean isEditableByDefault()
Returns true if empty and newly created cells are editable.


isStrictlyTyped

public boolean isStrictlyTyped()
Returns true if cells strictly enforce types once a type has been set, or if they allow their types to be changed once set.


setStrictTyping

public void setStrictTyping(boolean b)
Sets the option that controls whether cells will be strongly or weakly typed. Strongly typed cells will not change their types after being set: a new formula must return the same type as the type that the cell has been assigned. Weakly typed cells can have their types changed at any time. By default, cells are weakly typed.


readSpreadsheet

public void readSpreadsheet(java.io.InputStream is)
                     throws java.io.IOException
Throws:
java.io.IOException

writeSpreadsheet

public void writeSpreadsheet(java.io.OutputStream os)
                      throws java.io.IOException
Throws:
java.io.IOException

getRowHeader

public javax.swing.JComponent getRowHeader()
Returns the component used as a row header


setStatusHandler

public void setStatusHandler(UnaryFunctor<java.lang.String,?> fn)
Sets the functor used by the spreadsheet to display status information. This allows the spreadsheet's container to (for example) route status information from the spreadsheet to a log file, or to a status bar.


setStatus

public void setStatus(java.lang.String status)
Updates the spreadsheet's status message.


setUpdateHandler

public void setUpdateHandler(Generator<?> fn)

clearCellAt

public void clearCellAt(int row,
                        int col)
Discards all information about the given cell.


getCellAt

public Cell getCellAt(int row,
                      int col)
Returns the cell at the given address, creating one if one does not already exist.

Throws:
java.lang.IndexOutOfBoundsException - if the row or column is out of bounds

setCellAt

public <T> Cell setCellAt(java.lang.Class<T> type,
                          T value,
                          int row,
                          int col)
Builds a read-only cell to hold the given value. If you need the cell to be editable

Throws:
java.lang.IndexOutOfBoundsException - if the row or column is out of bounds

setCellAt

public <T> Cell setCellAt(java.lang.Class<T> type,
                          Generator<T> gen,
                          int row,
                          int col)
Builds a cell to hold the given value

Throws:
java.lang.IndexOutOfBoundsException - if the row or column is out of bounds

setCellAt

public Cell setCellAt(java.lang.String formula,
                      int row,
                      int col)
Builds a possibly editable cell to hold the given formula.

Throws:
java.lang.IndexOutOfBoundsException - if the row or column is out of bounds

setCellAt

public Cell setCellAt(java.lang.String formula,
                      int row,
                      int col,
                      boolean editable)
Builds a possibly editable cell to hold the given formula.

Throws:
java.lang.IndexOutOfBoundsException - if the row or column is out of bounds

setFormatAt

public <T> void setFormatAt(UnaryFunctor<T,java.lang.String> formatter,
                            int row,
                            int col)
Replaces the CellRenderer for the given cell such that the given formatting functor is used to present the contents of the cell. NOTE: Use this method cautiously, as passing a formatter of an inappropriate type can lead to a class cast exception or to the Cell reporting '### CLASS ###' condition.


getCellByName

public Cell getCellByName(java.lang.String name)
Returns the cell with the given name, or null if no cell has the given name.


setCellName

public Cell setCellName(java.lang.String name,
                        int row,
                        int col)
Sets the name of the given cell.

Throws:
java.lang.IllegalArgumentException - if the name is already in use.

getReference

public <T> Generator<T> getReference(java.lang.Class<T> type,
                                     int row,
                                     int col)
Returns a reference to the contents of a given cell


clear

public void clear()

getCellEditor

public javax.swing.table.TableCellEditor getCellEditor(int row,
                                                       int col)
Overrides:
getCellEditor in class javax.swing.JTable

getCellRenderer

public javax.swing.table.TableCellRenderer getCellRenderer(int row,
                                                           int col)
Overrides:
getCellRenderer in class javax.swing.JTable

setModel

public void setModel(javax.swing.table.TableModel model)
Overrides:
setModel in class javax.swing.JTable

configureEnclosingScrollPane

protected void configureEnclosingScrollPane()
In addition to the JTable behaviour of this method (which takes care to add the table's columnheader to an enclosing scrollpane's viewport), add a rowheader to the enclosing scrollpane's viewport as well.

Overrides:
configureEnclosingScrollPane in class javax.swing.JTable

valueChanged

public void valueChanged(javax.swing.event.ListSelectionEvent e)
Specified by:
valueChanged in interface javax.swing.event.ListSelectionListener
Overrides:
valueChanged in class javax.swing.JTable

columnSelectionChanged

public void columnSelectionChanged(javax.swing.event.ListSelectionEvent e)
Specified by:
columnSelectionChanged in interface javax.swing.event.TableColumnModelListener
Overrides:
columnSelectionChanged in class javax.swing.JTable


Copyright © 2002-2006 David A. Hall. All Rights Reserved.