get UI defaults in shared instances only once (same as used in e.g. MetalRadioButtonUI)

Note: this optimization does not work when switching from one Flat LaF to another Flat LaF because UI uninstall/install is done per component
This commit is contained in:
Karl Tauber
2019-09-14 21:23:33 +02:00
parent d8a9e3e3f0
commit 7aaf700d34
5 changed files with 82 additions and 27 deletions

View File

@@ -81,6 +81,8 @@ public class FlatButtonUI
private Icon helpButtonIcon; private Icon helpButtonIcon;
private boolean defaults_initialized = false;
private static ComponentUI instance; private static ComponentUI instance;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
@@ -93,6 +95,7 @@ public class FlatButtonUI
protected void installDefaults( AbstractButton b ) { protected void installDefaults( AbstractButton b ) {
super.installDefaults( b ); super.installDefaults( b );
if( !defaults_initialized ) {
String prefix = getPropertyPrefix(); String prefix = getPropertyPrefix();
focusWidth = UIManager.getInt( "Component.focusWidth" ); focusWidth = UIManager.getInt( "Component.focusWidth" );
@@ -115,6 +118,9 @@ public class FlatButtonUI
helpButtonIcon = UIManager.getIcon( "HelpButton.icon" ); helpButtonIcon = UIManager.getIcon( "HelpButton.icon" );
defaults_initialized = true;
}
MigLayoutVisualPadding.install( b, focusWidth ); MigLayoutVisualPadding.install( b, focusWidth );
} }
@@ -123,6 +129,7 @@ public class FlatButtonUI
super.uninstallDefaults( b ); super.uninstallDefaults( b );
MigLayoutVisualPadding.uninstall( b ); MigLayoutVisualPadding.uninstall( b );
defaults_initialized = false;
} }
static boolean isContentAreaFilled( Component c ) { static boolean isContentAreaFilled( Component c ) {

View File

@@ -39,6 +39,8 @@ public class FlatLabelUI
{ {
private Color disabledForeground; private Color disabledForeground;
private boolean defaults_initialized = false;
private static ComponentUI instance; private static ComponentUI instance;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
@@ -51,7 +53,17 @@ public class FlatLabelUI
protected void installDefaults( JLabel c ) { protected void installDefaults( JLabel c ) {
super.installDefaults( c ); super.installDefaults( c );
if( !defaults_initialized ) {
disabledForeground = UIManager.getColor( "Label.disabledForeground" ); disabledForeground = UIManager.getColor( "Label.disabledForeground" );
defaults_initialized = true;
}
}
@Override
protected void uninstallDefaults( JLabel c ) {
super.uninstallDefaults( c );
defaults_initialized = false;
} }
@Override @Override

View File

@@ -45,6 +45,8 @@ public class FlatSeparatorUI
protected int stripeWidth; protected int stripeWidth;
protected int stripeIndent; protected int stripeIndent;
private boolean defaults_initialized = false;
private static ComponentUI instance; private static ComponentUI instance;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
@@ -57,10 +59,20 @@ public class FlatSeparatorUI
protected void installDefaults( JSeparator s ) { protected void installDefaults( JSeparator s ) {
super.installDefaults( s ); super.installDefaults( s );
if( !defaults_initialized ) {
String prefix = getPropertyPrefix(); String prefix = getPropertyPrefix();
height = UIManager.getInt( prefix + ".height" ); height = UIManager.getInt( prefix + ".height" );
stripeWidth = UIManager.getInt( prefix + ".stripeWidth" ); stripeWidth = UIManager.getInt( prefix + ".stripeWidth" );
stripeIndent = UIManager.getInt( prefix + ".stripeIndent" ); stripeIndent = UIManager.getInt( prefix + ".stripeIndent" );
defaults_initialized = true;
}
}
@Override
protected void uninstallDefaults( JSeparator s ) {
super.uninstallDefaults( s );
defaults_initialized = false;
} }
protected String getPropertyPrefix() { protected String getPropertyPrefix() {

View File

@@ -51,6 +51,8 @@ public class FlatToggleButtonUI
protected Color toolbarSelectedBackground; protected Color toolbarSelectedBackground;
private boolean defaults_initialized = false;
private static ComponentUI instance; private static ComponentUI instance;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
@@ -68,11 +70,21 @@ public class FlatToggleButtonUI
protected void installDefaults( AbstractButton b ) { protected void installDefaults( AbstractButton b ) {
super.installDefaults( b ); super.installDefaults( b );
if( !defaults_initialized ) {
selectedBackground = UIManager.getColor( "ToggleButton.selectedBackground" ); selectedBackground = UIManager.getColor( "ToggleButton.selectedBackground" );
selectedForeground = UIManager.getColor( "ToggleButton.selectedForeground" ); selectedForeground = UIManager.getColor( "ToggleButton.selectedForeground" );
disabledSelectedBackground = UIManager.getColor( "ToggleButton.disabledSelectedBackground" ); disabledSelectedBackground = UIManager.getColor( "ToggleButton.disabledSelectedBackground" );
toolbarSelectedBackground = UIManager.getColor( "ToggleButton.toolbar.selectedBackground" ); toolbarSelectedBackground = UIManager.getColor( "ToggleButton.toolbar.selectedBackground" );
defaults_initialized = true;
}
}
@Override
protected void uninstallDefaults( AbstractButton b ) {
super.uninstallDefaults( b );
defaults_initialized = false;
} }
@Override @Override

View File

@@ -46,6 +46,8 @@ public class FlatToolBarSeparatorUI
protected int separatorWidth; protected int separatorWidth;
protected Color separatorColor; protected Color separatorColor;
private boolean defaults_initialized = false;
private static ComponentUI instance; private static ComponentUI instance;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
@@ -58,14 +60,24 @@ public class FlatToolBarSeparatorUI
protected void installDefaults( JSeparator c ) { protected void installDefaults( JSeparator c ) {
super.installDefaults( c ); super.installDefaults( c );
if( !defaults_initialized ) {
separatorWidth = UIManager.getInt( "ToolBar.separatorWidth" ); separatorWidth = UIManager.getInt( "ToolBar.separatorWidth" );
separatorColor = UIManager.getColor( "ToolBar.separatorColor" ); separatorColor = UIManager.getColor( "ToolBar.separatorColor" );
defaults_initialized = true;
}
// necessary for vertical toolbars if separator size was set using setSeparatorSize() // necessary for vertical toolbars if separator size was set using setSeparatorSize()
// (otherwise there will be a gap on the left side of the vertical toolbar) // (otherwise there will be a gap on the left side of the vertical toolbar)
c.setAlignmentX( 0 ); c.setAlignmentX( 0 );
} }
@Override
protected void uninstallDefaults( JSeparator s ) {
super.uninstallDefaults( s );
defaults_initialized = false;
}
@Override @Override
public Dimension getPreferredSize( JComponent c ) { public Dimension getPreferredSize( JComponent c ) {
Dimension size = ((JToolBar.Separator)c).getSeparatorSize(); Dimension size = ((JToolBar.Separator)c).getSeparatorSize();