1 /* 2 ***************************************************************************** 3 * Copyright (C) 2000-2004, International Business Machines Corporation and * 4 * others. All Rights Reserved. * 5 ***************************************************************************** 6 */ 7 package com.ibm.rbm; 8 9 import java.util.*; 10 import java.io.*; 11 12 /** 13 * This class defines the methods used by RBManager to access, set, and store 14 * individual user preferences for the application. All of the public methods defined 15 * in this class are static, and so the class need not be instantiated. 16 * 17 * @author Jared Jackson 18 * @see com.ibm.rbm.RBManager 19 */ 20 public class Preferences { 21 // Default values 22 private static final int NUM_RECENT_FILES = 4; 23 private static final String EMPTY_STRING = ""; 24 private static Properties prop; 25 26 /** 27 * Retrieve a preference by its key name 28 * @param name The name of the key associated with one preference 29 * @return The value of the preference sought 30 */ 31 getPreference(String name)32 public static String getPreference(String name) { 33 if (prop == null) init(); 34 Object o = prop.get(name); 35 if (o == null || !(o instanceof String)) return EMPTY_STRING; 36 return (String)o; 37 } 38 39 /** 40 * Sets a preference by key name and value. If the key name all ready exists, that 41 * preference is overwritten without warning. 42 * @param name The name of the key associated with the preference 43 * @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. 44 */ 45 setPreference(String name, String value)46 public static void setPreference(String name, String value) { 47 if (prop == null) init(); 48 if (value == null) { 49 // In this case, we will remove the property 50 prop.remove(name); 51 } 52 prop.put(name, value); 53 } 54 55 /** 56 * Writes the results of the buffered preferences to file. There is no option for 57 * where this file is saved on the file system. 58 */ 59 savePreferences()60 public static void savePreferences() throws IOException { 61 if (prop == null) init(); 62 FileOutputStream fos = new FileOutputStream("preferences.properties"); 63 prop.store(fos, "RBManager Preferences"); 64 fos.flush(); 65 fos.close(); 66 } 67 68 /** 69 * Given the name of a resource bundle and the file path location of the base 70 * document for that resource bundle, this method will insert that file into 71 * a list of recent files. Currently the past 4 resource bundles visited will 72 * be displayed. This method also sorts the prefences so that the most recently 73 * added will be the first returned, even if that file had all ready existed 74 * in the preferences when it was added. 75 * @param name The name of this file as it will be displayed to the user 76 * @param location The file path to this file (should be absolute). 77 */ 78 addRecentFilePreference(String name, String location)79 public static void addRecentFilePreference(String name, String location) { 80 Vector existingNames = new Vector(); 81 Vector existingLocations = new Vector(); 82 for (int i=0; i < NUM_RECENT_FILES; i++) { 83 String oldName = getPreference("recentfileid" + String.valueOf(i)); 84 String oldLocation = getPreference("recentfileloc" + String.valueOf(i)); 85 if (oldName.equals(EMPTY_STRING) || oldLocation.equals(EMPTY_STRING)) break; 86 existingNames.addElement(oldName); 87 existingLocations.addElement(oldLocation); 88 } 89 // Check to see if the file is all ready in there 90 int swap_start = 0; 91 int old_size = existingLocations.size(); 92 for (int i=0; i <= old_size; i++) { 93 if (i == existingLocations.size()) { 94 // No match was found, pull all the elements down one 95 swap_start = i; 96 if (swap_start >= NUM_RECENT_FILES) swap_start = NUM_RECENT_FILES-1; 97 else { 98 // Extend the length of the vectors 99 existingNames.addElement(EMPTY_STRING); 100 existingLocations.addElement(EMPTY_STRING); 101 } 102 } else { 103 String oldLocation = (String)existingLocations.elementAt(i); 104 if (oldLocation.equals(location)) { 105 // We found a match, pull this one to the front 106 swap_start = i; 107 break; 108 } 109 } 110 } 111 112 // Move the files down the line as appropriate 113 for (int i=swap_start; i > 0; i--) { 114 existingLocations.setElementAt(existingLocations.elementAt(i-1),i); 115 existingNames.setElementAt(existingNames.elementAt(i-1),i); 116 } 117 existingLocations.setElementAt(location, 0); 118 existingNames.setElementAt(name, 0); 119 120 // Set the properties 121 for (int i=0; i < existingLocations.size(); i++) { 122 setPreference("recentfileid" + String.valueOf(i), (String)existingNames.elementAt(i)); 123 setPreference("recentfileloc" + String.valueOf(i), (String)existingLocations.elementAt(i)); 124 } 125 for (int i=existingLocations.size(); i < NUM_RECENT_FILES; i++) { 126 setPreference("recentfileid" + String.valueOf(i), EMPTY_STRING); 127 setPreference("recentfileloc" + String.valueOf(i), EMPTY_STRING); 128 } 129 try { 130 savePreferences(); 131 } catch (IOException ioe) {} // Ignore, its not critical 132 } 133 134 /** 135 * Returns a list of the names and locations of the various recently used files. 136 * @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 ... 137 */ 138 getRecentFilesPreferences()139 public static Vector getRecentFilesPreferences() { 140 if (prop == null) init(); 141 Vector existing = new Vector(); 142 for (int i=0; i < NUM_RECENT_FILES; i++) { 143 String name = getPreference("recentfileid" + String.valueOf(i)); 144 String location = getPreference("recentfileloc" + String.valueOf(i)); 145 if (name.equals(EMPTY_STRING) || location.equals(EMPTY_STRING)) break; 146 existing.addElement(name); 147 existing.addElement(location); 148 } 149 return existing; 150 } 151 init()152 private static void init() { 153 Properties defaults = new Properties(); 154 // This values are needed and are specified by default 155 // If they exist in the file, they will be overwritten 156 defaults.put("username", Resources.getTranslation("unknown_user")); 157 defaults.put("locale", "en"); 158 defaults.put("lookandfeel", ""); 159 160 prop = new Properties(defaults); 161 try { 162 FileInputStream fis = new FileInputStream("preferences.properties"); 163 prop.load(fis); 164 } catch (IOException ioe) { 165 System.err.println("Error reading properties"); 166 ioe.printStackTrace(System.err); 167 } 168 try { 169 savePreferences(); 170 } catch (IOException ioe) { 171 System.err.println("Error saving preferences " + ioe.getMessage()); 172 } 173 } 174 175 /* 176 public static void main(String args[]) { 177 // Test 178 init(); 179 } 180 */ 181 } 182