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 );
}
// 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 ---------------------------------------------------
private static class ActiveFont

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -35,7 +35,10 @@ import java.awt.event.MouseEvent;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.util.IdentityHashMap;
import java.util.WeakHashMap;
import java.util.function.Consumer;
import java.util.function.Supplier;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.LookAndFeel;
@@ -43,6 +46,7 @@ import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import com.formdev.flatlaf.FlatClientProperties;
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" );
private static WeakHashMap<LookAndFeel, IdentityHashMap<Object, ComponentUI>> sharedUIinstances = new WeakHashMap<>();
public static Rectangle addInsets( Rectangle r, Insets insets ) {
return new Rectangle(
r.x - insets.left,
@@ -534,6 +540,19 @@ public class FlatUIUtils
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 ------------------------------------------------
public static class HoverListener

View File

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