|
30 | 30 | import java.beans.PropertyChangeEvent; |
31 | 31 | import java.beans.PropertyChangeListener; |
32 | 32 | import java.io.File; |
| 33 | +import java.lang.invoke.MethodHandle; |
| 34 | +import java.lang.invoke.MethodHandles; |
| 35 | +import java.lang.invoke.MethodType; |
33 | 36 | import java.lang.reflect.Method; |
34 | 37 | import java.net.URL; |
35 | 38 | import java.util.ArrayList; |
|
63 | 66 | import javax.swing.UIManager; |
64 | 67 | import javax.swing.UnsupportedLookAndFeelException; |
65 | 68 | import javax.swing.plaf.ColorUIResource; |
| 69 | +import javax.swing.plaf.ComponentUI; |
66 | 70 | import javax.swing.plaf.FontUIResource; |
67 | 71 | import javax.swing.plaf.IconUIResource; |
68 | 72 | import javax.swing.plaf.UIResource; |
|
73 | 77 | import com.formdev.flatlaf.ui.FlatPopupFactory; |
74 | 78 | import com.formdev.flatlaf.ui.FlatRootPaneUI; |
75 | 79 | import com.formdev.flatlaf.ui.FlatUIUtils; |
| 80 | +import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableUI; |
76 | 81 | import com.formdev.flatlaf.util.GrayFilter; |
77 | 82 | import com.formdev.flatlaf.util.LoggingFacade; |
78 | 83 | import com.formdev.flatlaf.util.MultiResolutionImageSupport; |
@@ -1230,6 +1235,62 @@ public static void runWithUIDefaultsGetter( Function<Object, Object> uiDefaultsG |
1230 | 1235 | */ |
1231 | 1236 | public static final Object NULL_VALUE = new Object(); |
1232 | 1237 |
|
| 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 | + |
1233 | 1294 | //---- class FlatUIDefaults ----------------------------------------------- |
1234 | 1295 |
|
1235 | 1296 | private class FlatUIDefaults |
|
0 commit comments