From 9888dfe678328bdc610d87ee8eb136c5c29a5490 Mon Sep 17 00:00:00 2001 From: Zukero Date: Fri, 24 Feb 2017 17:45:34 +0100 Subject: [PATCH] Introducing the WorkspaceSettings, workspace-wide user-modifiable settings. No UI yet, so it's useless for now. --- .../rpg/atcontentstudio/ATContentStudio.java | 2 +- .../rpg/atcontentstudio/model/Workspace.java | 4 + .../model/WorkspaceSettings.java | 169 ++++++++++++++++++ 3 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 src/com/gpl/rpg/atcontentstudio/model/WorkspaceSettings.java diff --git a/src/com/gpl/rpg/atcontentstudio/ATContentStudio.java b/src/com/gpl/rpg/atcontentstudio/ATContentStudio.java index 40f5bde..fbb2ab8 100644 --- a/src/com/gpl/rpg/atcontentstudio/ATContentStudio.java +++ b/src/com/gpl/rpg/atcontentstudio/ATContentStudio.java @@ -22,7 +22,7 @@ import com.gpl.rpg.atcontentstudio.ui.WorkspaceSelector; public class ATContentStudio { public static final String APP_NAME = "Andor's Trail Content Studio"; - public static final String APP_VERSION = "v0.5.1"; + public static final String APP_VERSION = "v0.5.2"; public static boolean STARTED = false; public static StudioFrame frame = null; diff --git a/src/com/gpl/rpg/atcontentstudio/model/Workspace.java b/src/com/gpl/rpg/atcontentstudio/model/Workspace.java index 28794fa..d91301e 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/Workspace.java +++ b/src/com/gpl/rpg/atcontentstudio/model/Workspace.java @@ -35,6 +35,7 @@ public class Workspace implements ProjectTreeNode, Serializable { public Preferences preferences = new Preferences(); public File baseFolder; public File settingsFile; + public transient WorkspaceSettings settings; public transient List projects = new ArrayList(); public Set projectsName = new HashSet(); public Map projectsOpenByName = new HashMap(); @@ -53,6 +54,7 @@ public class Workspace implements ProjectTreeNode, Serializable { e.printStackTrace(); } } + settings = new WorkspaceSettings(this); settingsFile = new File(workspaceRoot, WS_SETTINGS_FILE); if (!settingsFile.exists()) { try { @@ -89,6 +91,7 @@ public class Workspace implements ProjectTreeNode, Serializable { } public void save() { + settings.save(); SettingsSave.saveInstance(this, settingsFile, "Workspace"); } @@ -229,6 +232,7 @@ public class Workspace implements ProjectTreeNode, Serializable { } public void refreshTransients() { + this.settings = new WorkspaceSettings(this); this.projects = new ArrayList(); Set projectsFailed = new HashSet(); for (String projectName : projectsName) { diff --git a/src/com/gpl/rpg/atcontentstudio/model/WorkspaceSettings.java b/src/com/gpl/rpg/atcontentstudio/model/WorkspaceSettings.java new file mode 100644 index 0000000..3ca4dda --- /dev/null +++ b/src/com/gpl/rpg/atcontentstudio/model/WorkspaceSettings.java @@ -0,0 +1,169 @@ +package com.gpl.rpg.atcontentstudio.model; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import com.gpl.rpg.atcontentstudio.ATContentStudio; +import com.gpl.rpg.atcontentstudio.Notification; +import com.gpl.rpg.atcontentstudio.io.JsonPrettyWriter; + +public class WorkspaceSettings { + + public static final String VERSION_KEY = "ATCS_Version"; + public static final String FILENAME = "workspace_settings.json"; + + public Workspace parent; + public File file; + + public static Boolean DEFAULT_USE_SYS_MAP_EDITOR = true; + public Setting useSystemDefaultMapEditor = new Setting("useSystemDefaultMapEditor", DEFAULT_USE_SYS_MAP_EDITOR); + public static String DEFAULT_MAP_EDITOR_COMMAND = "tiled"; + public Setting mapEditorCommand = new Setting("mapEditorCommand", DEFAULT_MAP_EDITOR_COMMAND); + + public static Boolean DEFAULT_USE_SYS_IMG_VIEWER = true; + public Setting useSystemDefaultImageViewer = new Setting("useSystemDefaultImageViewer", DEFAULT_USE_SYS_MAP_EDITOR); + public static Boolean DEFAULT_USE_SYS_IMG_EDITOR = true; + public Setting useSystemDefaultImageEditor = new Setting("useSystemDefaultImageEditor", DEFAULT_USE_SYS_MAP_EDITOR); + public static String DEFAULT_IMG_EDITOR_COMMAND = "gimp"; + public Setting imageEditorCommand = new Setting("imageEditorCommand", DEFAULT_MAP_EDITOR_COMMAND); + + public List> settings = new ArrayList>(); + + public WorkspaceSettings(Workspace parent) { + this.parent = parent; + settings.add(useSystemDefaultMapEditor); + settings.add(mapEditorCommand); + settings.add(useSystemDefaultImageViewer); + settings.add(useSystemDefaultImageEditor); + settings.add(imageEditorCommand); + file = new File(parent.baseFolder, FILENAME); + if (file.exists()) { + load(file); + } + } + + public void load(File f) { + JSONParser parser = new JSONParser(); + FileReader reader = null; + try { + reader = new FileReader(f); + @SuppressWarnings("rawtypes") + Map jsonSettings = (Map) parser.parse(reader); + String version = (String) jsonSettings.get(VERSION_KEY); + if (version != null) { + if ("v0.5.2".equals(version)) { + loadv052(jsonSettings); + } + } + + } catch (FileNotFoundException e) { + Notification.addError("Error while parsing workspace settings: "+e.getMessage()); + e.printStackTrace(); + } catch (IOException e) { + Notification.addError("Error while parsing workspace settings: "+e.getMessage()); + e.printStackTrace(); + } catch (ParseException e) { + Notification.addError("Error while parsing workspace settings: "+e.getMessage()); + e.printStackTrace(); + } finally { + if (reader != null) + try { + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + @SuppressWarnings("rawtypes") + private void loadv052(Map jsonSettings) { + for (Setting s : settings) { + s.readFromJson(jsonSettings); + } + } + + @SuppressWarnings("unchecked") + public void save() { + @SuppressWarnings("rawtypes") + Map json = new LinkedHashMap(); + for (Setting s : settings) { + s.saveToJson(json); + } + + if (json.isEmpty()) { + //Everything is default. + file.delete(); + return; + } + + json.put(VERSION_KEY, ATContentStudio.APP_VERSION); + StringWriter writer = new JsonPrettyWriter(); + try { + JSONObject.writeJSONString(json, writer); + } catch (IOException e) { + //Impossible with a StringWriter + } + String toWrite = writer.toString(); + try { + FileWriter w = new FileWriter(file); + w.write(toWrite); + w.close(); + Notification.addSuccess("Workspace settings saved."); + } catch (IOException e) { + Notification.addError("Error while saving workspace settings : "+e.getMessage()); + e.printStackTrace(); + } + } + + public void resetDefault() { + for (Setting s : settings) { + s.resetDefault(); + } + } + + class Setting { + + X value, defaultValue; + String id; + + public Setting(String id, X defaultValue) { + this.id = id; + this.value = this.defaultValue = defaultValue; + } + + public X getCurrentValue() { + return value; + } + + public X getDefaultValue() { + return defaultValue; + } + + public void resetDefault() { + value = defaultValue; + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public void readFromJson(Map json) { + value = (X)json.get(id); + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public void saveToJson(Map json) { + if (!defaultValue.equals(value)) json.put(id, value); + } + } + +}