Search in sources :

Example 16 with Map

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);
    }
}
Also used : SerializationException(org.apache.pivot.serialization.SerializationException) InputStream(java.io.InputStream) IOException(java.io.IOException) URL(java.net.URL) Map(org.apache.pivot.collections.Map) HashMap(org.apache.pivot.collections.HashMap) JSONSerializer(org.apache.pivot.json.JSONSerializer)

Aggregations

Map (org.apache.pivot.collections.Map)16 HashMap (org.apache.pivot.collections.HashMap)7 List (org.apache.pivot.collections.List)5 BeanAdapter (org.apache.pivot.beans.BeanAdapter)4 Dictionary (org.apache.pivot.collections.Dictionary)3 Sequence (org.apache.pivot.collections.Sequence)3 JSONSerializer (org.apache.pivot.json.JSONSerializer)3 SerializationException (org.apache.pivot.serialization.SerializationException)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 ArrayList (org.apache.pivot.collections.ArrayList)2 FileList (org.apache.pivot.io.FileList)2 PropertiesSerializer (org.apache.pivot.serialization.PropertiesSerializer)2 Task (org.apache.pivot.util.concurrent.Task)2 TaskListener (org.apache.pivot.util.concurrent.TaskListener)2 TreeBranch (org.apache.pivot.wtk.content.TreeBranch)2 Test (org.junit.Test)2 Color (java.awt.Color)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1