From 97ae63693a78aff7a07fc0ebee5fbcc292d182ae Mon Sep 17 00:00:00 2001 From: Zukero Date: Thu, 2 Mar 2017 15:30:02 +0100 Subject: [PATCH] Enhanced Tiled integration. ATCS offers to save a modified map before opening in Tiled, and attempts to save a backup of the externally-modified one when saving in ATCS if necessary. --- .../rpg/atcontentstudio/ui/map/TMXMapEditor.java | 13 +++++++++++++ .../gpl/rpg/atcontentstudio/utils/FileUtils.java | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/com/gpl/rpg/atcontentstudio/ui/map/TMXMapEditor.java b/src/com/gpl/rpg/atcontentstudio/ui/map/TMXMapEditor.java index 923934e..2b0f407 100644 --- a/src/com/gpl/rpg/atcontentstudio/ui/map/TMXMapEditor.java +++ b/src/com/gpl/rpg/atcontentstudio/ui/map/TMXMapEditor.java @@ -18,6 +18,7 @@ import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import java.awt.event.MouseMotionListener; +import java.io.File; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; @@ -101,6 +102,7 @@ import com.gpl.rpg.atcontentstudio.ui.FieldUpdateListener; import com.gpl.rpg.atcontentstudio.ui.IntegerBasedCheckBox; import com.gpl.rpg.atcontentstudio.ui.ScrollablePanel; import com.gpl.rpg.atcontentstudio.utils.DesktopIntegration; +import com.gpl.rpg.atcontentstudio.utils.FileUtils; import com.jidesoft.swing.JideBoxLayout; import com.jidesoft.swing.JideTabbedPane; @@ -1613,6 +1615,11 @@ public class TMXMapEditor extends Editor implements TMXMap.MapChangedOnDiskListe gdeIcon.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + if (map.state == GameDataElement.State.modified) { + int confirm = JOptionPane.showConfirmDialog(TMXMapEditor.this, "You modified this map in ATCS.\nYou'd better save your changes in ATCS before opening this map with the external editor.\nDo you want to save before opening the file?", "Save before opening?", JOptionPane.YES_NO_CANCEL_OPTION); + if (confirm == JOptionPane.CANCEL_OPTION) return; + if (confirm == JOptionPane.YES_OPTION) map.save(); + } DesktopIntegration.openTmxMap(map.tmxFile); } }); @@ -1648,6 +1655,12 @@ public class TMXMapEditor extends Editor implements TMXMap.MapChangedOnDiskListe if (map.changedOnDisk) { int confirm = JOptionPane.showConfirmDialog(TMXMapEditor.this, "You modified this map in an external tool. All external changes will be lost if you confirm.\n On the other hand, if you reload in ATCS, all ATCS-made changes will be lost.\n Do you want to save?", "Confirm save?", JOptionPane.OK_CANCEL_OPTION); if (confirm == JOptionPane.CANCEL_OPTION) return; + File backup = FileUtils.backupFile(map.tmxFile); + if (backup != null) { + JOptionPane.showMessageDialog(TMXMapEditor.this, "The externally-modified file was backed up as "+backup.getAbsolutePath(), "File backed up", JOptionPane.INFORMATION_MESSAGE); + } else { + JOptionPane.showMessageDialog(TMXMapEditor.this, "The externally-modified file could not be backed up.", "File backup failed", JOptionPane.ERROR_MESSAGE); + } } map.save(); ATContentStudio.frame.nodeChanged(map); diff --git a/src/com/gpl/rpg/atcontentstudio/utils/FileUtils.java b/src/com/gpl/rpg/atcontentstudio/utils/FileUtils.java index 1a4e76e..0025e93 100644 --- a/src/com/gpl/rpg/atcontentstudio/utils/FileUtils.java +++ b/src/com/gpl/rpg/atcontentstudio/utils/FileUtils.java @@ -9,9 +9,11 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.file.CopyOption; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -154,5 +156,15 @@ public class FileUtils { } return true; } + + public static File backupFile(File f) { + try { + Path returned = Files.copy(Paths.get(f.getAbsolutePath()), Paths.get(f.getAbsolutePath()+".bak"), StandardCopyOption.REPLACE_EXISTING); + return returned.toFile(); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } }