Search in sources :

Example 1 with FilePickerPopup

use of org.terasology.rendering.nui.layers.mainMenu.FilePickerPopup in project Terasology by MovingBlocks.

the class IdentityIOHelper method importIdentities.

public void importIdentities() {
    FilePickerPopup filePicker = nuiManager.pushScreen(FilePickerPopup.ASSET_URI, FilePickerPopup.class);
    filePicker.setTitle(importPopupTitle);
    filePicker.setOkHandler(path -> {
        Map<PublicIdentityCertificate, ClientIdentity> newIdentities;
        try (BufferedReader reader = Files.newBufferedReader(path)) {
            newIdentities = GSON.fromJson(reader, MAP_TYPE);
        } catch (IOException | JsonIOException | JsonSyntaxException ex) {
            nuiManager.pushScreen(MessagePopup.ASSET_URI, MessagePopup.class).setMessage(translationSystem.translate("${engine:menu#identity-import-failed}"), ex.toString());
            return;
        }
        checkNextConflict(newIdentities.entrySet().iterator(), () -> {
            newIdentities.forEach(securityConfig::addIdentity);
            nuiManager.pushScreen(MessagePopup.ASSET_URI, MessagePopup.class).setMessage(importPopupTitle, newIdentities.isEmpty() ? translationSystem.translate("${engine:menu#identity-import-no-new}") : String.format(translationSystem.translate("${engine:menu#identity-import-ok}"), newIdentities.size()));
        });
    });
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) ClientIdentity(org.terasology.identity.ClientIdentity) JsonIOException(com.google.gson.JsonIOException) BufferedReader(java.io.BufferedReader) MessagePopup(org.terasology.rendering.nui.layers.mainMenu.MessagePopup) FilePickerPopup(org.terasology.rendering.nui.layers.mainMenu.FilePickerPopup) IOException(java.io.IOException) JsonIOException(com.google.gson.JsonIOException) PublicIdentityCertificate(org.terasology.identity.PublicIdentityCertificate)

Example 2 with FilePickerPopup

use of org.terasology.rendering.nui.layers.mainMenu.FilePickerPopup in project Terasology by MovingBlocks.

the class IdentityIOHelper method exportIdentities.

public void exportIdentities() {
    FilePickerPopup filePicker = nuiManager.pushScreen(FilePickerPopup.ASSET_URI, FilePickerPopup.class);
    filePicker.setTitle(exportPopupTitle);
    filePicker.setOkHandler(path -> {
        Runnable action = () -> {
            Map<PublicIdentityCertificate, ClientIdentity> identities = securityConfig.getAllIdentities();
            try (BufferedWriter writer = Files.newBufferedWriter(path, StandardOpenOption.CREATE)) {
                GSON.toJson(identities, MAP_TYPE, writer);
                nuiManager.pushScreen(MessagePopup.ASSET_URI, MessagePopup.class).setMessage(exportPopupTitle, String.format(translationSystem.translate("${engine:menu#identity-export-ok}"), identities.size(), path.toString()));
            } catch (IOException | JsonIOException ex) {
                nuiManager.pushScreen(MessagePopup.ASSET_URI, MessagePopup.class).setMessage(translationSystem.translate("${engine:menu#identity-export-fail}"), ex.toString());
            }
        };
        if (Files.exists(path)) {
            ConfirmPopup confirm = nuiManager.pushScreen(ConfirmPopup.ASSET_URI, ConfirmPopup.class);
            confirm.setMessage(exportPopupTitle, String.format(translationSystem.translate("${engine:menu#existing-file-warning}"), path.toString()));
            confirm.setOkHandler(action);
        } else {
            action.run();
        }
    });
}
Also used : ConfirmPopup(org.terasology.rendering.nui.layers.mainMenu.ConfirmPopup) MessagePopup(org.terasology.rendering.nui.layers.mainMenu.MessagePopup) FilePickerPopup(org.terasology.rendering.nui.layers.mainMenu.FilePickerPopup) Map(java.util.Map) BufferedWriter(java.io.BufferedWriter)

Aggregations

FilePickerPopup (org.terasology.rendering.nui.layers.mainMenu.FilePickerPopup)2 MessagePopup (org.terasology.rendering.nui.layers.mainMenu.MessagePopup)2 JsonIOException (com.google.gson.JsonIOException)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1 Map (java.util.Map)1 ClientIdentity (org.terasology.identity.ClientIdentity)1 PublicIdentityCertificate (org.terasology.identity.PublicIdentityCertificate)1 ConfirmPopup (org.terasology.rendering.nui.layers.mainMenu.ConfirmPopup)1