Skip to content

Commit 86a4f30

Browse files
committed
Styling: added convenience methods to invoke StyleableUI interface methods
~~~java JButton button = new JButton(); int arc = FlatLaf.getStyleableValue( button, "arc" ); Color borderColor = FlatLaf.getStyleableValue( button, "borderColor" ); ~~~
1 parent 0e523f1 commit 86a4f30

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
import java.beans.PropertyChangeEvent;
3131
import java.beans.PropertyChangeListener;
3232
import java.io.File;
33+
import java.lang.invoke.MethodHandle;
34+
import java.lang.invoke.MethodHandles;
35+
import java.lang.invoke.MethodType;
3336
import java.lang.reflect.Method;
3437
import java.net.URL;
3538
import java.util.ArrayList;
@@ -63,6 +66,7 @@
6366
import javax.swing.UIManager;
6467
import javax.swing.UnsupportedLookAndFeelException;
6568
import javax.swing.plaf.ColorUIResource;
69+
import javax.swing.plaf.ComponentUI;
6670
import javax.swing.plaf.FontUIResource;
6771
import javax.swing.plaf.IconUIResource;
6872
import javax.swing.plaf.UIResource;
@@ -73,6 +77,7 @@
7377
import com.formdev.flatlaf.ui.FlatPopupFactory;
7478
import com.formdev.flatlaf.ui.FlatRootPaneUI;
7579
import com.formdev.flatlaf.ui.FlatUIUtils;
80+
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableUI;
7681
import com.formdev.flatlaf.util.GrayFilter;
7782
import com.formdev.flatlaf.util.LoggingFacade;
7883
import com.formdev.flatlaf.util.MultiResolutionImageSupport;
@@ -1230,6 +1235,62 @@ public static void runWithUIDefaultsGetter( Function<Object, Object> uiDefaultsG
12301235
*/
12311236
public static final Object NULL_VALUE = new Object();
12321237

1238+
/**
1239+
* Returns information about styleable values of a component.
1240+
* <p>
1241+
* This is equivalent to: {@code ((StyleableUI)c.getUI()).getStyleableInfos(c)}
1242+
*
1243+
* @since 2.5
1244+
*/
1245+
public static Map<String, Class<?>> getStyleableInfos( JComponent c ) {
1246+
StyleableUI ui = getStyleableUI( c );
1247+
return (ui != null) ? ui.getStyleableInfos( c ) : null;
1248+
}
1249+
1250+
/**
1251+
* Returns the (styled) value for the given key from the given component.
1252+
* <p>
1253+
* This is equivalent to: {@code ((StyleableUI)c.getUI()).getStyleableValue(c, key)}
1254+
*
1255+
* @since 2.5
1256+
*/
1257+
@SuppressWarnings( "unchecked" )
1258+
public static <T> T getStyleableValue( JComponent c, String key ) {
1259+
StyleableUI ui = getStyleableUI( c );
1260+
return (ui != null) ? (T) ui.getStyleableValue( c, key ) : null;
1261+
}
1262+
1263+
private static StyleableUI getStyleableUI( JComponent c ) {
1264+
if( !getUIMethodInitialized ) {
1265+
getUIMethodInitialized = true;
1266+
1267+
if( SystemInfo.isJava_9_orLater ) {
1268+
try {
1269+
// JComponent.getUI() is available since Java 9
1270+
getUIMethod = MethodHandles.lookup().findVirtual( JComponent.class, "getUI",
1271+
MethodType.methodType( ComponentUI.class ) );
1272+
} catch( Exception ex ) {
1273+
// ignore
1274+
}
1275+
}
1276+
}
1277+
1278+
try {
1279+
Object ui;
1280+
if( getUIMethod != null )
1281+
ui = getUIMethod.invoke( c );
1282+
else
1283+
ui = c.getClass().getMethod( "getUI" ).invoke( c );
1284+
return (ui instanceof StyleableUI) ? (StyleableUI) ui : null;
1285+
} catch( Throwable ex ) {
1286+
// ignore
1287+
return null;
1288+
}
1289+
}
1290+
1291+
private static boolean getUIMethodInitialized;
1292+
private static MethodHandle getUIMethod;
1293+
12331294
//---- class FlatUIDefaults -----------------------------------------------
12341295

12351296
private class FlatUIDefaults

0 commit comments

Comments
 (0)