Search in sources :

Example 1 with GameDataExporter

use of games.strategy.engine.data.export.GameDataExporter in project triplea by triplea-game.

the class ExportMenu method exportXmlFile.

private void exportXmlFile() {
    final JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    final File rootDir = new File(SystemProperties.getUserDir());
    final int round = gameData.getCurrentRound();
    String defaultFileName = "xml_" + dateTimeFormatter.format(LocalDateTime.now()) + "_" + gameData.getGameName() + "_round_" + round;
    defaultFileName = FileNameUtils.removeIllegalCharacters(defaultFileName);
    defaultFileName = defaultFileName + ".xml";
    chooser.setSelectedFile(new File(rootDir, defaultFileName));
    if (chooser.showSaveDialog(frame) != JOptionPane.OK_OPTION) {
        return;
    }
    final String xmlFile;
    try {
        gameData.acquireReadLock();
        final GameDataExporter exporter = new GameDataExporter(gameData);
        xmlFile = exporter.getXml();
    } finally {
        gameData.releaseReadLock();
    }
    try (Writer writer = Files.newBufferedWriter(chooser.getSelectedFile().toPath(), StandardCharsets.UTF_8)) {
        writer.write(xmlFile);
    } catch (final IOException e1) {
        ClientLogger.logQuietly("Failed to write XML: " + chooser.getSelectedFile().getAbsolutePath(), e1);
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) IOException(java.io.IOException) File(java.io.File) Writer(java.io.Writer) GameDataExporter(games.strategy.engine.data.export.GameDataExporter)

Aggregations

GameDataExporter (games.strategy.engine.data.export.GameDataExporter)1 File (java.io.File)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 JFileChooser (javax.swing.JFileChooser)1