diff --git a/build.gradle b/build.gradle index bfa2f5c..da0d3af 100644 --- a/build.gradle +++ b/build.gradle @@ -27,6 +27,9 @@ sourceSets { java { srcDir 'src/test' } + resources { + srcDir "src/resources" + } } } test { diff --git a/src/main/data/Options.java b/src/main/data/Options.java deleted file mode 100644 index a97600b..0000000 --- a/src/main/data/Options.java +++ /dev/null @@ -1,75 +0,0 @@ -package data; - -import java.util.prefs.Preferences; -import data.Const; - -public class Options { - private Preferences store; - private boolean wasEmpty; - private static final String DEF_OPTS_PATH = ""; - - public Options() { - //store = Preferences.userRoot(); - //wasEmpty = store.nodeExists(Const.PROGRAM_NAME); - //store = store.node(Const.PROGRAM_NAME); - //if (wasEmpty) { - // //load default opts from resource - // importDefPreferences(); - //} - } - - public Options(String pathname) { - store = Preferences.userRoot().node(pathname); - } - - public Options getSection(String section) { - //String path = Const.PROGRAM_NAME + "/" + section; - //Options result = new Options(path); - //return result; - return null; - } - - public void imoprtPreferences(String xmlpath) { - //XXX - System.out.println("Loaded preference from: " - + xmlpath); - } - - public void imoprtDefPreferences(String xmlpath) { - //XXX - System.out.println("Loaded default preference: "); - } - - public void exportPreferences(String xmlpath) { - //XXX - System.out.println("Exported preference from: " - + xmlpath); - } - - public void destroy() { - //XXX - // Clear config if requested or config was empty - // and is not saving - //if ((getBool(EMPTY) && getBool(NO_SAVE)) - // || getBool(CLEAR)) { - // this.store.clear(); - // } - } - - // Default all false bool pref to be safe - // Default should be imported by constructor regardless - public boolean getBool(String key) { - return this.store.getBoolean(key, false); - } - - // Default all empty string pref to be safe - // Default should be imported by constructor regardless - public String getString(String key) { - return this.store.get(key, ""); - } - - public void parseArgs(String[] args) { - //XXX - } - -} diff --git a/src/main/ui/Iface.java b/src/main/ui/Iface.java index 9672305..ef9a902 100644 --- a/src/main/ui/Iface.java +++ b/src/main/ui/Iface.java @@ -1,6 +1,6 @@ package ui; -import data.Options; +import ui.Options; public interface Iface { //private Options ifaceOpts; diff --git a/src/main/ui/IfaceFactory.java b/src/main/ui/IfaceFactory.java index e347d52..54e555f 100644 --- a/src/main/ui/IfaceFactory.java +++ b/src/main/ui/IfaceFactory.java @@ -1,6 +1,6 @@ package ui; -import data.Options; +import ui.Options; import ui.Tui; public class IfaceFactory { diff --git a/src/main/ui/Main.java b/src/main/ui/Main.java index 6f74df1..d472956 100644 --- a/src/main/ui/Main.java +++ b/src/main/ui/Main.java @@ -1,7 +1,7 @@ package ui; import data.Const; -import data.Options; +import ui.Options; import data.StypeMap; import data.StockType; import data.WatchList; diff --git a/src/main/ui/Options.java b/src/main/ui/Options.java new file mode 100644 index 0000000..8ca6d59 --- /dev/null +++ b/src/main/ui/Options.java @@ -0,0 +1,150 @@ +package ui; + +import java.util.prefs.Preferences; +import java.util.prefs.BackingStoreException; +import java.util.prefs.InvalidPreferencesFormatException; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.FileNotFoundException; +import java.io.InputStream; +import data.Const; + +public class Options { + private Preferences store; + private boolean wasEmpty; + private boolean noSave; + private static final String DEF_OPTS_PATH = ""; + private static final String DEF_BACKUP = ".jwatch-backup.xml"; + private static final String RES_DEF = "defPref.xml"; + + public Options() { + store = Preferences.userRoot(); + try { + wasEmpty = store.nodeExists(Const.PROGRAM_NAME); + store = store.node(Const.PROGRAM_NAME); + if (wasEmpty) { + //load default opts from resource + //XXX + //importDefPreferences(); + } + } catch (BackingStoreException e) { + System.out.println("Critical Error: preference backing cannot be initialized"); + throw new RuntimeException(); + } + } + + public Options(String pathname) { + store = Preferences.userRoot().node(pathname); + } + + public Options getSection(String section) { + //String path = Const.PROGRAM_NAME + "/" + section; + //Options result = new Options(path); + //return result; + return null; + } + + public void importPreferences(String xmlpath) { + try { + File tarFile = new File(xmlpath); + FileInputStream tarStream = new FileInputStream(tarFile); + store.importPreferences(tarStream); + tarStream.close(); + } catch (FileNotFoundException e) { + System.out.println("File not found: " + xmlpath); + } catch (IOException e) { + System.out.println("IO Error when reading: " + xmlpath); + e.printStackTrace(); + } catch (InvalidPreferencesFormatException e) { + System.out.println("Import format error, possible file corruption"); + } + } + + public void importDefPreferences() { + try { + InputStream istream = getClass().getResourceAsStream(RES_DEF); + store.importPreferences(istream); + istream.close(); + } catch (IOException e) { + System.out.println("IO Error while reading def pref: " + + RES_DEF); + } catch (InvalidPreferencesFormatException e) { + System.out.println("Def resPref format error, possible program corruption"); + } + } + + public void exportPreferences(String xmlpath) { + try { + File tarFile = new File(xmlpath); + if (tarFile.isFile()) { + tarFile.delete(); + } else { + tarFile.createNewFile(); + } + FileOutputStream tarStream = new FileOutputStream(tarFile); + store.exportSubtree(tarStream); + tarStream.flush(); + tarStream.close(); + System.out.println("Exported preference to: " + xmlpath); + } catch (IOException e) { + System.out.println("IO Error when writing: " + xmlpath); + } catch (BackingStoreException e) { + System.out.println("Error retriving pref from store"); + e.printStackTrace(); + } + } + + public void destroy() { + // Clear config if requested or config was empty + // and is not saving + if (wasEmpty && noSave) { + try { + this.store.clear(); + } catch (BackingStoreException e) { + System.out.println("Error clearing pref store"); + e.printStackTrace(); + } + } else if (noSave) { + restorePrevious(); + } + delBackup(); + } + + public void backupPref() { + exportPreferences(DEF_BACKUP); + System.out.println("User Pref Backup created"); + } + + public void delBackup() { + try { + File tarFile = new File(DEF_BACKUP); + tarFile.delete(); + } catch (Exception e) { + System.out.println("Error deleting backup user pref"); + e.printStackTrace(); + } + } + + public void restorePrevious() { + importPreferences(DEF_BACKUP); + } + + // Default all false bool pref to be safe + // Default should be imported by constructor regardless + public boolean getBool(String key) { + return this.store.getBoolean(key, false); + } + + // Default all empty string pref to be safe + // Default should be imported by constructor regardless + public String getString(String key) { + return this.store.get(key, ""); + } + + public void parseArgs(String[] args) { + //XXX + } + +}