com.guigarage.jgrid
Class JGrid

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by com.guigarage.jgrid.JGrid
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Scrollable, SwingConstants

public class JGrid
extends JComponent
implements Scrollable, SwingConstants

A Component that displays a list of Elements in a grid. The Elements stored is a separate model, ListModel. So you can use a JList parallel to to the JGrid

Since:
0.1
Version:
0.1
Author:
Hendrik Ebbers
See Also:
JList, Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
 
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 javax.swing.SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
JGrid()
          Constructs a JGrid with a DefaultListModel.
JGrid(ListModel model)
          Constructs a JGrid that displays elements from the specified, non-null, model.
 
Method Summary
 void addListDataListener(ListDataListener l)
          Adds a listener to the JGrid, to be notified each time a change to the data occurs.
 void addListSelectionListener(ListSelectionListener l)
          Adds a listener to the JGrid, to be notified each time a change to the selection occurs.
 void addRendererMapping(Class<?> cls, GridCellRenderer renderer)
          Adds a renderer to the handler.
protected  ListSelectionModel createDefaultSelectionModel()
          Returns an default instance of ListSelectionModel; called during construction to initialize the grids selectionModel.
 int getCellAt(Point point)
          Returns the index of the cell at the given point.
 Color getCellBackground()
          Getter for the default cellbackground.
 Rectangle getCellBounds(int index)
          Returns the bounds inside the grid for the cell at index
 GridCellRenderer getCellRenderer(int index)
          Returns the for a specific cell
 GridCellRendererManager getCellRendererManager()
          Return the GridCellRendererManager of the JGrid
 int getColumnForIndex(int index)
          Returns the index of the column where modelIndex is in
 GridCellRenderer getDefaultRenderer()
          Returns the defaultRenderer.
 int getFixedCellDimension()
          Returns the Dimension of a cell.
 int getHorizontalAlignment()
          Getter for the horizontal alignment.
 int getHorizonztalMargin()
          Returns the horizonztalMargin.
 int getIndexAt(int row, int column)
          Returns the model-index of the cell at row / column
 int getLeadSelectionIndex()
          Returns the leadSelectionIndex from the ListSelectionModel
 ListModel getModel()
          Return the Model of the JGrid
 Dimension getPreferredScrollableViewportSize()
           
 int getRowForIndex(int index)
          Returns the index of the row where modelIndex is in
 int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
           
 boolean getScrollableTracksViewportHeight()
           
 boolean getScrollableTracksViewportWidth()
           
 int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
           
 int getSelectedIndex()
          Returns the selected Index from the ListSelectionModel
 Color getSelectionBackground()
          Getter for the selectionBackground.
 Color getSelectionBorderColor()
          Getter for the selectionBorderColor.
 Color getSelectionForeground()
          Getter for the selectionForeground.
 ListSelectionModel getSelectionModel()
          Returns the ListSelectionModel
 String getToolTipText(MouseEvent event)
           
 GridUI getUI()
          Returns the UI-Class for this JComponent.
 String getUIClassID()
           
 int getVerticalMargin()
          Returns the verticalMargin.
 void removeListDataListener(ListDataListener l)
          Removes a listener from the JGrid.
 void removeListSelectionListener(ListSelectionListener l)
          Removes a listener from the JGrid.
protected  void revalidateAndRepaint()
          Calls revalidate() and repaint()
 void setCellBackground(Color cellBackground)
          Setter for the default cellbackground.
 void setCellRendererManager(GridCellRendererManager cellRendererManager)
          Sets the GridCellRendererManager of the JGrid
 void setDefaultRenderer(GridCellRenderer defaultRenderer)
          Set the defaultRenderer.
 void setFixedCellDimension(int dimension)
          Sets the rendering dimension for all elements in the grid.
 void setHorizontalAlignment(int alignment)
          Setter for the horizontal alignment.
 void setHorizonztalMargin(int horizonztalMargin)
          Sets the horizonztal margin between all elements in the grid.
 void setModel(ListModel model)
          Sets the model that represents values of the JGrid
 void setSelectedIndex(int index)
          Sets the index of the selected cell
 void setSelectionBackground(Color selectionBackground)
          Setter for the selectionBackground.
 void setSelectionBorderColor(Color selectionBorderColor)
          Setter for the selectionBorderColor.
 void setSelectionForeground(Color selectionForeground)
          Setter for the selectionForeground.
 void setSelectionModel(ListSelectionModel selectionModel)
          Sets the ListSelectionModel of the JGrid
 void setUI(BasicGridUI ui)
          Setter for the UIClass
 void setVerticalMargin(int verticalMargin)
          Sets the vertical margin between all elements in the grid.
 void updateUI()
           
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, 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, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, 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, doLayout, 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

JGrid

public JGrid()
Constructs a JGrid with a DefaultListModel.

Parameters:
model - the model for the grid
Throws:
IllegalArgumentException - if the model is null
Since:
0.1

JGrid

public JGrid(ListModel model)
      throws IllegalArgumentException
Constructs a JGrid that displays elements from the specified, non-null, model. All JGrid constructors must delegate to this one.

Parameters:
model - the model for the grid
Throws:
IllegalArgumentException - if the model is null
Since:
0.1
Method Detail

addListSelectionListener

public void addListSelectionListener(ListSelectionListener l)
Adds a listener to the JGrid, to be notified each time a change to the selection occurs. This Method uses a ListSelectionProxy. Every time the underlying ListSelectionModel changes all listeners will be registered to the new model and deregistered from the old one. For adding listeneres to the model-scope (direct to the model) use ListSelectionListener.addListSelectionListener

Parameters:
listener - the ListSelectionListener to add
See Also:
removeListSelectionListener(javax.swing.event.ListSelectionListener)

removeListSelectionListener

public void removeListSelectionListener(ListSelectionListener l)
Removes a listener from the JGrid.

Parameters:
listener - the ListSelectionListener to remove
See Also:
addListSelectionListener(javax.swing.event.ListSelectionListener)

addListDataListener

public void addListDataListener(ListDataListener l)
Adds a listener to the JGrid, to be notified each time a change to the data occurs. This Method uses a ListDataProxy. Every time the underlying ListModel changes all listeners will be registered to the new model and deregistered from the old one. For adding listeneres to the model-scope (direct to the model) use ListModel.addListDataListener

Parameters:
listener - the ListSelectionListener to add
See Also:
removeListDataListener(javax.swing.event.ListDataListener)

removeListDataListener

public void removeListDataListener(ListDataListener l)
Removes a listener from the JGrid.

Parameters:
listener - the ListDataListener to remove
See Also:
addListDataListener(javax.swing.event.ListDataListener)

setSelectionModel

public void setSelectionModel(ListSelectionModel selectionModel)
Sets the ListSelectionModel of the JGrid

Parameters:
selectionModel - the new ListSelectionModel
Throws:
IllegalArgumentException - if the model is null
See Also:
getSelectionModel()

setCellRendererManager

public void setCellRendererManager(GridCellRendererManager cellRendererManager)
                            throws IllegalArgumentException
Sets the GridCellRendererManager of the JGrid

Parameters:
cellRendererManager - the new GridCellRendererManager
Throws:
IllegalArgumentException - if the cellRendererManager is null
Since:
0.3
See Also:
GridCellRendererManager, getCellRendererManager()

getCellRendererManager

public GridCellRendererManager getCellRendererManager()
Return the GridCellRendererManager of the JGrid

Returns:
The GridCellRendererManager of the JGrid
Since:
0.3
See Also:
GridCellRendererManager

setModel

public void setModel(ListModel model)
              throws IllegalArgumentException
Sets the model that represents values of the JGrid

Parameters:
model - the new ListModel
Throws:
IllegalArgumentException - if the model is null
See Also:
getModel()

revalidateAndRepaint

protected void revalidateAndRepaint()
Calls revalidate() and repaint()


createDefaultSelectionModel

protected ListSelectionModel createDefaultSelectionModel()
Returns an default instance of ListSelectionModel; called during construction to initialize the grids selectionModel. Normally this returns a DefaultListSelectionModel

Returns:
a default ListSelectionModel
Since:
0.1
See Also:
JList

getModel

public ListModel getModel()
Return the Model of the JGrid

Returns:
The Model of the JGrid
Since:
0.1
See Also:
JList

getSelectionModel

public ListSelectionModel getSelectionModel()
Returns the ListSelectionModel

Returns:
the ListSelectionModel
Since:
0.1
See Also:
JList

getSelectedIndex

public int getSelectedIndex()
Returns the selected Index from the ListSelectionModel

Returns:
the selected Index
Since:
0.1
See Also:
ListSelectionModel

getLeadSelectionIndex

public int getLeadSelectionIndex()
Returns the leadSelectionIndex from the ListSelectionModel

Returns:
the leadSelectionIndex
Since:
0.1
See Also:
ListSelectionModel

getUI

public GridUI getUI()
Returns the UI-Class for this JComponent. This is always a GridUI

Returns:
the UIClass
Since:
0.1

updateUI

public void updateUI()
Overrides:
updateUI in class JComponent

setFixedCellDimension

public void setFixedCellDimension(int dimension)
Sets the rendering dimension for all elements in the grid. The width and height of each element is equals the dimenison. Fires PropertyChangeEvent with the fixedCellDimension propertyname

Parameters:
dimension - the new dimension for this Grid
Since:
0.1

setVerticalMargin

public void setVerticalMargin(int verticalMargin)
Sets the vertical margin between all elements in the grid. Fires PropertyChangeEvent with the verticalMargin propertyname

Parameters:
verticalMargin - the vertical margin for this Grid
Since:
0.1

setHorizonztalMargin

public void setHorizonztalMargin(int horizonztalMargin)
Sets the horizonztal margin between all elements in the grid. Fires PropertyChangeEvent with the horizonztalMargin propertyname

Parameters:
horizonztalMargin - the horizonztal margin for this Grid
Since:
0.1

getFixedCellDimension

public int getFixedCellDimension()
Returns the Dimension of a cell. Each cell in the Grid has a width and height equals the fixedCellDimension

Returns:
the fixedCellDimension
Since:
0.1

getHorizonztalMargin

public int getHorizonztalMargin()
Returns the horizonztalMargin. The horizonztalMargin is the horizontal space between two cells

Returns:
the horizonztalMargin
Since:
0.1

getVerticalMargin

public int getVerticalMargin()
Returns the verticalMargin. The verticalMargin is the vertical space between two cells

Returns:
the verticalMargin
Since:
0.1

getUIClassID

public String getUIClassID()
Overrides:
getUIClassID in class JComponent

setUI

public void setUI(BasicGridUI ui)
Setter for the UIClass

Parameters:
ui - the new UI
Since:
0.1

getPreferredScrollableViewportSize

public Dimension getPreferredScrollableViewportSize()
Specified by:
getPreferredScrollableViewportSize in interface Scrollable

getScrollableBlockIncrement

public int getScrollableBlockIncrement(Rectangle visibleRect,
                                       int orientation,
                                       int direction)
Specified by:
getScrollableBlockIncrement in interface Scrollable

getScrollableTracksViewportHeight

public boolean getScrollableTracksViewportHeight()
Specified by:
getScrollableTracksViewportHeight in interface Scrollable

getScrollableTracksViewportWidth

public boolean getScrollableTracksViewportWidth()
Specified by:
getScrollableTracksViewportWidth in interface Scrollable

getScrollableUnitIncrement

public int getScrollableUnitIncrement(Rectangle visibleRect,
                                      int orientation,
                                      int direction)
Specified by:
getScrollableUnitIncrement in interface Scrollable

getSelectionForeground

public Color getSelectionForeground()
Getter for the selectionForeground. Each selected Cell paints the foreground in this Color. That means the foreground-Property in the renderer is set to the selectionForeground.

Returns:
the selectionForeground
Since:
0.1

setSelectionForeground

public void setSelectionForeground(Color selectionForeground)
Setter for the selectionForeground. Each selected Cell paints the foreground in this Color. That means the foreground-Property in the renderer is set to the selectionForeground. Fires PropertyChangeEvent with the selectionForeground propertyname

Parameters:
selectionForeground - the new selctionForeground
Since:
0.1

setSelectionBorderColor

public void setSelectionBorderColor(Color selectionBorderColor)
Setter for the selectionBorderColor. The UIClass can use this for borderpainting. Fires PropertyChangeEvent with the selectionBorderColor propertyname

Parameters:
selectionForeground - the new selctionForeground
Since:
0.1

getSelectionBorderColor

public Color getSelectionBorderColor()
Getter for the selectionBorderColor. The UIClass can use this for borderpainting.

Returns:
the selectionBorderColor
Since:
0.1

getSelectionBackground

public Color getSelectionBackground()
Getter for the selectionBackground. Each selected Cell paints the background in this Color. That means the background-Property in the renderer is set to the selectionbackground.

Returns:
the selectionBackground
Since:
0.1

setSelectionBackground

public void setSelectionBackground(Color selectionBackground)
Setter for the selectionBackground. Each selected Cell paints the background in this Color. That means the background-Property in the renderer is set to the selectionbackground. Fires PropertyChangeEvent with the selectionBackground propertyname

Parameters:
selectionBackground - the new selectionBackground
Since:
0.1

getCellBackground

public Color getCellBackground()
Getter for the default cellbackground. Each Cell paints the background in this Color. That means the background-Property in the renderer is set to the cellBackground.

Returns:
the default cellbackground
Since:
0.1

setCellBackground

public void setCellBackground(Color cellBackground)
Setter for the default cellbackground. Each Cell paints the background in this Color. That means the background-Property in the renderer is set to the cellBackground. Fires PropertyChangeEvent with the cellBackground propertyname

Parameters:
cellBackground - the new default cellbackground
Since:
0.1

setSelectedIndex

public void setSelectedIndex(int index)
Sets the index of the selected cell

Parameters:
index - the index of the selected cell
Since:
0.1

getCellBounds

public Rectangle getCellBounds(int index)
Returns the bounds inside the grid for the cell at index

Parameters:
index - the index of the cell
Returns:
the cellbounds
Since:
0.1

getCellAt

public int getCellAt(Point point)
Returns the index of the cell at the given point. Returns -1 if no cell is at this point

Parameters:
point - the pint in the grid
Returns:
the index of the cell at the point
Since:
0.1
See Also:
BasicGridUI

getToolTipText

public String getToolTipText(MouseEvent event)
Overrides:
getToolTipText in class JComponent

getCellRenderer

public GridCellRenderer getCellRenderer(int index)
Returns the for a specific cell

Parameters:
index - the index of the cell
Returns:
the
Since:
0.3
See Also:
getCellRendererManager(), setCellRendererManager(GridCellRendererManager), GridCellRendererManager

getHorizontalAlignment

public int getHorizontalAlignment()
Getter for the horizontal alignment. LEFT / CENTER / RIGHT / LEADING & TRAILING allowed

Returns:
the horizontal alignment
Since:
0.1

setHorizontalAlignment

public void setHorizontalAlignment(int alignment)
Setter for the horizontal alignment. LEFT / CENTER / RIGHT / LEADING & TRAILING allowed Fires PropertyChangeEvent with the horizontalAlignment propertyname

Parameters:
alignment - the new horizontal alignment
Throws:
IllegalArgumentException - if alignment not LEFT / CENTER / RIGHT / LEADING & TRAILING
Since:
0.1

getIndexAt

public int getIndexAt(int row,
                      int column)
Returns the model-index of the cell at row / column

Parameters:
row - the row of the cell
column - the column of the cell
Returns:
model-index of the cell

getColumnForIndex

public int getColumnForIndex(int index)
Returns the index of the column where modelIndex is in

Parameters:
selectedIndex - the model-index
Returns:
index of the column

getRowForIndex

public int getRowForIndex(int index)
Returns the index of the row where modelIndex is in

Parameters:
selectedIndex - the model-index
Returns:
index of the row

setDefaultRenderer

public void setDefaultRenderer(GridCellRenderer defaultRenderer)
Set the defaultRenderer. If no renderer is registered for a specific class the defaultRenderer will be used.

Parameters:
defaultRenderer - the new defaultRenderer

getDefaultRenderer

public GridCellRenderer getDefaultRenderer()
Returns the defaultRenderer. If no renderer is registered for a specific class the defaultRenderer will be used.

Returns:
the defaultRenderer

addRendererMapping

public void addRendererMapping(Class<?> cls,
                               GridCellRenderer renderer)
Adds a renderer to the handler. The renderer is the default renderer for the cellClass cls

Parameters:
cls - set the renderer for this class
renderer - the renderer for all instances of cls


Copyright © 2012. All Rights Reserved.