// 3/26/06 import java.util.*; import javax.microedition.rms.*; /** * This class manages a set of user preferences, and stores them in a RecordStore. A * single Preferences object is maintained by this class, and can be obtained using * the static getinstance() method. Preferences are stored as key/value pairs * (of Strings). A value may be requested as a String using the, * * public String getpreference (String key) * * method, or as an int using the. * * public int getintpreference (String key) * * method. These methods are available: * * getinstance() obtain the singleton Preferences object. * * getpreference (String key) obtain the corresponding value as a String. * * getintpreference (String key) obtain the corresponding value as an int. * * removepreference (String key) remove the Preferences key and its value. * * save() store the preferences in the Record Store. * * setdefaults (String[][]) the parameter is a 2xN array of Strings, * where each row of the array is a key/value * pair. For each key in the array that is NOT * already a Preference, the key/value pair * will be added as a Preference. * * setpreference (String key, add (overwrite) the key/value pair. * String value) * * setpreference (String key, add (overwrite) the key/value pair, after * int value) converting the int to a String. */ public class Preferences { static private final int BUFSIZE = 100, KEY = 0, VALUE = 1, ERROR = -1; static private String STORENAME = "PREFS"; static private Preferences instance_; private boolean exists_, haschanged_; private Hashtable table_; static public Preferences getinstance() { if (instance_ == null) { instance_ = new Preferences(); } return instance_; } static public boolean ispref (String field, String value) { Preferences prefs; prefs = getinstance(); return prefs.getpreference (field).equals (value); } private Preferences() { byte buf[]; RecordEnumeration records; RecordStore recstore; String strings[]; table_ = new Hashtable(); exists_ = false; recstore = null; records = null; try { ////////////////////////////////////////////////////////////////// // Try to open the RecordStore. It may or may not exist. // // If it doesn't exist a RecordStoreNotFoundException results // ////////////////////////////////////////////////////////////////// recstore = RecordStore.openRecordStore (STORENAME, false); exists_ = true; ////////////////////////////////////////////////////////// // Obtain an enumeration of the records in the store // // No filter, no comparer, don't keep updated. // ////////////////////////////////////////////////////////// records = recstore.enumerateRecords (null, null, false); strings = new String[2]; while (records.hasNextElement()) { buf = records.nextRecord(); makestrings (buf, strings); table_.put (strings[KEY], strings[VALUE]); } } catch (RecordStoreException e) { } finally { if (records != null) { records.destroy(); } if (recstore != null) { try { recstore.closeRecordStore(); } catch (RecordStoreException e) { } } } } public int getintpreference (String key) { int pref; String value; value = (String) table_.get (key); if (value != null) { pref = Integer.parseInt (value); } else { pref = ERROR; } return pref; } public String getpreference (String key) { return (String) table_.get (key); } private int makebuffer (String key, String value, byte buf[]) { int len; len = stringintobuf (buf, key, 0); len = stringintobuf (buf, value, len); return len; } private void makestrings (byte buf[], String strings[]) { int len, offset; len = buf[0]; strings[KEY] = new String (buf, 1, len); offset = len + 1; len = buf[offset++]; strings[VALUE] = new String (buf, offset, len); } public void removepreference (String key) { table_.remove (key); haschanged_ = true; } public void save() throws RecordStoreException { byte buf[]; int len; Enumeration keys; RecordStore recstore; String key, value; if (haschanged_) { recstore = null; try { if (exists_) { RecordStore.deleteRecordStore (STORENAME); } recstore = RecordStore.openRecordStore (STORENAME, true); buf = new byte[BUFSIZE]; keys = table_.keys(); while (keys.hasMoreElements()) { key = (String) keys.nextElement(); value = (String) table_.get (key); len = makebuffer (key, value, buf); recstore.addRecord (buf, 0, len); } haschanged_ = false; exists_ = true; } catch (RecordStoreException e) { throw e; } finally { if (recstore != null) { recstore.closeRecordStore(); } } } } public void setdefaults (String table[][]) { int len, pos; String key; len = table.length; for (pos = 0; pos < len; pos++) { key = table[pos][KEY]; if (!table_.containsKey (key)) { table_.put (key, table[pos][VALUE]); haschanged_ = true; } } } public void setpreference (String key, int value) { setpreference (key, value + ""); } public void setpreference (String key, String value) { instance_ = getinstance(); table_.put (key, value); haschanged_ = true; } private int stringintobuf (byte buf[], String text, int offset) { byte bytes[]; int len; len = text.length(); bytes = text.getBytes(); buf[offset] = (byte) len; System.arraycopy (bytes, 0, buf, offset + 1, len); return offset + len + 1; } }