-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from Rockdtben/AddAutoSaveOption
Users can now toggle autosave on and off.
- Loading branch information
Showing
4 changed files
with
137 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/main/java/com/github/monster860/fastdmm/gui/model/FastDMMOptionsModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |