Search in sources :

Example 1 with JsonTree

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);
        }
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree) JsonElement(com.google.gson.JsonElement) Transferable(java.awt.datatransfer.Transferable) Clipboard(java.awt.datatransfer.Clipboard) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) JsonParser(com.google.gson.JsonParser)

Example 2 with JsonTree

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);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InvalidUrnException(org.terasology.assets.exceptions.InvalidUrnException) JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree) NoSuchFileException(java.nio.file.NoSuchFileException) JsonReader(com.google.gson.stream.JsonReader) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) ResourceUrn(org.terasology.assets.ResourceUrn) JsonParser(com.google.gson.JsonParser)

Example 3 with JsonTree

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);
        }
    }
}
Also used : UIElement(org.terasology.rendering.nui.asset.UIElement) InputStreamReader(java.io.InputStreamReader) JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree) AssetDataFile(org.terasology.assets.format.AssetDataFile) JsonReader(com.google.gson.stream.JsonReader) IOException(java.io.IOException) JsonParser(com.google.gson.JsonParser)

Example 4 with JsonTree

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();
    });
}
Also used : JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree)

Example 5 with JsonTree

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));
        }
    }
}
Also used : UILabel(org.terasology.rendering.nui.widgets.UILabel) UISkinFormat(org.terasology.rendering.nui.skin.UISkinFormat) UIElement(org.terasology.rendering.nui.asset.UIElement) InputStreamReader(java.io.InputStreamReader) UIFormat(org.terasology.rendering.nui.asset.UIFormat) JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree) UISkinData(org.terasology.rendering.nui.skin.UISkinData) UISkin(org.terasology.rendering.nui.skin.UISkin) FileNotFoundException(java.io.FileNotFoundException) UIWidget(org.terasology.rendering.nui.UIWidget) JsonElement(com.google.gson.JsonElement) AssetDataFile(org.terasology.assets.format.AssetDataFile) JsonReader(com.google.gson.stream.JsonReader) JsonParser(com.google.gson.JsonParser)

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