use of org.terasology.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")));
}
use of org.terasology.nui.widgets.treeView.JsonTree in project Terasology by MovingBlocks.
the class NUIEditorTextEntryBuilder method createValueEditor.
/**
* @return A {@link UITextEntry} to be used to edit a JSON value node.
*/
public static UITextEntry<JsonTree> createValueEditor() {
UITextEntry.Formatter<JsonTree> formatter = value -> value.getValue().toString();
UITextEntry.Parser<JsonTree> parser = value -> {
try {
Double valueDouble = Double.parseDouble(value);
return new JsonTree(new JsonTreeValue(null, valueDouble, JsonTreeValue.Type.VALUE));
} catch (NumberFormatException e) {
if ("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value)) {
return new JsonTree(new JsonTreeValue(null, Boolean.parseBoolean(value), JsonTreeValue.Type.VALUE));
} else {
return new JsonTree(new JsonTreeValue(null, value, JsonTreeValue.Type.VALUE));
}
}
};
return createEditorEntry(formatter, parser);
}
use of org.terasology.nui.widgets.treeView.JsonTree in project Terasology by MovingBlocks.
the class NUIEditorTextEntryBuilder method createArrayEditor.
/**
* @return A {@link UITextEntry} to be used to edit a JSON array node.
*/
public static UITextEntry<JsonTree> createArrayEditor() {
UITextEntry.Formatter<JsonTree> formatter = value -> value.getValue().getKey();
UITextEntry.Parser<JsonTree> parser = value -> new JsonTree(new JsonTreeValue(value, null, JsonTreeValue.Type.ARRAY));
return createEditorEntry(formatter, parser);
}
use of org.terasology.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);
}
use of org.terasology.nui.widgets.treeView.JsonTree in project Terasology by MovingBlocks.
the class NUIEditorNodeUtils method createNewScreen.
/**
* @return The {@link JsonTree} to be used as an initial screen template within {@link NUIEditorScreen}.
*/
public static JsonTree createNewScreen() {
JsonTree tree = new JsonTree(new JsonTreeValue(null, null, JsonTreeValue.Type.OBJECT));
tree.addChild(new JsonTreeValue("type", "PlaceholderScreen", JsonTreeValue.Type.KEY_VALUE_PAIR));
tree.addChild(new JsonTreeValue("skin", "engine:default", JsonTreeValue.Type.KEY_VALUE_PAIR));
JsonTree layout = new JsonTree(new JsonTreeValue("contents", null, JsonTreeValue.Type.OBJECT));
layout.addChild(new JsonTreeValue("type", "RelativeLayout", JsonTreeValue.Type.KEY_VALUE_PAIR));
JsonTree contents = new JsonTree(new JsonTreeValue("contents", null, JsonTreeValue.Type.ARRAY));
JsonTree label = createNewWidget("UILabel", "sampleLabel", true);
label.addChild(new JsonTreeValue("text", SAMPLE_LABEL_TEXT, JsonTreeValue.Type.KEY_VALUE_PAIR));
contents.addChild(label);
layout.addChild(contents);
tree.addChild(layout);
return tree;
}
Aggregations