use of org.terasology.rendering.nui.widgets.treeView.JsonTree in project Terasology by MovingBlocks.
the class AbstractEditorScreen method pasteJson.
/**
* Attempts to serialize the system clipboard's contents - if successful,
* sets the current state of the editor to the serialized {@link JsonTree}.
*/
protected void pasteJson() {
// Get the clipboard contents.
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable t = clipboard.getContents(null);
// Attempt to convert them to a string.
String clipboardContents = null;
try {
if (t != null) {
clipboardContents = (String) t.getTransferData(DataFlavor.stringFlavor);
}
} catch (UnsupportedFlavorException | IOException e) {
logger.warn("Could not fetch clipboard contents.", e);
}
if (clipboardContents != null) {
try {
// Attempt to serialize them to a JsonTree and reset the editor state.
JsonElement json = new JsonParser().parse(clipboardContents);
JsonTree node = JsonTreeConverter.serialize(json);
resetState(node);
} catch (JsonSyntaxException | NullPointerException e) {
logger.warn("Could not construct a valid tree from clipboard contents.", e);
}
}
}
use of org.terasology.rendering.nui.widgets.treeView.JsonTree in project Terasology by MovingBlocks.
the class AbstractEditorScreen method loadAutosave.
/**
* Resets the editor based on the state of the autosave file.
*/
protected void loadAutosave() {
if (!disableAutosave) {
try (JsonReader reader = new JsonReader(new InputStreamReader(Files.newInputStream(getAutosaveFile())))) {
reader.setLenient(true);
String autosaveString = new JsonParser().parse(reader).toString();
JsonObject autosaveObject = new JsonParser().parse(autosaveString).getAsJsonObject();
String selectedAsset = autosaveObject.get("selectedAsset").getAsString();
setSelectedAsset(selectedAsset);
try {
ResourceUrn urn = new ResourceUrn(selectedAsset);
setSelectedAssetPath(urn);
} catch (InvalidUrnException ignored) {
}
JsonTree editorContents = JsonTreeConverter.serialize(autosaveObject.get("editorContents"));
resetState(editorContents);
setUnsavedChangesPresent(true);
} catch (NoSuchFileException ignored) {
} catch (IOException e) {
logger.warn("Could not load autosaved info", e);
}
}
}
use of org.terasology.rendering.nui.widgets.treeView.JsonTree in project Terasology by MovingBlocks.
the class NUIEditorScreen method selectAsset.
/**
* {@inheritDoc}
*/
@Override
public void selectAsset(ResourceUrn urn) {
boolean isLoaded = assetManager.isLoaded(urn, UIElement.class);
Optional<UIElement> asset = assetManager.getAsset(urn, UIElement.class);
if (asset.isPresent()) {
UIElement element = asset.get();
if (!isLoaded) {
asset.get().dispose();
}
AssetDataFile source = element.getSource();
String content = null;
try (JsonReader reader = new JsonReader(new InputStreamReader(source.openStream(), Charsets.UTF_8))) {
reader.setLenient(true);
content = new JsonParser().parse(reader).toString();
} catch (IOException e) {
logger.error(String.format("Could not load asset source file for %s", urn.toString()), e);
}
if (content != null) {
JsonTree node = JsonTreeConverter.serialize(new JsonParser().parse(content));
selectedAssetPending = urn.toString();
resetState(node);
}
}
}
use of org.terasology.rendering.nui.widgets.treeView.JsonTree in project Terasology by MovingBlocks.
the class NUISkinEditorScreen method addWidget.
/**
* {@inheritDoc}
*/
@Override
protected void addWidget(JsonTree node) {
getManager().pushScreen(WidgetSelectionScreen.ASSET_URI, WidgetSelectionScreen.class);
// Initialise and show the widget selection screen.
WidgetSelectionScreen widgetSelectionScreen = (WidgetSelectionScreen) getManager().getScreen(WidgetSelectionScreen.ASSET_URI);
widgetSelectionScreen.setNode(node);
widgetSelectionScreen.subscribeClose(() -> {
JsonTree widget = node.getChildAt(node.getChildren().size() - 1);
widget.setExpanded(true);
getEditor().fireUpdateListeners();
});
}
use of org.terasology.rendering.nui.widgets.treeView.JsonTree in project Terasology by MovingBlocks.
the class NUISkinEditorScreen method resetPreviewWidget.
/**
* {@inheritDoc}
*/
@Override
public void resetPreviewWidget() {
if (selectedScreen != null) {
try {
// Construct a UISkinData instance.
JsonElement skinElement = JsonTreeConverter.deserialize(getEditor().getRoot());
UISkinData data = new UISkinFormat().load(skinElement);
// Get the selected screen asset.
Optional<UIElement> sourceAsset = assetManager.getAsset(selectedScreen, UIElement.class);
if (!sourceAsset.isPresent()) {
throw new FileNotFoundException(String.format("Asset %s not found", selectedScreen));
}
AssetDataFile source = sourceAsset.get().getSource();
String content;
try (JsonReader reader = new JsonReader(new InputStreamReader(source.openStream(), Charsets.UTF_8))) {
reader.setLenient(true);
content = new JsonParser().parse(reader).toString();
}
if (content != null) {
JsonTree node = JsonTreeConverter.serialize(new JsonParser().parse(content));
JsonElement screenElement = JsonTreeConverter.deserialize(node);
UIWidget widget = new UIFormat().load(screenElement, alternativeLocale).getRootWidget();
// Set the screen's skin using the previously generated UISkinData.
widget.setSkin(Assets.generateAsset(data, UISkin.class));
selectedScreenBox.setContent(widget);
}
} catch (Throwable t) {
String truncatedStackTrace = Joiner.on(System.lineSeparator()).join(Arrays.copyOfRange(ExceptionUtils.getStackFrames(t), 0, 10));
selectedScreenBox.setContent(new UILabel(truncatedStackTrace));
}
}
}
Aggregations