Source Code Cross Referenced for Preferences.java in  » Internationalization-Localization » RBManager » com » ibm » rbm » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Internationalization Localization » RBManager » com.ibm.rbm 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.