|
67 | 67 | import java.awt.Image; |
68 | 68 | import java.io.IOException; |
69 | 69 | import java.io.InputStream; |
| 70 | +import java.lang.reflect.Constructor; |
70 | 71 | import java.lang.reflect.Field; |
71 | 72 | import java.lang.reflect.InvocationTargetException; |
72 | 73 | import java.net.MalformedURLException; |
73 | 74 | import java.net.URL; |
74 | 75 | import java.nio.file.Path; |
| 76 | +import java.util.Arrays; |
| 77 | +import java.util.Comparator; |
75 | 78 | import java.util.Deque; |
76 | 79 | import java.util.HashMap; |
77 | 80 | import java.util.LinkedList; |
78 | 81 | import java.util.Map; |
| 82 | +import java.util.Properties; |
79 | 83 | import java.util.ResourceBundle; |
80 | 84 | import java.util.function.Supplier; |
81 | 85 |
|
@@ -1024,6 +1028,57 @@ public static <C extends JComponent> void bind(String tag, Class<C> type, Suppli |
1024 | 1028 | suppliers.put(tag, supplier); |
1025 | 1029 | } |
1026 | 1030 |
|
| 1031 | + /** |
| 1032 | + * Applies multiple bindings. |
| 1033 | + * |
| 1034 | + * @param bindings |
| 1035 | + * The bindings to apply. |
| 1036 | + */ |
| 1037 | + public static void bind(Properties bindings) throws ReflectiveOperationException { |
| 1038 | + bind(bindings, ClassLoader.getSystemClassLoader()); |
| 1039 | + } |
| 1040 | + |
| 1041 | + /** |
| 1042 | + * Applies multiple bindings. |
| 1043 | + * |
| 1044 | + * @param bindings |
| 1045 | + * The bindings to apply. |
| 1046 | + * |
| 1047 | + * @param classLoader |
| 1048 | + * The class loader that will be used to resolve the bindings. |
| 1049 | + */ |
| 1050 | + @SuppressWarnings("unchecked") |
| 1051 | + public static void bind(Properties bindings, ClassLoader classLoader) throws ClassNotFoundException { |
| 1052 | + if (bindings == null || classLoader == null) { |
| 1053 | + throw new IllegalArgumentException(); |
| 1054 | + } |
| 1055 | + |
| 1056 | + for (var entry : bindings.entrySet()) { |
| 1057 | + var tag = (String)entry.getKey(); |
| 1058 | + var typeName = (String)entry.getValue(); |
| 1059 | + |
| 1060 | + var type = (Class<?>)classLoader.loadClass(typeName); |
| 1061 | + |
| 1062 | + var constructors = type.getConstructors(); |
| 1063 | + |
| 1064 | + if (constructors.length == 0) { |
| 1065 | + throw new UnsupportedOperationException(String.format("%s cannot be instantiated.", typeName)); |
| 1066 | + } |
| 1067 | + |
| 1068 | + Arrays.sort(constructors, Comparator.comparing(Constructor::getParameterCount)); |
| 1069 | + |
| 1070 | + var constructor = constructors[0]; |
| 1071 | + |
| 1072 | + bind(tag, (Class<JComponent>)type, () -> { |
| 1073 | + try { |
| 1074 | + return (JComponent)constructor.newInstance(new Object[constructor.getParameterCount()]); |
| 1075 | + } catch (InstantiationException | IllegalAccessException | InvocationTargetException exception) { |
| 1076 | + throw new RuntimeException(exception); |
| 1077 | + } |
| 1078 | + }); |
| 1079 | + } |
| 1080 | + } |
| 1081 | + |
1027 | 1082 | /** |
1028 | 1083 | * Retrieves a named color. |
1029 | 1084 | * |
|
0 commit comments