Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -540,28 +540,41 @@ protected void paintIcons( Graphics g, Rectangle r ) {

@Override
public Dimension getPreferredSize( JComponent c ) {
return applyMinimumWidth( c, applyExtraSize( super.getPreferredSize( c ) ), minimumWidth );
return applyMinimumWidth( c, applyExtraSize( super.getPreferredSize( c ), true ), minimumWidth );
}

@Override
public Dimension getMinimumSize( JComponent c ) {
return applyMinimumWidth( c, applyExtraSize( super.getMinimumSize( c ) ), minimumWidth );
return applyMinimumWidth( c, applyExtraSize( super.getMinimumSize( c ), false ), minimumWidth );
}

private Dimension applyExtraSize( Dimension size ) {
private Dimension applyExtraSize( Dimension size, boolean reduceInset ) {
// add width of leading and trailing icons
size.width += getLeadingIconWidth() + getTrailingIconWidth();

// add width of leading and trailing components
boolean leftVisible = false;
boolean rightVisible = false;
for( JComponent comp : getLeadingComponents() ) {
if( comp != null && comp.isVisible() )
if( comp != null && comp.isVisible() ) {
size.width += comp.getPreferredSize().width;
leftVisible = true;
}
}
for( JComponent comp : getTrailingComponents() ) {
if( comp != null && comp.isVisible() )
if( comp != null && comp.isVisible() ) {
size.width += comp.getPreferredSize().width;
rightVisible = true;
}
}
if( reduceInset ) {
boolean ltr = isLeftToRight();
HorizontalInsets diffInsets = getDiffInsets( leftVisible, rightVisible, ltr );
if( diffInsets.left > 0 )
size.width -= diffInsets.left;
if( diffInsets.right > 0 )
size.width -= diffInsets.right;
}

return size;
}

Expand All @@ -587,6 +600,23 @@ private Dimension applyMinimumWidth( JComponent c, Dimension size, int minimumWi
return size;
}

private HorizontalInsets getDiffInsets( boolean leftVisible, boolean rightVisible, boolean ltr ) {
int left = 0;
int right = 0;
Insets insets = getComponent().getInsets();
if( leftVisible || (ltr ? hasLeadingIcon() : hasTrailingIcon()) ) {
int newLeftInset = Math.min( insets.left, insets.top );
if( newLeftInset < insets.left )
left = insets.left - newLeftInset;
}
if( rightVisible || (ltr ? hasTrailingIcon() : hasLeadingIcon()) ) {
int newRightInset = Math.min( insets.right, insets.top );
if( newRightInset < insets.right )
right = insets.right - newRightInset;
}
return new HorizontalInsets( left, right );
}

static boolean hasDefaultMargins( JComponent c, Insets defaultMargin ) {
Insets margin = ((JTextComponent)c).getMargin();
return margin instanceof UIResource && Objects.equals( margin, defaultMargin );
Expand Down Expand Up @@ -662,23 +692,13 @@ protected Rectangle getIconsRect() {

// if a leading/trailing icons (or components) are shown, then the left/right insets are reduced
// to the top inset, which places the icon nicely centered on left/right side
if( leftVisible || (ltr ? hasLeadingIcon() : hasTrailingIcon()) ) {
// reduce left inset
Insets insets = getComponent().getInsets();
int newLeftInset = Math.min( insets.left, insets.top );
if( newLeftInset < insets.left ) {
int diff = insets.left - newLeftInset;
r.x -= diff;
r.width += diff;
}
}
if( rightVisible || (ltr ? hasTrailingIcon() : hasLeadingIcon()) ) {
// reduce right inset
Insets insets = getComponent().getInsets();
int newRightInset = Math.min( insets.right, insets.top );
if( newRightInset < insets.left )
r.width += insets.right - newRightInset;
HorizontalInsets diffInsets = getDiffInsets( leftVisible, rightVisible, ltr );
if( diffInsets.left > 0 ) {
r.x -= diffInsets.left;
r.width += diffInsets.left;
}
if( diffInsets.right > 0 )
r.width += diffInsets.right;

// make sure that width and height are not negative
r.width = Math.max( r.width, 0 );
Expand Down Expand Up @@ -984,4 +1004,17 @@ public void changedUpdate( DocumentEvent e ) {
documentChanged( e );
}
}

//---- class HorizontalInsets ---------------------------------------------

private static class HorizontalInsets
{
int left;
int right;

HorizontalInsets( int left, int right ) {
this.left = left;
this.right = right;
}
}
}
Loading