Skip to content

Commit

Permalink
Persist option settings.
Browse files Browse the repository at this point in the history
Default AutoSave to Off
  • Loading branch information
Rockdtben committed Feb 3, 2018
1 parent 12dd20d commit d514459
Show file tree
Hide file tree
Showing 4 changed files with 121 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
25 changes: 22 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 Down Expand Up @@ -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"));
Expand All @@ -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() {
Expand Down Expand Up @@ -285,6 +299,7 @@ public void stateChanged(ChangeEvent e) {
menu.add(menuItemMapImage);

initRecent("dme");
initOptions();

menu = new JMenu("Edit");
menuBar.add(menu);
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
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;
}
}

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

0 comments on commit d514459

Please sign in to comment.