From d514459150ac26cd58941664f2f49c254c277064 Mon Sep 17 00:00:00 2001 From: Rockdtben Date: Fri, 2 Feb 2018 19:16:41 -0600 Subject: [PATCH 1/2] Persist option settings. Default AutoSave to Off --- build.gradle | 2 + .../monster860/fastdmm/AutosaveDaemon.java | 37 +++++---- .../github/monster860/fastdmm/FastDMM.java | 25 +++++- .../gui/model/FastDMMOptionsModel.java | 76 +++++++++++++++++++ 4 files changed, 121 insertions(+), 19 deletions(-) create mode 100644 src/main/java/com/github/monster860/fastdmm/gui/model/FastDMMOptionsModel.java diff --git a/build.gradle b/build.gradle index 8a875b7..990dccf 100644 --- a/build.gradle +++ b/build.gradle @@ -25,6 +25,8 @@ dependencies { compile group: 'org.lwjgl.lwjgl', name: 'lwjgl', version: '2.9.3' compile group: 'org.lwjgl.lwjgl', name: 'lwjgl_util', version: '2.9.3' compile group: 'org.json', name: 'json', version: '20160810' + compile group: 'com.google.code.gson', name: 'gson', version: '2.8.2' + compile group: 'commons-io', name:'commons-io', version:'+' compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.16.10' compile group: 'junit', name: 'junit', version: '4.11' } diff --git a/src/main/java/com/github/monster860/fastdmm/AutosaveDaemon.java b/src/main/java/com/github/monster860/fastdmm/AutosaveDaemon.java index 0f78075..7bd7948 100644 --- a/src/main/java/com/github/monster860/fastdmm/AutosaveDaemon.java +++ b/src/main/java/com/github/monster860/fastdmm/AutosaveDaemon.java @@ -5,34 +5,39 @@ public class AutosaveDaemon implements Runnable { - private FastDMM editor; - - public AutosaveDaemon(FastDMM editor) + public AutosaveDaemon() { - this.editor = editor; } @Override public void run() { + FastDMM editor = FastDMM.getFastDMM(); while(true) { try { Thread.sleep(60000); - synchronized(editor) + + //Autosave only if enabled. + if (editor.options.autoSave) { - editor.loadedMaps.forEach((dmm) -> - { - try - { - dmm.save(); - } - catch(FileNotFoundException e) - { - JOptionPane.showMessageDialog(editor, "Got FileNotFoundException while saving " + dmm.file.getName(), "Error", JOptionPane.ERROR_MESSAGE); - } - }); + synchronized(editor) + { + editor.loadedMaps.forEach((dmm) -> + { + try + { + dmm.save(); + + //TODO: Add some Console like panel to report stuff like this to the user. "Map AutoSaved" + } + catch(FileNotFoundException e) + { + JOptionPane.showMessageDialog(editor, "Got FileNotFoundException while saving " + dmm.file.getName(), "Error", JOptionPane.ERROR_MESSAGE); + } + }); + } } } catch(InterruptedException e) diff --git a/src/main/java/com/github/monster860/fastdmm/FastDMM.java b/src/main/java/com/github/monster860/fastdmm/FastDMM.java index 49a371b..5594a70 100644 --- a/src/main/java/com/github/monster860/fastdmm/FastDMM.java +++ b/src/main/java/com/github/monster860/fastdmm/FastDMM.java @@ -35,6 +35,7 @@ import com.github.monster860.fastdmm.editing.ui.EmptyTabPanel; import com.github.monster860.fastdmm.editing.ui.NoDmeTreeModel; import com.github.monster860.fastdmm.editing.ui.ObjectTreeRenderer; +import com.github.monster860.fastdmm.gui.model.FastDMMOptionsModel; import com.github.monster860.fastdmm.objtree.InstancesRenderer; import com.github.monster860.fastdmm.objtree.ModifiedType; import com.github.monster860.fastdmm.objtree.ObjInstance; @@ -71,6 +72,8 @@ public class FastDMM extends JFrame implements ActionListener, TreeSelectionList boolean selMode = false; public String statusstring = " "; + + public FastDMMOptionsModel options; private JPanel leftPanel; private JPanel objTreePanel; @@ -124,7 +127,7 @@ public static final void main(String[] args) throws IOException, LWJGLException e.printStackTrace(); } - FastDMM fastdmm = new FastDMM(); + FastDMM fastdmm = FastDMM.getFastDMM(); fastdmm.initSwing(); fastdmm.interface_dmi = new DMI(Util.getFile("interface.dmi")); @@ -143,7 +146,18 @@ public static final void main(String[] args) throws IOException, LWJGLException } } - public FastDMM() { + //FastDMM is now a singleton. + private FastDMM() { + } + + //FastDMM is now a singleton. + private static FastDMM fastDMM; + + public static FastDMM getFastDMM() { + if (fastDMM == null) + return fastDMM = new FastDMM(); + else + return fastDMM; } public void initSwing() { @@ -285,6 +299,7 @@ public void stateChanged(ChangeEvent e) { menu.add(menuItemMapImage); initRecent("dme"); + initOptions(); menu = new JMenu("Edit"); menuBar.add(menu); @@ -725,7 +740,7 @@ private void init() throws LWJGLException { if (interface_dmi != null) { interface_dmi.createGL(); } - Thread autosaveThread = new Thread(new AutosaveDaemon(this)); + Thread autosaveThread = new Thread(new AutosaveDaemon()); autosaveThread.setDaemon(true); autosaveThread.start(); } @@ -1190,6 +1205,10 @@ private void addToRecent(File dme, DMM dmm) { JOptionPane.showMessageDialog(FastDMM.this, sw.getBuffer(), "Error", JOptionPane.ERROR_MESSAGE); } } + + private void initOptions() { + options = FastDMMOptionsModel.createOrLoadOptions(); + } private void initRecent(String mode) { String recentPath = System.getProperty("user.home") + File.separator + ".fastdmm" + File.separator diff --git a/src/main/java/com/github/monster860/fastdmm/gui/model/FastDMMOptionsModel.java b/src/main/java/com/github/monster860/fastdmm/gui/model/FastDMMOptionsModel.java new file mode 100644 index 0000000..6076851 --- /dev/null +++ b/src/main/java/com/github/monster860/fastdmm/gui/model/FastDMMOptionsModel.java @@ -0,0 +1,76 @@ +package com.github.monster860.fastdmm.gui.model; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.Objects; + +import javax.swing.JOptionPane; + +import org.apache.commons.io.FileUtils; +import org.json.JSONObject; +import org.json.JSONTokener; + +import com.github.monster860.fastdmm.FastDMM; +import com.google.gson.Gson; + +public class FastDMMOptionsModel { + private static String optionsPath = System.getProperty("user.home") + File.separator + ".fastdmm" + File.separator + + "options.json"; + + public boolean autoSave = false; + + //Restriction constructor to createOrLoadOptions. + private FastDMMOptionsModel() { } + + + public static FastDMMOptionsModel createOrLoadOptions() { + File optionsFile = new File(optionsPath); + + if (optionsFile.exists()) + { + String json = ""; + try { + json = FileUtils.readFileToString(optionsFile); + } catch (FileNotFoundException e) { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + e.printStackTrace(pw); + JOptionPane.showMessageDialog(FastDMM.getFastDMM(), sw.getBuffer(), "Error", JOptionPane.ERROR_MESSAGE); + } catch (IOException e) { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + e.printStackTrace(pw); + JOptionPane.showMessageDialog(FastDMM.getFastDMM(), sw.getBuffer(), "Error", JOptionPane.ERROR_MESSAGE); + } + Gson gson = new Gson(); + FastDMMOptionsModel options = gson.fromJson(json, FastDMMOptionsModel.class); + return options; + } + else + { + FastDMMOptionsModel options = new FastDMMOptionsModel(); + //Save Options to file. + options.saveOptions(); + return options; + } + } + + private void saveOptions() + { + File optionsFile = new File(optionsPath); + Gson gson = new Gson(); + String optionsJson = gson.toJson(this); + try { + FileUtils.writeStringToFile(optionsFile, optionsJson); + } catch (IOException e) { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + e.printStackTrace(pw); + JOptionPane.showMessageDialog(FastDMM.getFastDMM(), sw.getBuffer(), "Error", JOptionPane.ERROR_MESSAGE); + } + } +} From 48d19f761c8b43b923da5d05b67462e5f5d188e7 Mon Sep 17 00:00:00 2001 From: Rockdtben Date: Fri, 2 Feb 2018 19:34:53 -0600 Subject: [PATCH 2/2] Add AutoSave toggle option. --- .../com/github/monster860/fastdmm/FastDMM.java | 16 ++++++++++++++++ .../fastdmm/gui/model/FastDMMOptionsModel.java | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/github/monster860/fastdmm/FastDMM.java b/src/main/java/com/github/monster860/fastdmm/FastDMM.java index 5594a70..35ad291 100644 --- a/src/main/java/com/github/monster860/fastdmm/FastDMM.java +++ b/src/main/java/com/github/monster860/fastdmm/FastDMM.java @@ -96,6 +96,8 @@ public class FastDMM extends JFrame implements ActionListener, TreeSelectionList private JMenuItem menuItemMapImage; private JMenuItem menuItemUndo; private JMenuItem menuItemRedo; + + private JCheckBoxMenuItem menuItemAutoSave; private JPopupMenu currPopup; @@ -324,6 +326,13 @@ public void stateChanged(ChangeEvent e) { menuItem.setActionCommand("change_filters"); menuItem.addActionListener(FastDMM.this); menu.add(menuItem); + + menuItemAutoSave = new JCheckBoxMenuItem("AutoSave", options.autoSave); + menuItemAutoSave.setActionCommand("autoSaveToggle"); + menuItemAutoSave.addActionListener(FastDMM.this); + menuItemAutoSave.setSelected(options.autoSave); + + menu.add(menuItemAutoSave); menuItemExpand = new JMenuItem("Expand Map"); menuItemExpand.setActionCommand("expand"); @@ -432,6 +441,8 @@ public void actionPerformed(ActionEvent e) { } } else if ("open_dme".equals(e.getActionCommand())) { openDME(); + } else if ("autoSaveToggle".equals(e.getActionCommand())) { + autoSaveToggle(); } else if ("open".equals(e.getActionCommand())) { openDMM(); } else if ("save".equals(e.getActionCommand())) { @@ -613,6 +624,11 @@ private void openDME() { openDME(fc.getSelectedFile()); } } + + private void autoSaveToggle() { + options.autoSave = !options.autoSave; + options.saveOptions(); + } private void openDMM(File filetoopen) { synchronized (this) { diff --git a/src/main/java/com/github/monster860/fastdmm/gui/model/FastDMMOptionsModel.java b/src/main/java/com/github/monster860/fastdmm/gui/model/FastDMMOptionsModel.java index 6076851..c16581a 100644 --- a/src/main/java/com/github/monster860/fastdmm/gui/model/FastDMMOptionsModel.java +++ b/src/main/java/com/github/monster860/fastdmm/gui/model/FastDMMOptionsModel.java @@ -59,7 +59,7 @@ public static FastDMMOptionsModel createOrLoadOptions() { } } - private void saveOptions() + public void saveOptions() { File optionsFile = new File(optionsPath); Gson gson = new Gson();