001: /*
002: This file is part of BORG.
003:
004: BORG is free software; you can redistribute it and/or modify
005: it under the terms of the GNU General Public License as published by
006: the Free Software Foundation; either version 2 of the License, or
007: (at your option) any later version.
008:
009: BORG is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: GNU General Public License for more details.
013:
014: You should have received a copy of the GNU General Public License
015: along with BORG; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: Copyright 2003 by Mike Berger
019: */
020: package net.sf.borg.common;
021:
022: import java.io.InputStream;
023: import java.io.OutputStream;
024: import java.util.ArrayList;
025: import java.util.prefs.Preferences;
026:
027: /**
028: *
029: * Convenience class for retrieving preferences.
030: *
031: */
032: public class Prefs {
033:
034: public interface Listener {
035: public abstract void prefsChanged();
036: }
037:
038: static private ArrayList listeners = new ArrayList();
039:
040: static public void addListener(Listener listener) {
041: listeners.add(listener);
042: }
043:
044: static public void removeListener(Listener listener) {
045: listeners.remove(listener);
046: }
047:
048: // send a notification to all registered views
049: static public void notifyListeners() {
050: for (int i = 0; i < listeners.size(); i++) {
051: Listener v = (Listener) listeners.get(i);
052: v.prefsChanged();
053: }
054: }
055:
056: public static String getPref(PrefName pn) {
057: Object o = getPrefObject(pn);
058: if (o instanceof Integer)
059: return Integer.toString(((Integer) o).intValue());
060: return (String) getPrefObject(pn);
061: }
062:
063: public static int getIntPref(PrefName pn) {
064: return (((Integer) Prefs.getPrefObject(pn)).intValue());
065: }
066:
067: public static boolean getBoolPref(PrefName pn) {
068: String s = getPref(pn);
069: if (s != null && s.equals("true"))
070: return true;
071: return false;
072: }
073:
074: private static Object getPrefObject(PrefName pn) {
075: Preferences prefs = getPrefNode();
076: if (pn.getDefault() instanceof Integer) {
077: int val = prefs.getInt(pn.getName(), ((Integer) pn
078: .getDefault()).intValue());
079: return (new Integer(val));
080: }
081:
082: String val = prefs.get(pn.getName(), (String) pn.getDefault());
083: return (val);
084: }
085:
086: public static void putPref(PrefName pn, Object val) {
087:
088: // System.out.println("putpref-" + pn.getName() + "-" + val);
089: Preferences prefs = getPrefNode();
090: if (pn.getDefault() instanceof Integer) {
091: if (val instanceof Integer) {
092: prefs.putInt(pn.getName(), ((Integer) val).intValue());
093: } else {
094: prefs.putInt(pn.getName(), Integer
095: .parseInt((String) val));
096: }
097: } else {
098: prefs.put(pn.getName(), (String) val);
099: }
100: }
101:
102: static private Preferences getPrefNode() {
103: // hard code to original prefs location for backward compatiblity
104: Preferences root = Preferences.userRoot();
105: return root.node("net/sf/borg/common/util");
106: }
107:
108: private Prefs() {
109:
110: }
111:
112: public static void importPrefs(String filename) {
113: try {
114: InputStream istr = IOHelper.openStream(filename);
115: Preferences.importPreferences(istr);
116: istr.close();
117: } catch (Exception e) {
118: Errmsg.errmsg(e);
119: }
120: }
121:
122: public static void export(String filename) {
123: try {
124: OutputStream oostr = IOHelper.createOutputStream(filename);
125: Preferences prefs = getPrefNode();
126: prefs.exportNode(oostr);
127: oostr.close();
128: } catch (Exception e) {
129: Errmsg.errmsg(e);
130: }
131: }
132: }
|