Search in sources :

Example 21 with JSONSerializer

use of org.apache.pivot.json.JSONSerializer in project pivot by apache.

the class Resources method readJSONResource.

@SuppressWarnings("unchecked")
private Map<String, Object> readJSONResource(String name) throws IOException, SerializationException {
    Map<String, Object> resourceMapFromResource = null;
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream inputStream = classLoader.getResourceAsStream(name);
    if (inputStream != null) {
        JSONSerializer serializer = new JSONSerializer(this.charset);
        try {
            resourceMapFromResource = (Map<String, Object>) serializer.readObject(inputStream);
        } finally {
            inputStream.close();
        }
    }
    return resourceMapFromResource;
}
Also used : InputStream(java.io.InputStream) JSONSerializer(org.apache.pivot.json.JSONSerializer)

Example 22 with JSONSerializer

use of org.apache.pivot.json.JSONSerializer in project pivot by apache.

the class ListButton method setListData.

/**
 * Sets the list button's list data.
 *
 * @param listData A URL referring to a JSON file containing the data to be
 * presented by the list button.
 * @throws IllegalArgumentException if the URL is {@code null} or
 * the URL data stream cannot be propertly parsed into a list.
 */
public void setListData(URL listData) {
    Utils.checkNull(listData, "listData");
    JSONSerializer jsonSerializer = new JSONSerializer();
    try {
        setListData((List<?>) jsonSerializer.readObject(listData.openStream()));
    } catch (SerializationException | IOException exception) {
        throw new IllegalArgumentException(exception);
    }
}
Also used : SerializationException(org.apache.pivot.serialization.SerializationException) IOException(java.io.IOException) JSONSerializer(org.apache.pivot.json.JSONSerializer)

Example 23 with JSONSerializer

use of org.apache.pivot.json.JSONSerializer in project pivot by apache.

the class TerraTheme method load.

private void load(URL location) {
    Utils.checkNull(location, "location");
    try (InputStream inputStream = location.openStream()) {
        JSONSerializer serializer = new JSONSerializer();
        @SuppressWarnings("unchecked") Map<String, ?> properties = (Map<String, ?>) serializer.readObject(inputStream);
        font = Font.decode((String) properties.get(FONT_PROPERTY));
        String defaultStylesName = (String) properties.get(DEFAULT_STYLES_PROPERTY);
        if (defaultStylesName != null && !defaultStylesName.isEmpty()) {
            defaultStylesFile = defaultStylesName;
        }
        String namedStylesName = (String) properties.get(NAMED_STYLES_PROPERTY);
        if (namedStylesName != null && !namedStylesName.isEmpty()) {
            namedStylesFile = namedStylesName;
        }
        @SuppressWarnings("unchecked") List<String> colorCodes = (List<String>) properties.get(COLORS_PROPERTY);
        numberOfPaletteColors = colorCodes.getLength();
        int numberOfColors = numberOfPaletteColors * 3;
        colors = new ArrayList<>(numberOfColors);
        Double mult = (Double) properties.get(COLOR_MULTIPLIER_PROPERTY);
        if (mult != null) {
            colorMultiplier = mult.floatValue();
        }
        themeIsDark = properties.getBoolean(THEME_IS_DARK_PROPERTY, false);
        themeIsFlat = properties.getBoolean(THEME_IS_FLAT_PROPERTY, false);
        transitionEnabled = properties.getBoolean(TRANSITION_ENABLED_PROPERTY, true);
        for (String colorCode : colorCodes) {
            Color baseColor = GraphicsUtilities.decodeColor(colorCode, "baseColor");
            colors.add(darken(baseColor));
            colors.add(baseColor);
            colors.add(brighten(baseColor));
        }
        @SuppressWarnings("unchecked") Map<String, String> messageIconNames = (Map<String, String>) properties.get(MESSAGE_ICONS_PROPERTY);
        messageIcons = new HashMap<>();
        loadMessageIcons(messageIconNames, messageIcons);
        @SuppressWarnings("unchecked") Map<String, String> smallMessageIconNames = (Map<String, String>) properties.get(SMALL_MESSAGE_ICONS_PROPERTY);
        smallMessageIcons = new HashMap<>();
        loadMessageIcons(smallMessageIconNames, smallMessageIcons);
        if ((defaultBackgroundColor = getColorProperty(properties, DEFAULT_BACKGROUND_COLOR_PROPERTY)) == null) {
            defaultBackgroundColor = super.getDefaultBackgroundColor();
        }
        if ((defaultForegroundColor = getColorProperty(properties, DEFAULT_FOREGROUND_COLOR_PROPERTY)) == null) {
            defaultForegroundColor = super.getDefaultForegroundColor();
        }
    } 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) Color(java.awt.Color) IOException(java.io.IOException) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) Map(org.apache.pivot.collections.Map) HashMap(org.apache.pivot.collections.HashMap) JSONSerializer(org.apache.pivot.json.JSONSerializer)

Example 24 with JSONSerializer

use of org.apache.pivot.json.JSONSerializer 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)

Example 25 with JSONSerializer

use of org.apache.pivot.json.JSONSerializer in project pivot by apache.

the class DataBinding method initialize.

@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
    form = (Form) namespace.get("form");
    loadJavaButton = (PushButton) namespace.get("loadJavaButton");
    loadJSONButton = (PushButton) namespace.get("loadJSONButton");
    clearButton = (PushButton) namespace.get("clearButton");
    sourceLabel = (Label) namespace.get("sourceLabel");
    loadJavaButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            form.load(new BeanAdapter(CONTACT));
            sourceLabel.setText("Java");
        }
    });
    loadJSONButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            JSONSerializer serializer = new JSONSerializer();
            try (InputStream inputStream = getClass().getResourceAsStream("contact.json")) {
                form.load(serializer.readObject(inputStream));
                sourceLabel.setText("JSON");
            } catch (Exception exception) {
                System.err.println(exception);
            }
            button.setEnabled(true);
        }
    });
    clearButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            form.clear();
            sourceLabel.setText("");
        }
    });
}
Also used : ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) PushButton(org.apache.pivot.wtk.PushButton) Button(org.apache.pivot.wtk.Button) InputStream(java.io.InputStream) BeanAdapter(org.apache.pivot.beans.BeanAdapter) JSONSerializer(org.apache.pivot.json.JSONSerializer)

Aggregations

JSONSerializer (org.apache.pivot.json.JSONSerializer)25 IOException (java.io.IOException)11 Test (org.junit.Test)11 SerializationException (org.apache.pivot.serialization.SerializationException)8 StringReader (java.io.StringReader)6 ArrayList (org.apache.pivot.collections.ArrayList)5 HashMap (org.apache.pivot.collections.HashMap)5 List (org.apache.pivot.collections.List)5 File (java.io.File)4 InputStream (java.io.InputStream)4 URL (java.net.URL)3 Map (org.apache.pivot.collections.Map)3 QueryException (org.apache.pivot.web.QueryException)3 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 Color (java.awt.Color)1 StringWriter (java.io.StringWriter)1 MalformedURLException (java.net.MalformedURLException)1 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)1 BeanAdapter (org.apache.pivot.beans.BeanAdapter)1