Search in sources :

Example 26 with JsonTree

use of org.terasology.rendering.nui.widgets.treeView.JsonTree in project Terasology by MovingBlocks.

the class NUIEditorTextEntryBuilder method createKeyValueEditor.

/**
 * @return A {@link UITextEntry} to be used to edit a JSON key/value node.
 */
public static UITextEntry<JsonTree> createKeyValueEditor() {
    UITextEntry.Formatter<JsonTree> formatter = value -> {
        JsonObject jsonObject = new JsonObject();
        String jsonKey = value.getValue().getKey();
        Object jsonValue = value.getValue().getValue();
        if (jsonValue instanceof Boolean) {
            jsonObject.addProperty(jsonKey, (Boolean) jsonValue);
        } else if (jsonValue instanceof Number) {
            jsonObject.addProperty(jsonKey, (Number) jsonValue);
        } else if (jsonValue instanceof String) {
            jsonObject.addProperty(jsonKey, (String) jsonValue);
        } else {
            jsonObject.addProperty(jsonKey, (Character) jsonValue);
        }
        String jsonString = new Gson().toJson(jsonObject);
        return jsonString.substring(1, jsonString.length() - 1);
    };
    UITextEntry.Parser<JsonTree> parser = value -> {
        String jsonString = String.format("{%s}", value);
        try {
            JsonElement jsonElement = new JsonParser().parse(jsonString);
            Map.Entry keyValuePair = jsonElement.getAsJsonObject().entrySet().iterator().next();
            String jsonKey = (String) keyValuePair.getKey();
            JsonTreeValue parsedNode;
            if (keyValuePair.getValue() == null) {
                parsedNode = new JsonTreeValue(jsonKey, null, JsonTreeValue.Type.KEY_VALUE_PAIR);
            } else {
                JsonPrimitive jsonValue = (JsonPrimitive) keyValuePair.getValue();
                if (jsonValue.isBoolean()) {
                    parsedNode = new JsonTreeValue(jsonKey, jsonValue.getAsBoolean(), JsonTreeValue.Type.KEY_VALUE_PAIR);
                } else if (jsonValue.isNumber()) {
                    parsedNode = new JsonTreeValue(jsonKey, jsonValue.getAsNumber(), JsonTreeValue.Type.KEY_VALUE_PAIR);
                } else {
                    parsedNode = new JsonTreeValue(jsonKey, jsonValue.getAsString(), JsonTreeValue.Type.KEY_VALUE_PAIR);
                }
            }
            return new JsonTree(parsedNode);
        } catch (JsonSyntaxException e) {
            return null;
        }
    };
    return createEditorEntry(formatter, parser);
}
Also used : JsonElement(com.google.gson.JsonElement) UITextEntry(org.terasology.rendering.nui.widgets.UITextEntry) JsonTreeValue(org.terasology.rendering.nui.widgets.treeView.JsonTreeValue) JsonObject(com.google.gson.JsonObject) JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree) JsonSyntaxException(com.google.gson.JsonSyntaxException) Gson(com.google.gson.Gson) Map(java.util.Map) JsonPrimitive(com.google.gson.JsonPrimitive) JsonParser(com.google.gson.JsonParser) JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) UITextEntry(org.terasology.rendering.nui.widgets.UITextEntry) JsonTreeValue(org.terasology.rendering.nui.widgets.treeView.JsonTreeValue) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) UITextEntry(org.terasology.rendering.nui.widgets.UITextEntry) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser)

Example 27 with JsonTree

use of org.terasology.rendering.nui.widgets.treeView.JsonTree in project Terasology by MovingBlocks.

the class ContextMenuUtilsTest method testNodeTypes.

@Test
public void testNodeTypes() {
    JsonTree currentNode = inputTree;
    assertEquals(PlaceholderScreen.class, getNodeType(currentNode));
    currentNode = currentNode.getChildWithKey("contents");
    assertEquals(RelativeLayout.class, getNodeType(currentNode));
    currentNode = currentNode.getChildWithKey("contents");
    assertEquals(UIButton.class, getNodeType(currentNode.getChildAt(0)));
    assertEquals(RelativeLayoutHint.class, getNodeType(currentNode.getChildAt(0).getChildWithKey("layoutInfo")));
    assertEquals(VerticalInfo.class, getNodeType(currentNode.getChildAt(0).getChildWithKey("layoutInfo").getChildWithKey("position-top")));
    assertEquals(HorizontalInfo.class, getNodeType(currentNode.getChildAt(0).getChildWithKey("layoutInfo").getChildWithKey("position-horizontal-center")));
    currentNode = currentNode.getChildAt(1);
    assertEquals(RowLayout.class, getNodeType(currentNode));
    assertEquals(RelativeLayoutHint.class, getNodeType(currentNode.getChildWithKey("layoutInfo")));
    currentNode = currentNode.getChildWithKey("contents").getChildAt(0);
    assertEquals(UILabel.class, getNodeType(currentNode));
    assertEquals(RowLayoutHint.class, getNodeType(currentNode.getChildWithKey("layoutInfo")));
}
Also used : JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree) Test(org.junit.Test)

Aggregations

JsonTree (org.terasology.rendering.nui.widgets.treeView.JsonTree)27 JsonTreeValue (org.terasology.rendering.nui.widgets.treeView.JsonTreeValue)14 JsonParser (com.google.gson.JsonParser)9 JsonElement (com.google.gson.JsonElement)6 JsonObject (com.google.gson.JsonObject)5 JsonSyntaxException (com.google.gson.JsonSyntaxException)5 Map (java.util.Map)5 UIWidget (org.terasology.rendering.nui.UIWidget)5 Gson (com.google.gson.Gson)4 JsonPrimitive (com.google.gson.JsonPrimitive)4 JsonReader (com.google.gson.stream.JsonReader)4 IOException (java.io.IOException)4 InputStreamReader (java.io.InputStreamReader)4 UITextEntry (org.terasology.rendering.nui.widgets.UITextEntry)4 Optional (java.util.Optional)3 AssetDataFile (org.terasology.assets.format.AssetDataFile)3 UISkin (org.terasology.rendering.nui.skin.UISkin)3 Field (java.lang.reflect.Field)2 Set (java.util.Set)2 UILayout (org.terasology.rendering.nui.UILayout)2