Improved subclassing:

- reviewed all private methods and made them protected/public where it might be useful for subclasses
- ComboBox and Spinner: added protected getBackground() and getForeground() methods to allow subclasses to change colors
- TabbedPane: moved tab separator painting to own method

(issue #113)
This commit is contained in:
Karl Tauber
2020-06-20 10:46:56 +02:00
parent 40321856f2
commit ea2412d3a7
10 changed files with 66 additions and 49 deletions

View File

@@ -67,14 +67,14 @@ public class FlatCheckBoxMenuItemIcon
g2.draw( path ); g2.draw( path );
} }
private Color getCheckmarkColor( Component c ) { protected Color getCheckmarkColor( Component c ) {
if( c instanceof JMenuItem && ((JMenuItem)c).isArmed() && !isUnderlineSelection() ) if( c instanceof JMenuItem && ((JMenuItem)c).isArmed() && !isUnderlineSelection() )
return selectionForeground; return selectionForeground;
return c.isEnabled() ? checkmarkColor : disabledCheckmarkColor; return c.isEnabled() ? checkmarkColor : disabledCheckmarkColor;
} }
private boolean isUnderlineSelection() { protected boolean isUnderlineSelection() {
// not storing value of "MenuItem.selectionType" in class to allow changing at runtime // not storing value of "MenuItem.selectionType" in class to allow changing at runtime
return "underline".equals( UIManager.getString( "MenuItem.selectionType" ) ); return "underline".equals( UIManager.getString( "MenuItem.selectionType" ) );
} }

View File

@@ -65,14 +65,14 @@ public class FlatMenuArrowIcon
} }
} }
private Color getArrowColor( Component c ) { protected Color getArrowColor( Component c ) {
if( c instanceof JMenu && ((JMenu)c).isSelected() && !isUnderlineSelection() ) if( c instanceof JMenu && ((JMenu)c).isSelected() && !isUnderlineSelection() )
return selectionForeground; return selectionForeground;
return c.isEnabled() ? arrowColor : disabledArrowColor; return c.isEnabled() ? arrowColor : disabledArrowColor;
} }
private boolean isUnderlineSelection() { protected boolean isUnderlineSelection() {
// not storing value of "MenuItem.selectionType" in class to allow changing at runtime // not storing value of "MenuItem.selectionType" in class to allow changing at runtime
return "underline".equals( UIManager.getString( "MenuItem.selectionType" ) ); return "underline".equals( UIManager.getString( "MenuItem.selectionType" ) );
} }

View File

@@ -317,12 +317,11 @@ public class FlatComboBoxUI
// use non-UIResource colors because when SwingUtilities.updateComponentTreeUI() // use non-UIResource colors because when SwingUtilities.updateComponentTreeUI()
// is used, then the editor is updated after the combobox and the // is used, then the editor is updated after the combobox and the
// colors are again replaced with default colors // colors are again replaced with default colors
boolean enabled = editor.isEnabled(); boolean isTextComponent = editor instanceof JTextComponent;
editor.setForeground( FlatUIUtils.nonUIResource( (enabled || editor instanceof JTextComponent) editor.setForeground( FlatUIUtils.nonUIResource( getForeground( isTextComponent || editor.isEnabled() ) ) );
? comboBox.getForeground()
: disabledForeground ) ); if( isTextComponent )
if( editor instanceof JTextComponent ) ((JTextComponent)editor).setDisabledTextColor( FlatUIUtils.nonUIResource( getForeground( false ) ) );
((JTextComponent)editor).setDisabledTextColor( FlatUIUtils.nonUIResource( disabledForeground ) );
} }
@Override @Override
@@ -358,9 +357,7 @@ public class FlatComboBoxUI
boolean isLeftToRight = comboBox.getComponentOrientation().isLeftToRight(); boolean isLeftToRight = comboBox.getComponentOrientation().isLeftToRight();
// paint background // paint background
g2.setColor( enabled g2.setColor( getBackground( enabled ) );
? (editableBackground != null && comboBox.isEditable() ? editableBackground : c.getBackground())
: getDisabledBackground( comboBox ) );
FlatUIUtils.paintComponentBackground( g2, 0, 0, width, height, focusWidth, arc ); FlatUIUtils.paintComponentBackground( g2, 0, 0, width, height, focusWidth, arc );
// paint arrow button background // paint arrow button background
@@ -399,8 +396,8 @@ public class FlatComboBoxUI
uninstallCellPaddingBorder( c ); uninstallCellPaddingBorder( c );
boolean enabled = comboBox.isEnabled(); boolean enabled = comboBox.isEnabled();
c.setForeground( enabled ? comboBox.getForeground() : disabledForeground ); c.setBackground( getBackground( enabled ) );
c.setBackground( enabled ? comboBox.getBackground() : getDisabledBackground( comboBox ) ); c.setForeground( getForeground( enabled ) );
boolean shouldValidate = (c instanceof JPanel); boolean shouldValidate = (c instanceof JPanel);
if( padding != null ) if( padding != null )
@@ -420,8 +417,14 @@ public class FlatComboBoxUI
// not necessary because already painted in update() // not necessary because already painted in update()
} }
private Color getDisabledBackground( JComponent c ) { protected Color getBackground( boolean enabled ) {
return isIntelliJTheme ? FlatUIUtils.getParentBackground( c ) : disabledBackground; return enabled
? (editableBackground != null && comboBox.isEditable() ? editableBackground : comboBox.getBackground())
: (isIntelliJTheme ? FlatUIUtils.getParentBackground( comboBox ) : disabledBackground);
}
protected Color getForeground( boolean enabled ) {
return enabled ? comboBox.getForeground() : disabledForeground;
} }
@Override @Override

View File

@@ -371,7 +371,7 @@ debug*/
return menuItem instanceof JMenu && ((JMenu)menuItem).isTopLevelMenu(); return menuItem instanceof JMenu && ((JMenu)menuItem).isTopLevelMenu();
} }
private boolean isUnderlineSelection() { protected boolean isUnderlineSelection() {
return "underline".equals( UIManager.getString( "MenuItem.selectionType" ) ); return "underline".equals( UIManager.getString( "MenuItem.selectionType" ) );
} }
@@ -416,6 +416,13 @@ debug*/
if( accelerator == cachedAccelerator ) if( accelerator == cachedAccelerator )
return cachedAcceleratorText; return cachedAcceleratorText;
cachedAccelerator = accelerator;
cachedAcceleratorText = getTextForAccelerator( accelerator );
return cachedAcceleratorText;
}
protected String getTextForAccelerator( KeyStroke accelerator ) {
StringBuilder buf = new StringBuilder(); StringBuilder buf = new StringBuilder();
int modifiers = accelerator.getModifiers(); int modifiers = accelerator.getModifiers();
if( modifiers != 0 ) if( modifiers != 0 )
@@ -427,10 +434,7 @@ debug*/
else else
buf.append( accelerator.getKeyChar() ); buf.append( accelerator.getKeyChar() );
cachedAccelerator = accelerator; return buf.toString();
cachedAcceleratorText = buf.toString();
return cachedAcceleratorText;
} }
//---- class MinSizeIcon -------------------------------------------------- //---- class MinSizeIcon --------------------------------------------------

View File

@@ -240,7 +240,7 @@ public class FlatScrollBarUI
return button; return button;
} }
private boolean isShowButtons() { protected boolean isShowButtons() {
Object showButtons = scrollbar.getClientProperty( FlatClientProperties.SCROLL_BAR_SHOW_BUTTONS ); Object showButtons = scrollbar.getClientProperty( FlatClientProperties.SCROLL_BAR_SHOW_BUTTONS );
if( showButtons == null && scrollbar.getParent() instanceof JScrollPane ) if( showButtons == null && scrollbar.getParent() instanceof JScrollPane )
showButtons = ((JScrollPane)scrollbar.getParent()).getClientProperty( FlatClientProperties.SCROLL_BAR_SHOW_BUTTONS ); showButtons = ((JScrollPane)scrollbar.getParent()).getClientProperty( FlatClientProperties.SCROLL_BAR_SHOW_BUTTONS );

View File

@@ -206,8 +206,8 @@ public class FlatSpinnerUI
// use non-UIResource colors because when SwingUtilities.updateComponentTreeUI() // use non-UIResource colors because when SwingUtilities.updateComponentTreeUI()
// is used, then the text field is updated after the spinner and the // is used, then the text field is updated after the spinner and the
// colors are again replaced with default colors // colors are again replaced with default colors
textField.setForeground( FlatUIUtils.nonUIResource( spinner.getForeground() ) ); textField.setForeground( FlatUIUtils.nonUIResource( getForeground( true ) ) );
textField.setDisabledTextColor( FlatUIUtils.nonUIResource( disabledForeground ) ); textField.setDisabledTextColor( FlatUIUtils.nonUIResource( getForeground( false ) ) );
} }
} }
@@ -217,6 +217,16 @@ public class FlatSpinnerUI
: null; : null;
} }
protected Color getBackground( boolean enabled ) {
return enabled
? spinner.getBackground()
: (isIntelliJTheme ? FlatUIUtils.getParentBackground( spinner ) : disabledBackground);
}
protected Color getForeground( boolean enabled ) {
return enabled ? spinner.getForeground() : disabledForeground;
}
@Override @Override
protected LayoutManager createLayout() { protected LayoutManager createLayout() {
return getHandler(); return getHandler();
@@ -266,9 +276,7 @@ public class FlatSpinnerUI
boolean isLeftToRight = spinner.getComponentOrientation().isLeftToRight(); boolean isLeftToRight = spinner.getComponentOrientation().isLeftToRight();
// paint background // paint background
g2.setColor( enabled g2.setColor( getBackground( enabled ) );
? c.getBackground()
: (isIntelliJTheme ? FlatUIUtils.getParentBackground( c ) : disabledBackground) );
FlatUIUtils.paintComponentBackground( g2, 0, 0, width, height, focusWidth, arc ); FlatUIUtils.paintComponentBackground( g2, 0, 0, width, height, focusWidth, arc );
// paint arrow buttons background // paint arrow buttons background

View File

@@ -333,28 +333,30 @@ public class FlatTabbedPaneUI
// paint tab separators // paint tab separators
if( clientPropertyBoolean( tabPane, TABBED_PANE_SHOW_TAB_SEPARATORS, showTabSeparators ) && if( clientPropertyBoolean( tabPane, TABBED_PANE_SHOW_TAB_SEPARATORS, showTabSeparators ) &&
!isLastInRun( tabIndex ) ) !isLastInRun( tabIndex ) )
{ paintTabSeparator( g, tabPlacement, x, y, w, h );
float sepWidth = UIScale.scale( 1f );
float offset = tabSeparatorsFullHeight ? 0 : UIScale.scale( 5f );
g.setColor( (tabSeparatorColor != null) ? tabSeparatorColor : contentAreaColor );
if( tabPlacement == LEFT || tabPlacement == RIGHT ) {
// paint tab separator at bottom side
((Graphics2D)g).fill( new Rectangle2D.Float( x + offset, y + h - sepWidth, w - (offset * 2), sepWidth ) );
} else if( tabPane.getComponentOrientation().isLeftToRight() ) {
// paint tab separator at right side
((Graphics2D)g).fill( new Rectangle2D.Float( x + w - sepWidth, y + offset, sepWidth, h - (offset * 2) ) );
} else {
// paint tab separator at left side
((Graphics2D)g).fill( new Rectangle2D.Float( x, y + offset, sepWidth, h - (offset * 2) ) );
}
}
if( isSelected ) if( isSelected )
paintTabSelection( g, tabPlacement, x, y, w, h ); paintTabSelection( g, tabPlacement, x, y, w, h );
} }
protected void paintTabSelection( Graphics g, int tabPlacement, int x, int y, int w, int h ) { protected void paintTabSeparator( Graphics g, int tabPlacement, int x, int y, int w, int h ) {
float sepWidth = UIScale.scale( 1f );
float offset = tabSeparatorsFullHeight ? 0 : UIScale.scale( 5f );
g.setColor( (tabSeparatorColor != null) ? tabSeparatorColor : contentAreaColor );
if( tabPlacement == LEFT || tabPlacement == RIGHT ) {
// paint tab separator at bottom side
((Graphics2D)g).fill( new Rectangle2D.Float( x + offset, y + h - sepWidth, w - (offset * 2), sepWidth ) );
} else if( tabPane.getComponentOrientation().isLeftToRight() ) {
// paint tab separator at right side
((Graphics2D)g).fill( new Rectangle2D.Float( x + w - sepWidth, y + offset, sepWidth, h - (offset * 2) ) );
} else {
// paint tab separator at left side
((Graphics2D)g).fill( new Rectangle2D.Float( x, y + offset, sepWidth, h - (offset * 2) ) );
}
}
protected void paintTabSelection( Graphics g, int tabPlacement, int x, int y, int w, int h ) {
// increase clip bounds in scroll-tab-layout to paint over the separator line // increase clip bounds in scroll-tab-layout to paint over the separator line
Rectangle clipBounds = isScrollTabLayout() ? g.getClipBounds() : null; Rectangle clipBounds = isScrollTabLayout() ? g.getClipBounds() : null;
if( clipBounds != null ) { if( clipBounds != null ) {

View File

@@ -86,7 +86,7 @@ public class FlatTableCellBorder
/** /**
* Checks whether at least one selected cell is editable. * Checks whether at least one selected cell is editable.
*/ */
private boolean isSelectionEditable( JTable table ) { protected boolean isSelectionEditable( JTable table ) {
if( table.getRowSelectionAllowed() ) { if( table.getRowSelectionAllowed() ) {
int columnCount = table.getColumnCount(); int columnCount = table.getColumnCount();
int[] selectedRows = table.getSelectedRows(); int[] selectedRows = table.getSelectedRows();

View File

@@ -35,7 +35,7 @@ public class ColorFunctions
return HSLColor.toRGB( hsl, alpha ); return HSLColor.toRGB( hsl, alpha );
} }
private static float clamp( float value ) { public static float clamp( float value ) {
return (value < 0) return (value < 0)
? 0 ? 0
: ((value > 100) : ((value > 100)

View File

@@ -68,7 +68,7 @@ public class SystemInfo
IS_KDE = (IS_LINUX && System.getenv( "KDE_FULL_SESSION" ) != null); IS_KDE = (IS_LINUX && System.getenv( "KDE_FULL_SESSION" ) != null);
} }
private static long scanVersion( String version ) { public static long scanVersion( String version ) {
int major = 1; int major = 1;
int minor = 0; int minor = 0;
int micro = 0; int micro = 0;
@@ -86,7 +86,7 @@ public class SystemInfo
return toVersion( major, minor, micro, patch ); return toVersion( major, minor, micro, patch );
} }
private static long toVersion( int major, int minor, int micro, int patch ) { public static long toVersion( int major, int minor, int micro, int patch ) {
return ((long) major << 48) + ((long) minor << 32) + ((long) micro << 16) + patch; return ((long) major << 48) + ((long) minor << 32) + ((long) micro << 16) + patch;
} }
} }