org.jdesktop.swingx.plaf.basic.core
Class BasicXListUI

java.lang.Object
  extended by javax.swing.plaf.ComponentUI
      extended by javax.swing.plaf.ListUI
          extended by javax.swing.plaf.basic.BasicListUI
              extended by org.jdesktop.swingx.plaf.basic.core.BasicXListUI
Direct Known Subclasses:
SynthXListUI

public class BasicXListUI
extends javax.swing.plaf.basic.BasicListUI

An extensible implementation of ListUI for JXList. BasicXListUI instances cannot be shared between multiple lists.

The heart of added functionality is to support sorting/filtering, that is keep model-selection and RowSorter state synchronized. The details are delegated to a ListSortUI, but this class is responsible to manage the sortUI on changes of list properties, model and view selection (same strategy as in JXTable).

Note: this delegate is mostly a 1:1 copy of BasicListUI. The difference is that it accesses the list elements and list elementCount exclusively through the JXList api. This allows a clean implementation of sorting/filtering.

The differences (goal was to touch as little code as possible as this needs to be updated on every change to core until that is changed to not access the list's model directly, sigh) for core functionality:

Differences to achieve extended functionality: changed createListSelectionListener to return a ListSelectionHandler Note: extension of core (instead of implement from scratch) is to keep external (?) code working which expects a ui delegate of type BasicSomething. LAF implementors with a custom ListUI extending BasicListUI should be able to add support for JXList by adding a separate CustomXListUI extending this, same as the default with parent changed. Beware: custom code must not call access the model directly or - if they insist - convert the row index to account for sorting/filtering! That's the whole point of this class.

Version:
1.127 12/02/08
Author:
Hans Muller, Philip Milne, Shannon Hickey (drag and drop)

Nested Class Summary
 class BasicXListUI.FocusHandler
          This inner class is marked "public" due to a compiler bug.
 class BasicXListUI.ListDataHandler
          The ListDataListener that's added to the JLists model at installUI time, and whenever the JList.model property changes.
 class BasicXListUI.ListSelectionHandler
          The ListSelectionListener that's added to the JLists selection model at installUI time, and whenever the JList.selectionModel property changes.
 class BasicXListUI.MouseInputHandler
          Mouse input, and focus handling for JList.
 class BasicXListUI.PropertyChangeHandler
          The PropertyChangeListener that's added to the JList at installUI time.
 
Field Summary
protected  int cellHeight
           
protected  int[] cellHeights
           
protected static int cellRendererChanged
           
protected  int cellWidth
           
protected static int fixedCellHeightChanged
           
protected static int fixedCellWidthChanged
           
protected  java.awt.event.FocusListener focusListener
           
protected static int fontChanged
           
protected  JXList list
           
protected  javax.swing.event.ListDataListener listDataListener
           
protected  javax.swing.event.ListSelectionListener listSelectionListener
           
protected static int modelChanged
           
protected  javax.swing.event.MouseInputListener mouseInputListener
           
protected  java.beans.PropertyChangeListener propertyChangeListener
           
protected static int prototypeCellValueChanged
           
protected  javax.swing.CellRendererPane rendererPane
           
protected static int selectionModelChanged
           
protected  int updateLayoutStateNeeded
           
 
Constructor Summary
BasicXListUI()
           
 
Method Summary
protected  int convertRowToY(int row)
          Return the JList relative Y coordinate of the origin of the specified row or -1 if row isn't valid.
protected  int convertYToRow(int y0)
          Convert the JList relative coordinate to the row that contains it, based on the current layout.
protected  java.awt.event.FocusListener createFocusListener()
           
protected  javax.swing.event.ListDataListener createListDataListener()
          Creates an instance of ListDataListener that's added to the JLists by model as needed.
protected  javax.swing.event.ListSelectionListener createListSelectionListener()
          Creates an instance of ListSelectionHandler that's added to the JLists by selectionModel as needed.
protected  javax.swing.event.MouseInputListener createMouseInputListener()
          Creates a delegate that implements MouseInputListener.
protected  java.beans.PropertyChangeListener createPropertyChangeListener()
          Creates an instance of PropertyChangeHandler that's added to the JList by installUI().
static javax.swing.plaf.ComponentUI createUI(javax.swing.JComponent list)
          Returns a new instance of BasicXListUI.
 int getBaseline(javax.swing.JComponent c, int width, int height)
          Returns the baseline.
 java.awt.Component.BaselineResizeBehavior getBaselineResizeBehavior(javax.swing.JComponent c)
          Returns an enum indicating how the baseline of the component changes as the size changes.
 java.awt.Rectangle getCellBounds(javax.swing.JList list, int index1, int index2)
          
protected  java.lang.Object getElementAt(int viewIndex)
           
protected  int getElementCount()
           
 java.awt.Dimension getPreferredSize(javax.swing.JComponent c)
          The preferredSize of the list depends upon the layout orientation.
protected  int getRowHeight(int row)
          Returns the height of the specified row based on the current layout.
protected  ListSortUI getSortUI()
           
protected  javax.swing.ListModel getViewModel()
          Compatibility Wrapper: a synthetic model which delegates to list api and throws
 java.awt.Point indexToLocation(javax.swing.JList list, int index)
          
protected  void installDefaults()
          Initialize JList properties, e.g.
protected  void installKeyboardActions()
          Registers the keyboard bindings on the JList that the BasicXListUI is associated with.
protected  void installListeners()
          Create and install the listeners for the JList, its model, and its selectionModel.
protected  void installSortUI()
          Installs SortUI if the list has a rowSorter.
 void installUI(javax.swing.JComponent c)
          Initializes this.list by calling installDefaults(), installListeners(), and installKeyboardActions() in order.
 void invalidateCellSizeCache()
          Invalidates the cell size cache and revalidates/-paints the list.
static void loadActionMap(LazyActionMap map)
           
 int locationToIndex(javax.swing.JList list, java.awt.Point location)
          
protected  void maybeUpdateLayoutState()
          If updateLayoutStateNeeded is non zero, call updateLayoutState() and reset updateLayoutStateNeeded.
 void paint(java.awt.Graphics g, javax.swing.JComponent c)
          Paint the rows that intersect the Graphics objects clipRect.
protected  void paintCell(java.awt.Graphics g, int row, java.awt.Rectangle rowBounds, javax.swing.ListCellRenderer cellRenderer, javax.swing.ListModel dataModel, javax.swing.ListSelectionModel selModel, int leadIndex)
          Paint one List cell: compute the relevant state, get the "rubber stamp" cell renderer component, and then use the CellRendererPane to paint it.
protected  boolean processedBySortUI(javax.swing.event.ListDataEvent e)
          Returns a boolean indicating whether or not the event has been processed by the sortUI.
protected  boolean processedBySortUI(javax.swing.event.ListSelectionEvent e)
          Returns a boolean indicating whether or not the event has been processed by the sortUI.
protected  void selectNextIndex()
          Selected the previous row and force it to be visible.
protected  void selectPreviousIndex()
          Selected the previous row and force it to be visible.
protected  void uninstallDefaults()
          Set the JList properties that haven't been explicitly overridden to null.
protected  void uninstallKeyboardActions()
          Unregisters keyboard actions installed from installKeyboardActions.
protected  void uninstallListeners()
          Remove the listeners for the JList, its model, and its selectionModel.
protected  void uninstallSortUI()
          Dispose and null's the sortUI if installed.
 void uninstallUI(javax.swing.JComponent c)
          Uninitializes this.list by calling uninstallListeners(), uninstallKeyboardActions(), and uninstallDefaults() in order.
protected  void updateLayoutState()
          Recompute the value of cellHeight or cellHeights based and cellWidth, based on the current font and the current values of fixedCellWidth, fixedCellHeight, and prototypeCellValue.
protected  void updateSortUI(java.lang.String property)
          Called from the PropertyChangeHandler.
 
Methods inherited from class javax.swing.plaf.ComponentUI
contains, getAccessibleChild, getAccessibleChildrenCount, getMaximumSize, getMinimumSize, update
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

list

protected JXList list

rendererPane

protected javax.swing.CellRendererPane rendererPane

focusListener

protected java.awt.event.FocusListener focusListener

mouseInputListener

protected javax.swing.event.MouseInputListener mouseInputListener

listSelectionListener

protected javax.swing.event.ListSelectionListener listSelectionListener

listDataListener

protected javax.swing.event.ListDataListener listDataListener

propertyChangeListener

protected java.beans.PropertyChangeListener propertyChangeListener

cellHeights

protected int[] cellHeights

cellHeight

protected int cellHeight

cellWidth

protected int cellWidth

updateLayoutStateNeeded

protected int updateLayoutStateNeeded

modelChanged

protected static final int modelChanged
See Also:
Constant Field Values

selectionModelChanged

protected static final int selectionModelChanged
See Also:
Constant Field Values

fontChanged

protected static final int fontChanged
See Also:
Constant Field Values

fixedCellWidthChanged

protected static final int fixedCellWidthChanged
See Also:
Constant Field Values

fixedCellHeightChanged

protected static final int fixedCellHeightChanged
See Also:
Constant Field Values

prototypeCellValueChanged

protected static final int prototypeCellValueChanged
See Also:
Constant Field Values

cellRendererChanged

protected static final int cellRendererChanged
See Also:
Constant Field Values
Constructor Detail

BasicXListUI

public BasicXListUI()
Method Detail

loadActionMap

public static void loadActionMap(LazyActionMap map)

getViewModel

protected javax.swing.ListModel getViewModel()
Compatibility Wrapper: a synthetic model which delegates to list api and throws

Returns:

getElementCount

protected int getElementCount()
Returns:

getElementAt

protected java.lang.Object getElementAt(int viewIndex)

getSortUI

protected ListSortUI getSortUI()

installSortUI

protected void installSortUI()
Installs SortUI if the list has a rowSorter. Does nothing if not.


uninstallSortUI

protected void uninstallSortUI()
Dispose and null's the sortUI if installed. Does nothing if not.


updateSortUI

protected void updateSortUI(java.lang.String property)
Called from the PropertyChangeHandler.

Parameters:
property - the name of the changed property.

processedBySortUI

protected boolean processedBySortUI(javax.swing.event.ListDataEvent e)
Returns a boolean indicating whether or not the event has been processed by the sortUI.

Parameters:
e -
Returns:

processedBySortUI

protected boolean processedBySortUI(javax.swing.event.ListSelectionEvent e)
Returns a boolean indicating whether or not the event has been processed by the sortUI.

Parameters:
e -
Returns:

invalidateCellSizeCache

public void invalidateCellSizeCache()
Invalidates the cell size cache and revalidates/-paints the list.


paintCell

protected void paintCell(java.awt.Graphics g,
                         int row,
                         java.awt.Rectangle rowBounds,
                         javax.swing.ListCellRenderer cellRenderer,
                         javax.swing.ListModel dataModel,
                         javax.swing.ListSelectionModel selModel,
                         int leadIndex)
Paint one List cell: compute the relevant state, get the "rubber stamp" cell renderer component, and then use the CellRendererPane to paint it. Subclasses may want to override this method rather than paint().

Overrides:
paintCell in class javax.swing.plaf.basic.BasicListUI
See Also:
paint(java.awt.Graphics, javax.swing.JComponent)

paint

public void paint(java.awt.Graphics g,
                  javax.swing.JComponent c)
Paint the rows that intersect the Graphics objects clipRect. This method calls paintCell as necessary. Subclasses may want to override these methods.

Overrides:
paint in class javax.swing.plaf.basic.BasicListUI
See Also:
paintCell(java.awt.Graphics, int, java.awt.Rectangle, javax.swing.ListCellRenderer, javax.swing.ListModel, javax.swing.ListSelectionModel, int)

getBaseline

public int getBaseline(javax.swing.JComponent c,
                       int width,
                       int height)
Returns the baseline.

Overrides:
getBaseline in class javax.swing.plaf.basic.BasicListUI
Throws:
java.lang.NullPointerException
java.lang.IllegalArgumentException
Since:
1.6
See Also:
JComponent.getBaseline(int, int)

getBaselineResizeBehavior

public java.awt.Component.BaselineResizeBehavior getBaselineResizeBehavior(javax.swing.JComponent c)
Returns an enum indicating how the baseline of the component changes as the size changes.

Overrides:
getBaselineResizeBehavior in class javax.swing.plaf.basic.BasicListUI
Throws:
java.lang.NullPointerException
Since:
1.6
See Also:
JComponent.getBaseline(int, int)

getPreferredSize

public java.awt.Dimension getPreferredSize(javax.swing.JComponent c)
The preferredSize of the list depends upon the layout orientation.
Layout OrientationPreferred Size
JList.VERTICAL The preferredSize of the list is total height of the rows and the maximum width of the cells. If JList.fixedCellHeight is specified then the total height of the rows is just (cellVerticalMargins + fixedCellHeight) * model.getSize() where rowVerticalMargins is the space we allocate for drawing the yellow focus outline. Similarly if fixedCellWidth is specified then we just use that.
JList.VERTICAL_WRAP If the visible row count is greater than zero, the preferredHeight is the maximum cell height * visibleRowCount. If the visible row count is <= 0, the preferred height is either the current height of the list, or the maximum cell height, whichever is bigger. The preferred width is than the maximum cell width * number of columns needed. Where the number of columns needs is list.height / max cell height. Max cell height is either the fixed cell height, or is determined by iterating through all the cells to find the maximum height from the ListCellRenderer.
JList.HORIZONTAL_WRAP If the visible row count is greater than zero, the preferredHeight is the maximum cell height * adjustedRowCount. Where visibleRowCount is used to determine the number of columns. Because this lays out horizontally the number of rows is then determined from the column count. For example, lets say you have a model with 10 items and the visible row count is 8. The number of columns needed to display this is 2, but you no longer need 8 rows to display this, you only need 5, thus the adjustedRowCount is 5.

If the visible row count is <= 0, the preferred height is dictated by the number of columns, which will be as many as can fit in the width of the JList (width / max cell width), with at least one column. The preferred height then becomes the model size / number of columns * maximum cell height. Max cell height is either the fixed cell height, or is determined by iterating through all the cells to find the maximum height from the ListCellRenderer.

The above specifies the raw preferred width and height. The resulting preferred width is the above width + insets.left + insets.right and the resulting preferred height is the above height + insets.top + insets.bottom. Where the Insets are determined from list.getInsets().

Overrides:
getPreferredSize in class javax.swing.plaf.basic.BasicListUI
Parameters:
c - The JList component.
Returns:
The total size of the list.

selectPreviousIndex

protected void selectPreviousIndex()
Selected the previous row and force it to be visible.

Overrides:
selectPreviousIndex in class javax.swing.plaf.basic.BasicListUI
See Also:
JList.ensureIndexIsVisible(int)

selectNextIndex

protected void selectNextIndex()
Selected the previous row and force it to be visible.

Overrides:
selectNextIndex in class javax.swing.plaf.basic.BasicListUI
See Also:
JList.ensureIndexIsVisible(int)

installKeyboardActions

protected void installKeyboardActions()
Registers the keyboard bindings on the JList that the BasicXListUI is associated with. This method is called at installUI() time.

Overrides:
installKeyboardActions in class javax.swing.plaf.basic.BasicListUI
See Also:
installUI(javax.swing.JComponent)

uninstallKeyboardActions

protected void uninstallKeyboardActions()
Unregisters keyboard actions installed from installKeyboardActions. This method is called at uninstallUI() time - subclassess should ensure that all of the keyboard actions registered at installUI time are removed here.

Overrides:
uninstallKeyboardActions in class javax.swing.plaf.basic.BasicListUI
See Also:
installUI(javax.swing.JComponent)

installListeners

protected void installListeners()
Create and install the listeners for the JList, its model, and its selectionModel. This method is called at installUI() time.

Overrides:
installListeners in class javax.swing.plaf.basic.BasicListUI
See Also:
installUI(javax.swing.JComponent), uninstallListeners()

uninstallListeners

protected void uninstallListeners()
Remove the listeners for the JList, its model, and its selectionModel. All of the listener fields, are reset to null here. This method is called at uninstallUI() time, it should be kept in sync with installListeners.

Overrides:
uninstallListeners in class javax.swing.plaf.basic.BasicListUI
See Also:
uninstallUI(javax.swing.JComponent), installListeners()

installDefaults

protected void installDefaults()
Initialize JList properties, e.g. font, foreground, and background, and add the CellRendererPane. The font, foreground, and background properties are only set if their current value is either null or a UIResource, other properties are set if the current value is null.

Overrides:
installDefaults in class javax.swing.plaf.basic.BasicListUI
See Also:
uninstallDefaults(), installUI(javax.swing.JComponent), CellRendererPane

uninstallDefaults

protected void uninstallDefaults()
Set the JList properties that haven't been explicitly overridden to null. A property is considered overridden if its current value is not a UIResource.

Overrides:
uninstallDefaults in class javax.swing.plaf.basic.BasicListUI
See Also:
installDefaults(), uninstallUI(javax.swing.JComponent), CellRendererPane

installUI

public void installUI(javax.swing.JComponent c)
Initializes this.list by calling installDefaults(), installListeners(), and installKeyboardActions() in order.

Overrides:
installUI in class javax.swing.plaf.basic.BasicListUI
See Also:
installDefaults(), installListeners(), installKeyboardActions()

uninstallUI

public void uninstallUI(javax.swing.JComponent c)
Uninitializes this.list by calling uninstallListeners(), uninstallKeyboardActions(), and uninstallDefaults() in order. Sets this.list to null.

Overrides:
uninstallUI in class javax.swing.plaf.basic.BasicListUI
See Also:
uninstallListeners(), uninstallKeyboardActions(), uninstallDefaults()

createUI

public static javax.swing.plaf.ComponentUI createUI(javax.swing.JComponent list)
Returns a new instance of BasicXListUI. BasicXListUI delegates are allocated one per JList.

Returns:
A new ListUI implementation for the Windows look and feel.

locationToIndex

public int locationToIndex(javax.swing.JList list,
                           java.awt.Point location)

Overrides:
locationToIndex in class javax.swing.plaf.basic.BasicListUI
Throws:
java.lang.NullPointerException

indexToLocation

public java.awt.Point indexToLocation(javax.swing.JList list,
                                      int index)

Overrides:
indexToLocation in class javax.swing.plaf.basic.BasicListUI

getCellBounds

public java.awt.Rectangle getCellBounds(javax.swing.JList list,
                                        int index1,
                                        int index2)

Overrides:
getCellBounds in class javax.swing.plaf.basic.BasicListUI

getRowHeight

protected int getRowHeight(int row)
Returns the height of the specified row based on the current layout.

Overrides:
getRowHeight in class javax.swing.plaf.basic.BasicListUI
Returns:
The specified row height or -1 if row isn't valid.
See Also:
convertYToRow(int), convertRowToY(int), updateLayoutState()

convertYToRow

protected int convertYToRow(int y0)
Convert the JList relative coordinate to the row that contains it, based on the current layout. If y0 doesn't fall within any row, return -1.

Overrides:
convertYToRow in class javax.swing.plaf.basic.BasicListUI
Returns:
The row that contains y0, or -1.
See Also:
getRowHeight(int), updateLayoutState()

convertRowToY

protected int convertRowToY(int row)
Return the JList relative Y coordinate of the origin of the specified row or -1 if row isn't valid.

Overrides:
convertRowToY in class javax.swing.plaf.basic.BasicListUI
Returns:
The Y coordinate of the origin of row, or -1.
See Also:
getRowHeight(int), updateLayoutState()

maybeUpdateLayoutState

protected void maybeUpdateLayoutState()
If updateLayoutStateNeeded is non zero, call updateLayoutState() and reset updateLayoutStateNeeded. This method should be called by methods before doing any computation based on the geometry of the list. For example it's the first call in paint() and getPreferredSize().

Overrides:
maybeUpdateLayoutState in class javax.swing.plaf.basic.BasicListUI
See Also:
updateLayoutState()

updateLayoutState

protected void updateLayoutState()
Recompute the value of cellHeight or cellHeights based and cellWidth, based on the current font and the current values of fixedCellWidth, fixedCellHeight, and prototypeCellValue.

Overrides:
updateLayoutState in class javax.swing.plaf.basic.BasicListUI
See Also:
maybeUpdateLayoutState()

createMouseInputListener

protected javax.swing.event.MouseInputListener createMouseInputListener()
Creates a delegate that implements MouseInputListener. The delegate is added to the corresponding java.awt.Component listener lists at installUI() time. Subclasses can override this method to return a custom MouseInputListener, e.g.
 class MyListUI extends BasicXListUI {
    protected MouseInputListener createMouseInputListener() {
        return new MyMouseInputHandler();
    }
    public class MyMouseInputHandler extends MouseInputHandler {
        public void mouseMoved(MouseEvent e) {
            // do some extra work when the mouse moves
            super.mouseMoved(e);
        }
    }
 }
 

Overrides:
createMouseInputListener in class javax.swing.plaf.basic.BasicListUI
See Also:
BasicXListUI.MouseInputHandler, installUI(javax.swing.JComponent)

createFocusListener

protected java.awt.event.FocusListener createFocusListener()
Overrides:
createFocusListener in class javax.swing.plaf.basic.BasicListUI

createListSelectionListener

protected javax.swing.event.ListSelectionListener createListSelectionListener()
Creates an instance of ListSelectionHandler that's added to the JLists by selectionModel as needed. Subclasses can override this method to return a custom ListSelectionListener, e.g.
 class MyListUI extends BasicXListUI {
    protected ListSelectionListener createListSelectionListener() {
        return new MySelectionListener();
    }
    public class MySelectionListener extends ListSelectionHandler {
        public void valueChanged(ListSelectionEvent e) {
            // do some extra work when the selection changes
            super.valueChange(e);
        }
    }
 }
 

Overrides:
createListSelectionListener in class javax.swing.plaf.basic.BasicListUI
See Also:
BasicXListUI.ListSelectionHandler, installUI(javax.swing.JComponent)

createListDataListener

protected javax.swing.event.ListDataListener createListDataListener()
Creates an instance of ListDataListener that's added to the JLists by model as needed. Subclasses can override this method to return a custom ListDataListener, e.g.
 class MyListUI extends BasicXListUI {
    protected ListDataListener createListDataListener() {
        return new MyListDataListener();
    }
    public class MyListDataListener extends ListDataHandler {
        public void contentsChanged(ListDataEvent e) {
            // do some extra work when the models contents change
            super.contentsChange(e);
        }
    }
 }
 

Overrides:
createListDataListener in class javax.swing.plaf.basic.BasicListUI
See Also:
ListDataListener, JList.getModel(), installUI(javax.swing.JComponent)

createPropertyChangeListener

protected java.beans.PropertyChangeListener createPropertyChangeListener()
Creates an instance of PropertyChangeHandler that's added to the JList by installUI(). Subclasses can override this method to return a custom PropertyChangeListener, e.g.
 class MyListUI extends BasicXListUI {
    protected PropertyChangeListener createPropertyChangeListener() {
        return new MyPropertyChangeListener();
    }
    public class MyPropertyChangeListener extends PropertyChangeHandler {
        public void propertyChange(PropertyChangeEvent e) {
            if (e.getPropertyName().equals("model")) {
                // do some extra work when the model changes
            }
            super.propertyChange(e);
        }
    }
 }
 

Overrides:
createPropertyChangeListener in class javax.swing.plaf.basic.BasicListUI
See Also:
PropertyChangeListener, installUI(javax.swing.JComponent)


Copyright © 2011. All Rights Reserved.