Class BasicXListUI
- Direct Known Subclasses:
SynthXListUI
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:
- extracted method for list.getModel().getSize (for the delegate class and all contained static classes) and use that method exclusively
- similar for remaining list.getModel(): implemented wrapping listModel which messages the list
- rename key for shared actionMap to keep core list actions separate (just in case somebody wants both) - they point to the wrong delegate
- replaced references to SwingUtilities2 in sun packages by references to cinvalid input: '&p''ed methods in SwingXUtilities
- replaced storage of shared Input/ActionMap in defaultLookup by direct storage in UIManager.
- added methods to un/-installSortUI and call in un/installUI(component)
- changed PropertyChangeHandler to call a) hasHandledPropertyChange to allow this class to replace super handler functinality and b) updateSortUI after handling all not-sorter related properties.
- changed createPropertyChangeListener to return a PropertyChangeHandler
- changed ListDataHandler to check if event handled by SortUI and delegate to handler only if not
- changed createListDataListener to return a ListDataHandler
- changed ListSelectionHandler to check if event handled by SortUI and delegate to handler only if not
- Issue #1495-swingx: getBaseline throughs NPE
- Version:
- 1.127 12/02/08
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
class
This inner class is marked "public" due to a compiler bug.private class
class
The ListDataListener that's added to the JLists model at installUI time, and whenever the JList.model property changes.class
The ListSelectionListener that's added to the JLists selection model at installUI time, and whenever the JList.selectionModel property changes.(package private) static class
class
Mouse input, and focus handling for JList.class
The PropertyChangeListener that's added to the JList at installUI time. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringBuilder
protected int
protected int[]
protected static final int
protected int
private static final int
Used by IncrementLeadSelectionAction.private static final int
Used by IncrementLeadSelectionAction.private int
Number of columns to create.private static final int
private static final TransferHandler
private static final int
private static final int
Used by IncrementLeadSelectionAction.protected static final int
protected static final int
protected FocusListener
protected static final int
private BasicXListUI.Handler
private static final int
private boolean
Local cache of JList's client property "List.isFileList"private boolean
Local cache of JList's component orientation propertyprivate int
The layout orientation of the list.private static final int
protected JXList
protected ListDataListener
private int
Height of the list.protected ListSelectionListener
private int
Width of the list.protected static final int
private ListModel
protected MouseInputListener
private int
Preferred height to make the list, this is only used if the the list is layed out horizontally.protected PropertyChangeListener
protected static final int
protected CellRendererPane
private int
Number of rows per column.protected static final int
private ListSortUI
private long
The time factor to treate the series of typed alphanumeric key as prefix for first letter navigation.protected int
private static final int
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static int
adjustIndex
(int index, JList list) protected void
checkBaselinePrecondition
(JComponent c, int width, int height) Fix for Issue #1495: NPE on getBaseline.private int
convertLocationToColumn
(int x, int y) Returns the closest column to the passed in location.private int
convertLocationToModel
(int x, int y) Returns the closest location to the model index of the passed in location.private int
convertLocationToRow
(int x, int y0, boolean closest) Returns the row at location x/y.private int
convertLocationToRowInColumn
(int y, int column) Returns the closest row that starts at the specified y-location in the passed in column.private int
convertModelToColumn
(int index) Returns the column that the model indexindex
will be displayed in.private int
convertModelToRow
(int index) Returns the row that the model indexindex
will be displayed in..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 FocusListener
protected ListDataListener
Creates an instance of ListDataListener that's added to the JLists by model as needed.protected ListSelectionListener
Creates an instance of ListSelectionHandler that's added to the JLists by selectionModel as needed.protected MouseInputListener
Creates a delegate that implements MouseInputListener.protected PropertyChangeListener
Creates an instance of PropertyChangeHandler that's added to the JList by installUI().static ComponentUI
createUI
(JComponent list) Returns a new instance of BasicXListUI.int
getBaseline
(JComponent c, int width, int height) Returns the baseline.Returns an enum indicating how the baseline of the component changes as the size changes.private Rectangle
getCellBounds
(JList list, int index) Gets the bounds of the specified model index, returning the resulting bounds, or null ifindex
is not valid.getCellBounds
(JList list, int index1, int index2) private Rectangle
protected Object
getElementAt
(int viewIndex) protected int
private BasicXListUI.Handler
private int
getHeight
(int column, int row) Returns the height of the cell at the passed in location.(package private) InputMap
getInputMap
(int condition) private int
getModelIndex
(int column, int row) Returns the model index for the specified display location.The preferredSize of the list depends upon the layout orientation.private int
getRowCount
(int column) Returns the number of rows in the given column.protected int
getRowHeight
(int row) Returns the height of the specified row based on the current layout.protected ListSortUI
protected ListModel
Compatibility Wrapper: a synthetic model which delegates to list api and throwsindexToLocation
(JList list, int index) protected void
Initialize JList properties, e.g.protected void
Registers the keyboard bindings on theJList
that theBasicXListUI
is associated with.protected void
Create and install the listeners for the JList, its model, and its selectionModel.protected void
Installs SortUI if the list has a rowSorter.void
Initializesthis.list
by callinginstallDefaults()
,installListeners()
, andinstallKeyboardActions()
in order.void
Invalidates the cell size cache and revalidates/-paints the list.static void
int
locationToIndex
(JList list, Point location) protected void
If updateLayoutStateNeeded is non zero, call updateLayoutState() and reset updateLayoutStateNeeded.void
paint
(Graphics g, JComponent c) Paint the rows that intersect the Graphics objects clipRect.protected void
paintCell
(Graphics g, int row, Rectangle rowBounds, ListCellRenderer cellRenderer, ListModel dataModel, 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.private void
private void
paintImpl
(Graphics g, JComponent c) protected boolean
Returns a boolean indicating whether or not the event has been processed by the sortUI.protected boolean
Returns a boolean indicating whether or not the event has been processed by the sortUI.private void
protected void
Selected the previous row and force it to be visible.protected void
Selected the previous row and force it to be visible.protected void
Set the JList properties that haven't been explicitly overridden to null.protected void
Unregisters keyboard actions installed frominstallKeyboardActions
.protected void
Remove the listeners for the JList, its model, and its selectionModel.protected void
Dispose and null's the sortUI if installed.void
Uninitializesthis.list
by callinguninstallListeners()
,uninstallKeyboardActions()
, anduninstallDefaults()
in order.private void
updateHorizontalLayoutState
(int fixedCellWidth, int fixedCellHeight) Invoked when the list is layed out horizontally to determine how many columns to create.private void
protected void
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
(String property) Called from the PropertyChangeHandler.private void
Methods inherited from class javax.swing.plaf.ComponentUI
contains, getAccessibleChild, getAccessibleChildrenCount, getMaximumSize, getMinimumSize, update
-
Field Details
-
BASELINE_COMPONENT_KEY
-
list
-
rendererPane
-
focusListener
-
mouseInputListener
-
listSelectionListener
-
listDataListener
-
propertyChangeListener
-
handler
-
cellHeights
protected int[] cellHeights -
cellHeight
protected int cellHeight -
cellWidth
protected int cellWidth -
updateLayoutStateNeeded
protected int updateLayoutStateNeeded -
listHeight
private int listHeightHeight of the list. When asked to paint, if the current size of the list differs, this will update the layout state. -
listWidth
private int listWidthWidth of the list. When asked to paint, if the current size of the list differs, this will update the layout state. -
layoutOrientation
private int layoutOrientationThe layout orientation of the list. -
columnCount
private int columnCountNumber of columns to create. -
preferredHeight
private int preferredHeightPreferred height to make the list, this is only used if the the list is layed out horizontally. -
rowsPerColumn
private int rowsPerColumnNumber of rows per column. This is only used if the row height is fixed. -
timeFactor
private long timeFactorThe time factor to treate the series of typed alphanumeric key as prefix for first letter navigation. -
isFileList
private boolean isFileListLocal cache of JList's client property "List.isFileList" -
isLeftToRight
private boolean isLeftToRightLocal cache of JList's component orientation property -
modelChanged
protected static final int modelChanged- See Also:
-
selectionModelChanged
protected static final int selectionModelChanged- See Also:
-
fontChanged
protected static final int fontChanged- See Also:
-
fixedCellWidthChanged
protected static final int fixedCellWidthChanged- See Also:
-
fixedCellHeightChanged
protected static final int fixedCellHeightChanged- See Also:
-
prototypeCellValueChanged
protected static final int prototypeCellValueChanged- See Also:
-
cellRendererChanged
protected static final int cellRendererChanged- See Also:
-
layoutOrientationChanged
private static final int layoutOrientationChanged- See Also:
-
heightChanged
private static final int heightChanged- See Also:
-
widthChanged
private static final int widthChanged- See Also:
-
componentOrientationChanged
private static final int componentOrientationChanged- See Also:
-
DROP_LINE_THICKNESS
private static final int DROP_LINE_THICKNESS- See Also:
-
modelX
-
sortUI
-
CHANGE_LEAD
private static final int CHANGE_LEADUsed by IncrementLeadSelectionAction. Indicates the action should change the lead, and not select it.- See Also:
-
CHANGE_SELECTION
private static final int CHANGE_SELECTIONUsed by IncrementLeadSelectionAction. Indicates the action should change the selection and lead.- See Also:
-
EXTEND_SELECTION
private static final int EXTEND_SELECTIONUsed by IncrementLeadSelectionAction. Indicates the action should extend the selection from the anchor to the next index.- See Also:
-
defaultTransferHandler
-
-
Constructor Details
-
BasicXListUI
public BasicXListUI()
-
-
Method Details
-
loadActionMap
-
getViewModel
Compatibility Wrapper: a synthetic model which delegates to list api and throws- Returns:
-
getElementCount
protected int getElementCount()- Returns:
-
getElementAt
-
checkBaselinePrecondition
Fix for Issue #1495: NPE on getBaseline. As per contract, that methods needs to throw Exceptions on illegal parameters. As we by-pass super, need to do the check and throw ouerselves.- Parameters:
c
-width
-height
-
-
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
Called from the PropertyChangeHandler.- Parameters:
property
- the name of the changed property.
-
updateSortUIToRowSorterProperty
private void updateSortUIToRowSorterProperty() -
processedBySortUI
Returns a boolean indicating whether or not the event has been processed by the sortUI.- Parameters:
e
-- Returns:
-
processedBySortUI
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(Graphics g, int row, Rectangle rowBounds, ListCellRenderer cellRenderer, ListModel dataModel, 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 classBasicListUI
- See Also:
-
paint
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 classBasicListUI
- See Also:
-
paintImpl
-
paintDropLine
-
getDropLineRect
-
getBaseline
Returns the baseline.- Overrides:
getBaseline
in classBasicListUI
- Since:
- 1.6
- See Also:
-
getBaselineResizeBehavior
Returns an enum indicating how the baseline of the component changes as the size changes.- Overrides:
getBaselineResizeBehavior
in classBasicListUI
- Since:
- 1.6
- See Also:
-
getPreferredSize
The preferredSize of the list depends upon the layout orientation.Layout Orientation Preferred 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 invalid input: '<'= 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 invalid input: '<'= 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.Insets
are determined fromlist.getInsets()
.- Overrides:
getPreferredSize
in classBasicListUI
- 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 classBasicListUI
- See Also:
-
selectNextIndex
protected void selectNextIndex()Selected the previous row and force it to be visible.- Overrides:
selectNextIndex
in classBasicListUI
- See Also:
-
installKeyboardActions
protected void installKeyboardActions()Registers the keyboard bindings on theJList
that theBasicXListUI
is associated with. This method is called at installUI() time.- Overrides:
installKeyboardActions
in classBasicListUI
- See Also:
-
getInputMap
-
uninstallKeyboardActions
protected void uninstallKeyboardActions()Unregisters keyboard actions installed frominstallKeyboardActions
. 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 classBasicListUI
- See Also:
-
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 classBasicListUI
- See Also:
-
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 classBasicListUI
- See Also:
-
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 classBasicListUI
- See Also:
-
updateIsFileList
private void updateIsFileList() -
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 classBasicListUI
- See Also:
-
installUI
Initializesthis.list
by callinginstallDefaults()
,installListeners()
, andinstallKeyboardActions()
in order.- Overrides:
installUI
in classBasicListUI
- See Also:
-
uninstallUI
Uninitializesthis.list
by callinguninstallListeners()
,uninstallKeyboardActions()
, anduninstallDefaults()
in order. Sets this.list to null.- Overrides:
uninstallUI
in classBasicListUI
- See Also:
-
createUI
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
- Overrides:
locationToIndex
in classBasicListUI
-
indexToLocation
- Overrides:
indexToLocation
in classBasicListUI
-
getCellBounds
- Overrides:
getCellBounds
in classBasicListUI
-
getCellBounds
Gets the bounds of the specified model index, returning the resulting bounds, or null ifindex
is not valid. -
getRowHeight
protected int getRowHeight(int row) Returns the height of the specified row based on the current layout.- Overrides:
getRowHeight
in classBasicListUI
- Returns:
- The specified row height or -1 if row isn't valid.
- See Also:
-
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 classBasicListUI
- Returns:
- The row that contains y0, or -1.
- See Also:
-
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 classBasicListUI
- Returns:
- The Y coordinate of the origin of row, or -1.
- See Also:
-
getHeight
private int getHeight(int column, int row) Returns the height of the cell at the passed in location. -
convertLocationToRow
private int convertLocationToRow(int x, int y0, boolean closest) Returns the row at location x/y.- Parameters:
closest
- If true and the location doesn't exactly match a particular location, this will return the closest row.
-
convertLocationToRowInColumn
private int convertLocationToRowInColumn(int y, int column) Returns the closest row that starts at the specified y-location in the passed in column. -
convertLocationToModel
private int convertLocationToModel(int x, int y) Returns the closest location to the model index of the passed in location. -
getRowCount
private int getRowCount(int column) Returns the number of rows in the given column. -
getModelIndex
private int getModelIndex(int column, int row) Returns the model index for the specified display location. Ifcolumn
xrow
is beyond the length of the model, this will return the model size - 1. -
convertLocationToColumn
private int convertLocationToColumn(int x, int y) Returns the closest column to the passed in location. -
convertModelToRow
private int convertModelToRow(int index) Returns the row that the model indexindex
will be displayed in.. -
convertModelToColumn
private int convertModelToColumn(int index) Returns the column that the model indexindex
will be displayed in. -
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 classBasicListUI
- See Also:
-
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 classBasicListUI
- See Also:
-
updateHorizontalLayoutState
private void updateHorizontalLayoutState(int fixedCellWidth, int fixedCellHeight) Invoked when the list is layed out horizontally to determine how many columns to create.This updates the
rowsPerColumn,
columnCount
,preferredHeight
and potentiallycellHeight
instance variables. -
getHandler
-
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 classBasicListUI
- See Also:
-
createFocusListener
- Overrides:
createFocusListener
in classBasicListUI
-
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 classBasicListUI
- See Also:
-
redrawList
private void redrawList() -
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 classBasicListUI
- See Also:
-
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 classBasicListUI
- See Also:
-
adjustIndex
-