001: /*
002: *****************************************************************************
003: * Copyright (C) 2000-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *****************************************************************************
006: */
007: package com.ibm.rbm;
008:
009: import java.util.*;
010: import java.io.*;
011:
012: /**
013: * This class defines the methods used by RBManager to access, set, and store
014: * individual user preferences for the application. All of the public methods defined
015: * in this class are static, and so the class need not be instantiated.
016: *
017: * @author Jared Jackson
018: * @see com.ibm.rbm.RBManager
019: */
020: public class Preferences {
021: // Default values
022: private static final int NUM_RECENT_FILES = 4;
023: private static final String EMPTY_STRING = "";
024: private static Properties prop;
025:
026: /**
027: * Retrieve a preference by its key name
028: * @param name The name of the key associated with one preference
029: * @return The value of the preference sought
030: */
031:
032: public static String getPreference(String name) {
033: if (prop == null)
034: init();
035: Object o = prop.get(name);
036: if (o == null || !(o instanceof String))
037: return EMPTY_STRING;
038: return (String) o;
039: }
040:
041: /**
042: * Sets a preference by key name and value. If the key name all ready exists, that
043: * preference is overwritten without warning.
044: * @param name The name of the key associated with the preference
045: * @param value The value of the preference to be set and later retrieved. If this value is null, the property of this name is erased.
046: */
047:
048: public static void setPreference(String name, String value) {
049: if (prop == null)
050: init();
051: if (value == null) {
052: // In this case, we will remove the property
053: prop.remove(name);
054: }
055: prop.put(name, value);
056: }
057:
058: /**
059: * Writes the results of the buffered preferences to file. There is no option for
060: * where this file is saved on the file system.
061: */
062:
063: public static void savePreferences() throws IOException {
064: if (prop == null)
065: init();
066: FileOutputStream fos = new FileOutputStream(
067: "preferences.properties");
068: prop.store(fos, "RBManager Preferences");
069: fos.flush();
070: fos.close();
071: }
072:
073: /**
074: * Given the name of a resource bundle and the file path location of the base
075: * document for that resource bundle, this method will insert that file into
076: * a list of recent files. Currently the past 4 resource bundles visited will
077: * be displayed. This method also sorts the prefences so that the most recently
078: * added will be the first returned, even if that file had all ready existed
079: * in the preferences when it was added.
080: * @param name The name of this file as it will be displayed to the user
081: * @param location The file path to this file (should be absolute).
082: */
083:
084: public static void addRecentFilePreference(String name,
085: String location) {
086: Vector existingNames = new Vector();
087: Vector existingLocations = new Vector();
088: for (int i = 0; i < NUM_RECENT_FILES; i++) {
089: String oldName = getPreference("recentfileid"
090: + String.valueOf(i));
091: String oldLocation = getPreference("recentfileloc"
092: + String.valueOf(i));
093: if (oldName.equals(EMPTY_STRING)
094: || oldLocation.equals(EMPTY_STRING))
095: break;
096: existingNames.addElement(oldName);
097: existingLocations.addElement(oldLocation);
098: }
099: // Check to see if the file is all ready in there
100: int swap_start = 0;
101: int old_size = existingLocations.size();
102: for (int i = 0; i <= old_size; i++) {
103: if (i == existingLocations.size()) {
104: // No match was found, pull all the elements down one
105: swap_start = i;
106: if (swap_start >= NUM_RECENT_FILES)
107: swap_start = NUM_RECENT_FILES - 1;
108: else {
109: // Extend the length of the vectors
110: existingNames.addElement(EMPTY_STRING);
111: existingLocations.addElement(EMPTY_STRING);
112: }
113: } else {
114: String oldLocation = (String) existingLocations
115: .elementAt(i);
116: if (oldLocation.equals(location)) {
117: // We found a match, pull this one to the front
118: swap_start = i;
119: break;
120: }
121: }
122: }
123:
124: // Move the files down the line as appropriate
125: for (int i = swap_start; i > 0; i--) {
126: existingLocations.setElementAt(existingLocations
127: .elementAt(i - 1), i);
128: existingNames.setElementAt(existingNames.elementAt(i - 1),
129: i);
130: }
131: existingLocations.setElementAt(location, 0);
132: existingNames.setElementAt(name, 0);
133:
134: // Set the properties
135: for (int i = 0; i < existingLocations.size(); i++) {
136: setPreference("recentfileid" + String.valueOf(i),
137: (String) existingNames.elementAt(i));
138: setPreference("recentfileloc" + String.valueOf(i),
139: (String) existingLocations.elementAt(i));
140: }
141: for (int i = existingLocations.size(); i < NUM_RECENT_FILES; i++) {
142: setPreference("recentfileid" + String.valueOf(i),
143: EMPTY_STRING);
144: setPreference("recentfileloc" + String.valueOf(i),
145: EMPTY_STRING);
146: }
147: try {
148: savePreferences();
149: } catch (IOException ioe) {
150: } // Ignore, its not critical
151: }
152:
153: /**
154: * Returns a list of the names and locations of the various recently used files.
155: * @return A Vector of Strings which is twice in length the number of files known about. The vector contains name 1 then location 1, then name 2 ...
156: */
157:
158: public static Vector getRecentFilesPreferences() {
159: if (prop == null)
160: init();
161: Vector existing = new Vector();
162: for (int i = 0; i < NUM_RECENT_FILES; i++) {
163: String name = getPreference("recentfileid"
164: + String.valueOf(i));
165: String location = getPreference("recentfileloc"
166: + String.valueOf(i));
167: if (name.equals(EMPTY_STRING)
168: || location.equals(EMPTY_STRING))
169: break;
170: existing.addElement(name);
171: existing.addElement(location);
172: }
173: return existing;
174: }
175:
176: private static void init() {
177: Properties defaults = new Properties();
178: // This values are needed and are specified by default
179: // If they exist in the file, they will be overwritten
180: defaults.put("username", Resources
181: .getTranslation("unknown_user"));
182: defaults.put("locale", "en");
183: defaults.put("lookandfeel", "");
184:
185: prop = new Properties(defaults);
186: try {
187: FileInputStream fis = new FileInputStream(
188: "preferences.properties");
189: prop.load(fis);
190: } catch (IOException ioe) {
191: System.err.println("Error reading properties");
192: ioe.printStackTrace(System.err);
193: }
194: try {
195: savePreferences();
196: } catch (IOException ioe) {
197: System.err.println("Error saving preferences "
198: + ioe.getMessage());
199: }
200: }
201:
202: /*
203: public static void main(String args[]) {
204: // Test
205: init();
206: }
207: */
208: }
|