no longer use static fields for shared instances of UI delegates because this makes problems in GUI builders that support Laf switching and use more than one FlatLaf theme at the same time

This commit is contained in:
Karl Tauber
2020-10-04 14:21:00 +02:00
parent 01058bde1b
commit 0a8ece8c9c
13 changed files with 42 additions and 55 deletions

View File

@@ -700,6 +700,18 @@ public abstract class FlatLaf
MnemonicHandler.showMnemonics( false, null ); MnemonicHandler.showMnemonics( false, null );
} }
// do not allow overriding to avoid issues in FlatUIUtils.createSharedUI()
@Override
public final boolean equals( Object obj ) {
return super.equals( obj );
}
// do not allow overriding to avoid issues in FlatUIUtils.createSharedUI()
@Override
public final int hashCode() {
return super.hashCode();
}
//---- class ActiveFont --------------------------------------------------- //---- class ActiveFont ---------------------------------------------------
private static class ActiveFont private static class ActiveFont

View File

@@ -132,12 +132,8 @@ public class FlatButtonUI
private boolean defaults_initialized = false; private boolean defaults_initialized = false;
private static ComponentUI instance;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
if( instance == null ) return FlatUIUtils.createSharedUI( FlatButtonUI.class, FlatButtonUI::new );
instance = new FlatButtonUI();
return instance;
} }
@Override @Override

View File

@@ -42,12 +42,8 @@ import javax.swing.plaf.ComponentUI;
public class FlatCheckBoxUI public class FlatCheckBoxUI
extends FlatRadioButtonUI extends FlatRadioButtonUI
{ {
private static ComponentUI instance;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
if( instance == null ) return FlatUIUtils.createSharedUI( FlatCheckBoxUI.class, FlatCheckBoxUI::new );
instance = new FlatCheckBoxUI();
return instance;
} }
@Override @Override

View File

@@ -56,12 +56,8 @@ public class FlatLabelUI
private boolean defaults_initialized = false; private boolean defaults_initialized = false;
private static ComponentUI instance;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
if( instance == null ) return FlatUIUtils.createSharedUI( FlatLabelUI.class, FlatLabelUI::new );
instance = new FlatLabelUI();
return instance;
} }
@Override @Override

View File

@@ -35,11 +35,7 @@ import javax.swing.plaf.basic.BasicPanelUI;
public class FlatPanelUI public class FlatPanelUI
extends BasicPanelUI extends BasicPanelUI
{ {
private static ComponentUI instance;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
if( instance == null ) return FlatUIUtils.createSharedUI( FlatPanelUI.class, FlatPanelUI::new );
instance = new FlatPanelUI();
return instance;
} }
} }

View File

@@ -38,12 +38,8 @@ import javax.swing.plaf.ComponentUI;
public class FlatPopupMenuSeparatorUI public class FlatPopupMenuSeparatorUI
extends FlatSeparatorUI extends FlatSeparatorUI
{ {
private static ComponentUI instance;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
if( instance == null ) return FlatUIUtils.createSharedUI( FlatPopupMenuSeparatorUI.class, FlatPopupMenuSeparatorUI::new );
instance = new FlatPopupMenuSeparatorUI();
return instance;
} }
@Override @Override

View File

@@ -60,12 +60,8 @@ public class FlatRadioButtonUI
private boolean defaults_initialized = false; private boolean defaults_initialized = false;
private static ComponentUI instance;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
if( instance == null ) return FlatUIUtils.createSharedUI( FlatRadioButtonUI.class, FlatRadioButtonUI::new );
instance = new FlatRadioButtonUI();
return instance;
} }
@Override @Override

View File

@@ -52,12 +52,8 @@ public class FlatSeparatorUI
private boolean defaults_initialized = false; private boolean defaults_initialized = false;
private static ComponentUI instance;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
if( instance == null ) return FlatUIUtils.createSharedUI( FlatSeparatorUI.class, FlatSeparatorUI::new );
instance = new FlatSeparatorUI();
return instance;
} }
@Override @Override

View File

@@ -82,12 +82,8 @@ public class FlatToggleButtonUI
private boolean defaults_initialized = false; private boolean defaults_initialized = false;
private static ComponentUI instance;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
if( instance == null ) return FlatUIUtils.createSharedUI( FlatToggleButtonUI.class, FlatToggleButtonUI::new );
instance = new FlatToggleButtonUI();
return instance;
} }
@Override @Override

View File

@@ -50,12 +50,8 @@ public class FlatToolBarSeparatorUI
private boolean defaults_initialized = false; private boolean defaults_initialized = false;
private static ComponentUI instance;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
if( instance == null ) return FlatUIUtils.createSharedUI( FlatToolBarSeparatorUI.class, FlatToolBarSeparatorUI::new );
instance = new FlatToolBarSeparatorUI();
return instance;
} }
@Override @Override

View File

@@ -52,12 +52,8 @@ public class FlatToolTipUI
{ {
private static PropertyChangeListener sharedPropertyChangedListener; private static PropertyChangeListener sharedPropertyChangedListener;
private static ComponentUI instance;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
if( instance == null ) return FlatUIUtils.createSharedUI( FlatToolTipUI.class, FlatToolTipUI::new );
instance = new FlatToolTipUI();
return instance;
} }
@Override @Override

View File

@@ -35,7 +35,10 @@ import java.awt.event.MouseEvent;
import java.awt.geom.Path2D; import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D; import java.awt.geom.RoundRectangle2D;
import java.util.IdentityHashMap;
import java.util.WeakHashMap;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Supplier;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.JTable; import javax.swing.JTable;
import javax.swing.LookAndFeel; import javax.swing.LookAndFeel;
@@ -43,6 +46,7 @@ import javax.swing.SwingUtilities;
import javax.swing.UIManager; import javax.swing.UIManager;
import javax.swing.border.Border; import javax.swing.border.Border;
import javax.swing.border.CompoundBorder; import javax.swing.border.CompoundBorder;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource; import javax.swing.plaf.UIResource;
import com.formdev.flatlaf.FlatClientProperties; import com.formdev.flatlaf.FlatClientProperties;
import com.formdev.flatlaf.util.DerivedColor; import com.formdev.flatlaf.util.DerivedColor;
@@ -59,6 +63,8 @@ public class FlatUIUtils
{ {
public static final boolean MAC_USE_QUARTZ = Boolean.getBoolean( "apple.awt.graphics.UseQuartz" ); public static final boolean MAC_USE_QUARTZ = Boolean.getBoolean( "apple.awt.graphics.UseQuartz" );
private static WeakHashMap<LookAndFeel, IdentityHashMap<Object, ComponentUI>> sharedUIinstances = new WeakHashMap<>();
public static Rectangle addInsets( Rectangle r, Insets insets ) { public static Rectangle addInsets( Rectangle r, Insets insets ) {
return new Rectangle( return new Rectangle(
r.x - insets.left, r.x - insets.left,
@@ -534,6 +540,19 @@ public class FlatUIUtils
return explicitlySet; return explicitlySet;
} }
/**
* Creates a shared component UI for the given key and the current Laf.
* Each Laf instance has its own shared component UI instance.
* <p>
* This is for GUI builders that support Laf switching and
* may use multiple Laf instances at the same time.
*/
public static ComponentUI createSharedUI( Object key, Supplier<ComponentUI> newInstanceSupplier ) {
return sharedUIinstances
.computeIfAbsent( UIManager.getLookAndFeel(), k -> new IdentityHashMap<>() )
.computeIfAbsent( key, k -> newInstanceSupplier.get() );
}
//---- class HoverListener ------------------------------------------------ //---- class HoverListener ------------------------------------------------
public static class HoverListener public static class HoverListener

View File

@@ -38,12 +38,8 @@ import javax.swing.plaf.basic.BasicViewportUI;
public class FlatViewportUI public class FlatViewportUI
extends BasicViewportUI extends BasicViewportUI
{ {
private static ComponentUI instance;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
if( instance == null ) return FlatUIUtils.createSharedUI( FlatViewportUI.class, FlatViewportUI::new );
instance = new FlatViewportUI();
return instance;
} }
@Override @Override