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;
}
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);
}
}
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);
}
}
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);
}
}
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("");
}
});
}
Aggregations