001: /*
002: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License version
007: * 2 only, as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License version 2 for more details (a copy is
013: * included at /legal/license.txt).
014: *
015: * You should have received a copy of the GNU General Public License
016: * version 2 along with this work; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018: * 02110-1301 USA
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
021: * Clara, CA 95054 or visit www.sun.com if you need additional
022: * information or have any questions.
023: */
024:
025: package com.sun.jumpimpl.module.preferences;
026:
027: import java.util.Vector;
028: import java.util.Properties;
029: import java.util.Enumeration;
030: import java.io.File;
031:
032: import com.sun.jump.module.contentstore.JUMPData;
033: import com.sun.jump.module.contentstore.JUMPNode;
034: import com.sun.jump.module.contentstore.JUMPStoreHandle;
035:
036: /**
037: * An implementation of preferences with "save state"
038: *
039: * An instance of PersistentPreferences shadows an optional
040: * default set of preferences. It keeps track of changes,
041: * and saves conditionally only of there are changes to the prefs.
042: */
043:
044: class PersistentPreferences {
045:
046: private Properties defaultProps = null;
047: private Properties props = null;
048: private boolean changed = false;
049: private String filename = null;
050: private File f = null;
051:
052: private static boolean debug = false;
053:
054: PersistentPreferences(String filename, JUMPStoreHandle storeHandle) {
055: this (null, filename, storeHandle);
056: }
057:
058: PersistentPreferences(PersistentPreferences defaultPrefs,
059: String filename, JUMPStoreHandle storeHandle) {
060: if (defaultPrefs != null) {
061: this .defaultProps = defaultPrefs.getProps();
062: this .props = new Properties(this .defaultProps);
063: } else {
064: this .props = new Properties();
065: }
066: this .filename = filename;
067: this .changed = false; // No need to save copy if default is there
068: initializeProps(storeHandle);
069: }
070:
071: private void initializeProps(JUMPStoreHandle storeHandle) {
072: // If corresponding 'filename' exists, make sure to load it in
073: // but keep the defaults in
074: try {
075: if (storeHandle.getNode(filename) != null) {
076: if (debug)
077: System.err
078: .println("File " + f + " exists, loading");
079: load(storeHandle);
080: } else {
081: storeHandle.createDataNode(filename, new JUMPData(
082: new Properties()));
083: }
084: } catch (Exception e) {
085: e.printStackTrace();
086: }
087: }
088:
089: void delete(String key) {
090: props.remove(key);
091: changed = true;
092: }
093:
094: Properties getProps() {
095: return props;
096: }
097:
098: String[] getNames() {
099: return PersistentPreferences.getNames(props.propertyNames());
100: }
101:
102: private static String[] getNames(Enumeration e) {
103: Vector v = new Vector();
104: int i = 0;
105: while (e.hasMoreElements()) {
106: String key = (String) e.nextElement();
107: v.addElement(key);
108: i++;
109: }
110: String[] s = new String[i];
111: v.copyInto(s);
112: return s;
113: }
114:
115: String get(String key) {
116: return props.getProperty(key);
117: }
118:
119: void set(String key, String value) {
120: props.setProperty(key, value);
121: changed = true;
122: }
123:
124: void save(JUMPStoreHandle storeHandle) {
125: // Nothing to do
126: if (!changed)
127: return;
128:
129: JUMPData propertiesData = new JUMPData(props);
130:
131: if (debug)
132: System.err.println("Storing Prefs to \"" + filename + "\"");
133:
134: try {
135: storeHandle.updateDataNode(filename, propertiesData);
136: } catch (Exception e) {
137: e.printStackTrace();
138: }
139: }
140:
141: void load(JUMPStoreHandle storeHandle) {
142:
143: JUMPNode prefsNode = null;
144: JUMPData prefsData = null;
145:
146: try {
147: prefsNode = storeHandle.getNode(filename);
148: if (prefsNode != null && prefsNode.containsData()) {
149: prefsData = ((JUMPNode.Data) prefsNode).getData();
150: props = (Properties) prefsData.getValue();
151: if (debug) {
152: System.err.println("PREFS LOADED FROM \""
153: + filename + "\"");
154: props.list(System.err);
155: }
156: }
157: } catch (Exception e) {
158: e.printStackTrace();
159: }
160:
161: }
162:
163: }
|