Skip to content

Commit

Permalink
Merge pull request #20 from Rockdtben/AddAutoSaveOption
Browse files Browse the repository at this point in the history
Users can now toggle autosave on and off.
  • Loading branch information
Rockdtben authored Feb 3, 2018
2 parents 12dd20d + 48d19f7 commit a15882c
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 19 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
37 changes: 21 additions & 16 deletions src/main/java/com/github/monster860/fastdmm/AutosaveDaemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
41 changes: 38 additions & 3 deletions src/main/java/com/github/monster860/fastdmm/FastDMM.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -93,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;

Expand Down Expand Up @@ -124,7 +129,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"));
Expand All @@ -143,7 +148,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() {
Expand Down Expand Up @@ -285,6 +301,7 @@ public void stateChanged(ChangeEvent e) {
menu.add(menuItemMapImage);

initRecent("dme");
initOptions();

menu = new JMenu("Edit");
menuBar.add(menu);
Expand All @@ -309,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");
Expand Down Expand Up @@ -417,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())) {
Expand Down Expand Up @@ -598,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) {
Expand Down Expand Up @@ -725,7 +756,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();
}
Expand Down Expand Up @@ -1190,6 +1221,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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

public 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);
}
}
}

0 comments on commit a15882c

Please sign in to comment.