parent
3629449c0f
commit
bc7542de7a
@ -1,150 +0,0 @@
|
|||||||
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…
Reference in new issue