Nearly completed Options, moved over to ui

deliverable_5
asdfasdf 6 years ago
parent bc604a3a53
commit e6eef8899f

@ -27,6 +27,9 @@ sourceSets {
java {
srcDir 'src/test'
}
resources {
srcDir "src/resources"
}
}
}
test {

@ -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
}
}

@ -1,6 +1,6 @@
package ui;
import data.Options;
import ui.Options;
public interface Iface {
//private Options ifaceOpts;

@ -1,6 +1,6 @@
package ui;
import data.Options;
import ui.Options;
import ui.Tui;
public class IfaceFactory {

@ -1,7 +1,7 @@
package ui;
import data.Const;
import data.Options;
import ui.Options;
import data.StypeMap;
import data.StockType;
import data.WatchList;

@ -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
}
}
Loading…
Cancel
Save