Tree and List: fixed painting of rounded drop backgrounds (issue #1023)

This commit is contained in:
Karl Tauber
2025-09-06 00:15:11 +02:00
parent e7a766bf8f
commit d388158de7
5 changed files with 125 additions and 37 deletions

View File

@@ -302,6 +302,7 @@ public class FlatListUI
ListModel dataModel, ListSelectionModel selModel, int leadIndex )
{
boolean isSelected = selModel.isSelectedIndex( row );
boolean isDropRow = isDropRow( row );
// paint alternating rows
if( alternateRowColor != null && row % 2 != 0 &&
@@ -335,7 +336,7 @@ public class FlatListUI
}
// rounded selection or selection insets
if( isSelected &&
if( (isSelected || isDropRow) &&
!isFileList && // rounded selection is not supported for file list
(rendererComponent instanceof DefaultListCellRenderer ||
rendererComponent instanceof BasicComboBoxRenderer) &&
@@ -376,7 +377,22 @@ public class FlatListUI
this.getColor() == rendererComponent.getBackground() )
{
inPaintSelection = true;
paintCellSelection( this, row, x, y, width, height );
if( isDropRow ) {
// for rounded drop background, it is necessary to first
// paint selection background because may be not rounded on some corners
if( isSelected ) {
Color oldColor = getColor();
setColor( list.getSelectionBackground() );
paintCellSelection( this, row, x, y, width, height );
setColor( oldColor );
}
// paint drop background
float arc = UIScale.scale( selectionArc / 2f );
FlatUIUtils.paintSelection( this, x, y, width, height,
UIScale.scale( selectionInsets ), arc, arc, arc, arc, 0 );
} else
paintCellSelection( this, row, x, y, width, height );
inPaintSelection = false;
} else
super.fillRect( x, y, width, height );
@@ -475,4 +491,15 @@ public class FlatListUI
FlatListUI ui = (FlatListUI) list.getUI();
ui.paintCellSelection( g, row, x, y, width, height );
}
/**
* Checks whether dropping on a row.
* See DefaultListCellRenderer.getListCellRendererComponent().
*/
private boolean isDropRow( int row ) {
JList.DropLocation dropLocation = list.getDropLocation();
return dropLocation != null &&
!dropLocation.isInsert() &&
dropLocation.getIndex() == row;
}
}

View File

@@ -577,7 +577,6 @@ public class FlatTreeUI
boolean isEditing = (editingComponent != null && editingRow == row);
boolean isSelected = tree.isRowSelected( row );
boolean isDropRow = isDropRow( row );
boolean needsSelectionPainting = (isSelected || isDropRow) && isPaintSelection();
// paint alternating rows
if( alternateRowColor != null && row % 2 != 0 ) {
@@ -608,7 +607,7 @@ public class FlatTreeUI
if( isSelected && isWideSelection() ) {
Color oldColor = g.getColor();
g.setColor( selectionInactiveBackground );
paintWideSelection( g, bounds, row );
paintWideSelection( g, bounds, row, false );
g.setColor( oldColor );
}
return;
@@ -628,7 +627,7 @@ public class FlatTreeUI
// renderer background/foreground
Color oldBackgroundSelectionColor = null;
if( isSelected && !hasFocus && !isDropRow ) {
if( isSelected && !hasFocus ) {
// apply inactive selection background/foreground if tree is not focused
oldBackgroundSelectionColor = setRendererBackgroundSelectionColor( rendererComponent, selectionInactiveBackground );
setRendererForeground( rendererComponent, selectionInactiveForeground );
@@ -655,26 +654,12 @@ public class FlatTreeUI
}
// paint selection background
if( needsSelectionPainting ) {
// set selection color
Color oldColor = g.getColor();
g.setColor( isDropRow
? UIManager.getColor( "Tree.dropCellBackground" )
: (rendererComponent instanceof DefaultTreeCellRenderer
? ((DefaultTreeCellRenderer)rendererComponent).getBackgroundSelectionColor()
: (hasFocus ? selectionBackground : selectionInactiveBackground)) );
if( isSelected && isPaintSelection() ) {
Color selectionColor = rendererComponent instanceof DefaultTreeCellRenderer
? ((DefaultTreeCellRenderer)rendererComponent).getBackgroundSelectionColor()
: (hasFocus ? selectionBackground : selectionInactiveBackground);
if( isWideSelection() ) {
// wide selection
paintWideSelection( g, bounds, row );
} else {
// non-wide selection
paintCellBackground( g, rendererComponent, bounds, row, true );
}
// this is actually not necessary because renderer should always set color
// before painting, but doing anyway to avoid any side effect (in bad renderers)
g.setColor( oldColor );
paintRowSelection( g, selectionColor, rendererComponent, bounds, row, hasFocus, false );
} else {
// paint cell background if DefaultTreeCellRenderer.getBackgroundNonSelectionColor() is set
if( rendererComponent instanceof DefaultTreeCellRenderer ) {
@@ -683,12 +668,19 @@ public class FlatTreeUI
if( bg != null && !bg.equals( defaultCellNonSelectionBackground ) ) {
Color oldColor = g.getColor();
g.setColor( bg );
paintCellBackground( g, rendererComponent, bounds, row, false );
paintCellBackground( g, rendererComponent, bounds, row, false, false );
g.setColor( oldColor );
}
}
}
// paint drop background
// (this needs to be an extra step for rounded selection)
if( isDropRow && isPaintSelection() ) {
paintRowSelection( g, UIManager.getColor( "Tree.dropCellBackground" ),
rendererComponent, bounds, row, hasFocus, true );
}
// paint renderer
rendererPane.paintComponent( g, rendererComponent, tree, bounds.x, bounds.y, bounds.width, bounds.height, true );
@@ -699,6 +691,26 @@ public class FlatTreeUI
((DefaultTreeCellRenderer)rendererComponent).setBorderSelectionColor( oldBorderSelectionColor );
}
private void paintRowSelection( Graphics g, Color color, Component rendererComponent,
Rectangle bounds, int row, boolean hasFocus, boolean paintDropSelection )
{
// set selection color
Color oldColor = g.getColor();
g.setColor( color );
if( isWideSelection() ) {
// wide selection
paintWideSelection( g, bounds, row, paintDropSelection );
} else {
// non-wide selection
paintCellBackground( g, rendererComponent, bounds, row, true, paintDropSelection );
}
// this is actually not necessary because renderer should always set color
// before painting, but doing anyway to avoid any side effect (in bad renderers)
g.setColor( oldColor );
}
private Color setRendererBackgroundSelectionColor( Component rendererComponent, Color color ) {
Color oldColor = null;
@@ -735,11 +747,11 @@ public class FlatTreeUI
return oldColor;
}
private void paintWideSelection( Graphics g, Rectangle bounds, int row ) {
private void paintWideSelection( Graphics g, Rectangle bounds, int row, boolean paintDropSelection ) {
float arcTop, arcBottom;
arcTop = arcBottom = UIScale.scale( selectionArc / 2f );
if( useUnitedRoundedSelection() ) {
if( useUnitedRoundedSelection() && !paintDropSelection ) {
if( row > 0 && tree.isRowSelected( row - 1 ) )
arcTop = 0;
if( row < tree.getRowCount() - 1 && tree.isRowSelected( row + 1 ) )
@@ -751,7 +763,7 @@ public class FlatTreeUI
}
private void paintCellBackground( Graphics g, Component rendererComponent, Rectangle bounds,
int row, boolean paintSelection )
int row, boolean paintSelection, boolean paintDropSelection )
{
int xOffset = 0;
int imageOffset = 0;
@@ -769,7 +781,7 @@ public class FlatTreeUI
float arcTopLeft, arcTopRight, arcBottomLeft, arcBottomRight;
arcTopLeft = arcTopRight = arcBottomLeft = arcBottomRight = UIScale.scale( selectionArc / 2f );
if( useUnitedRoundedSelection() ) {
if( useUnitedRoundedSelection() && !paintDropSelection ) {
if( row > 0 && tree.isRowSelected( row - 1 ) ) {
Rectangle r = getPathBounds( tree, tree.getPathForRow( row - 1 ) );
arcTopLeft = Math.min( arcTopLeft, r.x - bounds.x );