Skip to content

Commit

Permalink
Remove the dependency with JMenuBar to support, for example, the Comm…
Browse files Browse the repository at this point in the history
…andMenuBar in JIDE OSS
  • Loading branch information
rogerbj authored and rogerbj committed Jan 13, 2024
1 parent afa18f8 commit 4d4b90c
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.formdev.flatlaf.ui;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
Expand Down Expand Up @@ -271,7 +272,7 @@ protected void paintSelection( Graphics g, Color selectionBackground, Insets sel
if( !isHover() )
selectionBackground = getStyleFromMenuBarUI( ui -> ui.selectionBackground, menuBarSelectionBackground, selectionBackground );

JMenuBar menuBar = (JMenuBar) menuItem.getParent();
Container menuBar = menuItem.getParent();
JRootPane rootPane = SwingUtilities.getRootPane( menuBar );
if( rootPane != null && rootPane.getParent() instanceof Window &&
rootPane.getJMenuBar() == menuBar &&
Expand Down Expand Up @@ -321,12 +322,17 @@ private <T> T getStyleFromMenuBarUI( Function<FlatMenuBarUI, T> f, T defaultValu
}

private <T> T getStyleFromMenuBarUI( Function<FlatMenuBarUI, T> f, T defaultValue ) {
MenuBarUI ui = ((JMenuBar)menuItem.getParent()).getUI();
if( !(ui instanceof FlatMenuBarUI) )
return defaultValue;

T value = f.apply( (FlatMenuBarUI) ui );
return (value != null) ? value : defaultValue;
Container menuItemParent = menuItem.getParent();
if( menuItemParent instanceof JMenuBar ) {
MenuBarUI ui = ((JMenuBar) menuItemParent).getUI();
if( ui instanceof FlatMenuBarUI ) {
T value = f.apply( (FlatMenuBarUI) ui );
if( value != null ) {
return value;
}
}
}
return defaultValue;
}
}
}

0 comments on commit 4d4b90c

Please sign in to comment.