Source Code Cross Referenced for AppProperties.java in  » IDE » tIDE » snow » utils » storage » 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 » IDE » tIDE » snow.utils.storage 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package snow.utils.storage;
002:
003:        import java.util.*;
004:        import java.io.*;
005:        import java.awt.Component;
006:        import java.awt.Font;
007:        import java.awt.Color;
008:
009:        /** Customized properties (some getters/setters) should remain compatible with Properties.
010:         *   Is stored in XML format, in a file.
011:         */
012:        @SuppressWarnings("unchecked")
013:        public class AppProperties extends Properties {
014:
015:            public AppProperties() {
016:                super ();
017:            }
018:
019:            public void load_XML(File f) {
020:                if (!f.exists())
021:                    return;
022:
023:                FileInputStream fis = null;
024:                try {
025:                    fis = new FileInputStream(f);
026:                    this .loadFromXML(fis);
027:                } catch (Exception e) {
028:                    e.printStackTrace();
029:                } finally {
030:                    FileUtils.closeIgnoringExceptions(fis);
031:                }
032:            }
033:
034:            public void save_XML(File f) {
035:                FileOutputStream fos = null;
036:                try {
037:                    fos = new FileOutputStream(f);
038:                    this .storeToXML(fos, "AppProperties");
039:                } catch (Exception e) {
040:                    e.printStackTrace();
041:                } finally {
042:                    FileUtils.closeIgnoringExceptions(fos);
043:                }
044:            }
045:
046:            public final void loadFromStoredVectorRepresentation(File file) {
047:                FileInputStream fis = null;
048:                try {
049:                    fis = new FileInputStream(file);
050:                    List rep = FileUtils.loadVector(fis);
051:                    this .createFromVectorRepresentation(rep, true);
052:                    fis.close();
053:                } catch (Exception ex) {
054:                    ex.printStackTrace();
055:                } finally {
056:                    FileUtils.closeIgnoringExceptions(fis);
057:                }
058:            }
059:
060:            public final void storeToFileAsVectorRepresentation(File file) {
061:                try {
062:                    FileUtils.saveVectorToFile(file, getVectorRepresentation());
063:                } catch (Exception ignored) {
064:                    ignored.printStackTrace();
065:                }
066:            }
067:
068:            //
069:            //
070:
071:            public void setArrayProperty(String key, List<String> values) {
072:                setArrayProperty(key, values.toArray(new String[values.size()]));
073:            }
074:
075:            public void setArrayProperty(String key, String[] values) {
076:                setProperty(key + "length", "" + values.length);
077:                for (int i = 0; i < values.length; i++) {
078:                    setProperty(key + (i), values[i]); //NOT ROBUST :-(, collides if a key ends with a number !!
079:                }
080:            }
081:
082:            /** @return a modifiable list.
083:             */
084:            public List<String> getArrayProperty(String key,
085:                    final List<String> defaults) {
086:                if (defaults == null) {
087:                    return getArrayProperty(key, (String[]) null);
088:                }
089:                return getArrayProperty(key, defaults
090:                        .toArray(new String[defaults.size()]));
091:            }
092:
093:            /** @return a modifiable list. Null if not found.
094:             */
095:            public List<String> getArrayProperty(String key) {
096:                return getArrayProperty(key, (String[]) null);
097:            }
098:
099:            /** @return a modifiable list.
100:             */
101:            public List<String> getArrayProperty(String key, String[] defaults) {
102:                List<String> ap = new ArrayList<String>();
103:                String found = getProperty(key + "length", "no");
104:                if (found.equals("no")) {
105:                    if (defaults != null) {
106:                        ap.addAll(Arrays.asList(defaults));
107:                    }
108:                    return ap;
109:                }
110:                int n = Integer.parseInt(found);
111:
112:                String[] props = new String[n];
113:                for (int i = 0; i < n; i++) {
114:                    ap.add(getProperty(key + (i), "oh la la la la"));
115:                }
116:                return ap;
117:            }
118:
119:            public void setInteger(String key, int val) {
120:                this .put(key, "" + val);
121:            }
122:
123:            public void setLong(String key, long val) {
124:                this .put(key, "" + val);
125:            }
126:
127:            public void setDouble(String key, double val) {
128:                this .put(key, "" + val);
129:            }
130:
131:            public Font getFont(String key, Font def) {
132:                String name = this .getProperty(key + "_name", "?");
133:                if (name.equals("?"))
134:                    return def;
135:
136:                int style = this .getInteger(key + "_style", Font.PLAIN);
137:                int size = this .getInteger(key + "_size", 12);
138:
139:                return new Font(name, style, size);
140:            }
141:
142:            public Color getColor(String key, Color def) {
143:                int r = this .getInteger(key + "_r", -1);
144:                if (r == -1)
145:                    return def;
146:                int g = this .getInteger(key + "_g", 0);
147:                int b = this .getInteger(key + "_b", 0);
148:                int a = this .getInteger(key + "_a", 0);
149:                return new Color(r, g, b, a);
150:            }
151:
152:            public void setColor(String key, Color val) {
153:                if (val == null) {
154:                    this .remove(key + "_r");
155:                    this .remove(key + "_g");
156:                    this .remove(key + "_b");
157:                    this .remove(key + "_a");
158:
159:                } else {
160:                    this .setInteger(key + "_r", val.getRed());
161:                    this .setInteger(key + "_g", val.getGreen());
162:                    this .setInteger(key + "_b", val.getBlue());
163:                    this .setInteger(key + "_a", val.getAlpha());
164:                }
165:            }
166:
167:            public void setFont(String key, Font font) {
168:                if (font == null) {
169:                    this .remove(key + "_name");
170:                    this .remove(key + "_style");
171:                    this .remove(key + "_size");
172:                } else {
173:                    this .put(key + "_name", font.getName());
174:                    this .setInteger(key + "_style", font.getStyle());
175:                    this .setInteger(key + "_size", font.getSize());
176:                }
177:            }
178:
179:            /** this set the key
180:             */
181:            public void setStringLCK(String key, String val) {
182:                this .put(key.toLowerCase(), val);
183:            }
184:
185:            public int getInteger(String key, int def) {
186:                String found = getProperty(key, "no");
187:                if (found.equals("no"))
188:                    return def;
189:                try {
190:                    int n = Integer.parseInt(found);
191:                    return n;
192:                } catch (Exception e) {
193:                    return def;
194:                }
195:            }
196:
197:            public long getLong(String key, long def) {
198:                String found = getProperty(key, "no");
199:                if (found.equals("no"))
200:                    return def;
201:                try {
202:                    long n = Long.parseLong(found);
203:                    return n;
204:                } catch (Exception e) {
205:                    return def;
206:                }
207:            }
208:
209:            public double getDouble(String key, double def) {
210:                String found = getProperty(key, "no");
211:                if (found.equals("no"))
212:                    return def;
213:                try {
214:                    double n = Double.parseDouble(found);
215:                    return n;
216:                } catch (Exception e) {
217:                    return def;
218:                }
219:            }
220:
221:            public void setBoolean(String key, boolean val) {
222:                this .put(key, (val ? "true" : "false"));
223:            }
224:
225:            public boolean getBoolean(String key, boolean def) {
226:                String found = getProperty(key, "no");
227:                if (found.equals("no"))
228:                    return def;
229:                return (found.equals("true"));
230:            }
231:
232:            /** lowercase key
233:             */
234:            public String getStringLCK(String key, String def) {
235:                String found = getProperty(key.toLowerCase(), "no");
236:                if (found.equals("no"))
237:                    return def;
238:                return found;
239:            }
240:
241:            /** set the component size and location
242:             */
243:            public synchronized void setComponentSizeFromINIFile(
244:                    Component comp, String key, int defWidth, int defHeight,
245:                    int defPosX, int defPosY) {
246:                int w = getInteger(key + ".width", defWidth);
247:                int h = getInteger(key + ".height", defHeight);
248:                int x = getInteger(key + ".posx", defPosX);
249:                int y = getInteger(key + ".posy", defPosY);
250:
251:                comp.setSize(w, h);
252:                comp.setLocation(x, y);
253:            }
254:
255:            /** save the component size and location
256:             */
257:            public synchronized void saveComponentSizeInINIFile(Component comp,
258:                    String key) {
259:                this 
260:                        .setInteger(key + ".width", (int) comp.getSize()
261:                                .getWidth());
262:                this .setInteger(key + ".height", (int) comp.getSize()
263:                        .getHeight());
264:                this .setInteger(key + ".posx", (int) comp.getLocation().getX());
265:                this .setInteger(key + ".posy", (int) comp.getLocation().getY());
266:            }
267:
268:            /** set the component location
269:             */
270:            public synchronized void setComponentLocationFromINIFile(
271:                    Component comp, String key, int defPosX, int defPosY) {
272:                int x = getInteger(key + ".posx", defPosX);
273:                int y = getInteger(key + ".posy", defPosY);
274:
275:                comp.setLocation(x, y);
276:            }
277:
278:            /** save the component location
279:             */
280:            public synchronized void saveComponentLocationInINIFile(
281:                    Component comp, String key) {
282:                this .setInteger(key + ".posx", (int) comp.getLocation().getX());
283:                this .setInteger(key + ".posy", (int) comp.getLocation().getY());
284:            }
285:
286:            public StorageVector getVectorRepresentation() {
287:                StorageVector v = new StorageVector();
288:                v.add(1); // version
289:                v.add(this .size());
290:                Enumeration<Object> enum2 = this .keys();
291:                while (enum2.hasMoreElements()) {
292:                    String obj = "" + enum2.nextElement();
293:                    v.add(obj);
294:                    v.add(getProperty(obj));
295:                }
296:                return v;
297:            }
298:
299:            /** clears and recreate from the passed vector
300:             */
301:            public void createFromVectorRepresentation(List<Object> v,
302:                    boolean clearRep) {
303:                this .clear();
304:
305:                int ver = (Integer) v.get(0);
306:                if (ver != 1)
307:                    throw new RuntimeException("version =" + ver
308:                            + " not supported");
309:                int size = (Integer) v.get(1);
310:                for (int i = 0; i < size; i++) {
311:                    this .setProperty((String) v.get(2 * i + 2), (String) v
312:                            .get(2 * i + 3));
313:                }
314:
315:                if (clearRep) {
316:                    v.clear();
317:                }
318:            }
319:
320:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.