use of org.apache.pivot.collections.Map in project pivot by apache.
the class ApplicationContext method applyStylesheet.
/**
* Adds the styles from a named stylesheet to the named or typed style
* collections.
*
* @param resourceName The resource name of the stylesheet to apply.
*/
@SuppressWarnings("unchecked")
public static void applyStylesheet(String resourceName) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL stylesheetLocation = classLoader.getResource(resourceName.substring(1));
if (stylesheetLocation == null) {
throw new RuntimeException("Unable to locate style sheet resource \"" + resourceName + "\".");
}
try (InputStream inputStream = stylesheetLocation.openStream()) {
JSONSerializer serializer = new JSONSerializer();
Map<String, ?> stylesheet = (Map<String, ?>) serializer.readObject(inputStream);
for (String name : stylesheet) {
Map<String, ?> styles = (Map<String, ?>) stylesheet.get(name);
int i = name.lastIndexOf('.') + 1;
if (Character.isUpperCase(name.charAt(i))) {
// Assume the current package if none specified
if (!name.contains(".")) {
name = CURRENT_PACKAGE.getName() + "." + name;
}
Class<?> type = null;
try {
type = Class.forName(name);
} catch (ClassNotFoundException exception) {
// No-op
}
if (type != null && Component.class.isAssignableFrom(type)) {
Component.getTypedStyles().put((Class<? extends Component>) type, styles);
}
} else {
Component.getNamedStyles().put(name, styles);
}
}
} catch (IOException exception) {
throw new RuntimeException(exception);
} catch (SerializationException exception) {
throw new RuntimeException(exception);
}
}
Aggregations