Source Code Cross Referenced for ResourceFactory.java in  » Swing-Library » wings3 » org » wings » plaf » 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 » Swing Library » wings3 » org.wings.plaf 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2000,2005 wingS development team.
003:         *
004:         * This file is part of wingS (http://wingsframework.org).
005:         *
006:         * wingS is free software; you can redistribute it and/or modify
007:         * it under the terms of the GNU Lesser General Public License
008:         * as published by the Free Software Foundation; either version 2.1
009:         * of the License, or (at your option) any later version.
010:         *
011:         * Please see COPYING for the complete licence.
012:         */
013:
014:        package org.wings.plaf;
015:
016:        import java.awt.Color;
017:        import java.io.InputStream;
018:        import java.lang.reflect.Constructor;
019:        import java.util.HashMap;
020:        import java.util.Map;
021:        import java.util.Properties;
022:        import java.util.StringTokenizer;
023:
024:        import org.apache.commons.logging.Log;
025:        import org.apache.commons.logging.LogFactory;
026:        import org.wings.Resource;
027:        import org.wings.SDimension;
028:        import org.wings.SIcon;
029:        import org.wings.SResourceIcon;
030:        import org.wings.resource.ClassPathResource;
031:        import org.wings.style.CSSAttributeSet;
032:        import org.wings.style.CSSProperty;
033:        import org.wings.style.CSSStyleSheet;
034:        import org.wings.style.StyleSheet;
035:
036:        public class ResourceFactory {
037:            private final transient static Log log = LogFactory
038:                    .getLog(ResourceFactory.class);
039:
040:            private static final Map WRAPPERS = new HashMap();
041:            static {
042:                WRAPPERS.put(Boolean.TYPE, Boolean.class);
043:                WRAPPERS.put(Character.TYPE, Character.class);
044:                WRAPPERS.put(Byte.TYPE, Byte.class);
045:                WRAPPERS.put(Short.TYPE, Short.class);
046:                WRAPPERS.put(Integer.TYPE, Integer.class);
047:                WRAPPERS.put(Long.TYPE, Long.class);
048:                WRAPPERS.put(Float.TYPE, Float.class);
049:                WRAPPERS.put(Double.TYPE, Double.class);
050:            }
051:
052:            private final Properties properties;
053:
054:            public ResourceFactory(Properties properties) {
055:                this .properties = properties;
056:            }
057:
058:            public Object get(Object key, Class type) {
059:                String property;
060:                if (key instanceof  Class) {
061:                    Class clazz = (Class) key;
062:                    do {
063:                        property = properties.getProperty(clazz.getName());
064:                        clazz = clazz.getSuperclass();
065:                    } while (property == null && clazz != null);
066:                } else
067:                    property = properties.getProperty(key.toString());
068:
069:                if (property == null)
070:                    return null;
071:                if (ComponentCG.class.isAssignableFrom(type))
072:                    return makeComponentCG(property);
073:                else if (LayoutCG.class.isAssignableFrom(type))
074:                    return makeLayoutCG(property);
075:                else if (type.isAssignableFrom(SIcon.class))
076:                    return makeIcon(property);
077:                else if (type.isAssignableFrom(Resource.class))
078:                    return makeResource(property);
079:                else if (type.isAssignableFrom(CSSAttributeSet.class))
080:                    return makeAttributeSet(property);
081:                else if (type.isAssignableFrom(StyleSheet.class))
082:                    return makeStyleSheet(property);
083:                else if (type.isAssignableFrom(Color.class))
084:                    return makeColor(property);
085:                else if (type.isAssignableFrom(SDimension.class))
086:                    return makeDimension(property);
087:                else if (type.isAssignableFrom(Boolean.class))
088:                    return makeBoolean(property);
089:                else if (type.isAssignableFrom(Class.class))
090:                    return makeClass(property);
091:                else
092:                    return makeObject(property, type);
093:            }
094:
095:            private static Map instances = new HashMap();
096:
097:            private static Object sharedInstance(String className) {
098:                Object cg = instances.get(className);
099:                if (cg == null) {
100:                    try {
101:                        Class cgClass = Class.forName(className, true, Thread
102:                                .currentThread().getContextClassLoader());
103:                        cg = cgClass.newInstance();
104:                        instances.put(className, cg);
105:                    } catch (Exception ex) {
106:                        throw new RuntimeException(ex);
107:                    }
108:                }
109:                return cg;
110:            }
111:
112:            /**
113:             * Create a CG instance.
114:             *
115:             * @param className the full qualified class name of the CG
116:             * @return a new CG instance
117:             */
118:            public static ComponentCG makeComponentCG(String className) {
119:                return (ComponentCG) sharedInstance(className);
120:            }
121:
122:            /**
123:             * Create a CG instance.
124:             *
125:             * @param className the full qualified class name of the CG
126:             * @return a new CG instance
127:             */
128:            public static LayoutCG makeLayoutCG(String className) {
129:                return (LayoutCG) sharedInstance(className);
130:            }
131:
132:            /**
133:             * Utility method that creates an java.awt.Color from a html color hex string
134:             *
135:             * @return the create color
136:             */
137:            public static Color makeColor(String colorString) {
138:                if (colorString != null) {
139:                    try {
140:                        return Color.decode(colorString.trim());
141:                    } catch (Exception ex) {
142:                        throw new RuntimeException(ex);
143:                    }
144:                }
145:                return null;
146:            }
147:
148:            /**
149:             * Utility method that creates a dimension from a dimension string separated by comma 
150:             *
151:             * @return the create color
152:             */
153:            public static SDimension makeDimension(String dimensionString) {
154:                if (dimensionString != null) {
155:                    int commaIndex = dimensionString.indexOf(',');
156:                    if (commaIndex > 0) {
157:                        return new SDimension(dimensionString.substring(0,
158:                                commaIndex), dimensionString
159:                                .substring(commaIndex + 1));
160:                    }
161:                }
162:                return null;
163:            }
164:
165:            /**
166:             * Utility method that creates an Icon from a resource
167:             * located relative to the given base class. Uses the ClassLoader
168:             * of the LookAndFeel
169:             *
170:             * @param fileName of the image file
171:             * @return a newly allocated Icon
172:             */
173:            public static SIcon makeIcon(String fileName) {
174:                return new SResourceIcon(fileName);
175:            }
176:
177:            /**
178:             * Utility method that creates a Boolean from a String
179:             * Uses the ClassLoader of the LookAndFeel.
180:             *
181:             * @param bool The Boolean as String 
182:             * @return a newly allocated Icon
183:             */
184:            public static Boolean makeBoolean(String bool) {
185:                return Boolean.valueOf(bool);
186:            }
187:
188:            /**
189:             * Utility method that creates an CSSPropertySet from a String
190:             *
191:             * @param string attributes string
192:             * @return a newly allocated CSSPropertySet
193:             */
194:            public static CSSAttributeSet makeAttributeSet(String string) {
195:                CSSAttributeSet attributes = new CSSAttributeSet();
196:                StringTokenizer tokens = new StringTokenizer(string, ";");
197:                while (tokens.hasMoreTokens()) {
198:                    String token = tokens.nextToken();
199:                    int pos = token.indexOf(":");
200:                    if (pos >= 0) {
201:                        attributes.put(
202:                                new CSSProperty(token.substring(0, pos)), token
203:                                        .substring(pos + 1));
204:                    }
205:                }
206:                return attributes;
207:            }
208:
209:            /**
210:             * Utility method that creates a styleSheet from a string
211:             *
212:             * @param resourceName styleSheet as a string
213:             * @return the styleSheet
214:             */
215:            public static Resource makeResource(String resourceName) {
216:                return new ClassPathResource(resourceName);
217:            }
218:
219:            /**
220:             * Utility method that creates a stylesheet object from a resource
221:             *
222:             * @return the styleSheet
223:             */
224:            public static StyleSheet makeStyleSheet(String resourceName) {
225:                try {
226:                    CSSStyleSheet result = new CSSStyleSheet();
227:                    InputStream in = Thread.currentThread()
228:                            .getContextClassLoader().getResourceAsStream(
229:                                    resourceName);
230:                    result.read(in);
231:                    in.close();
232:                    return result;
233:                } catch (Exception e) {
234:                    throw new RuntimeException(e);
235:                }
236:            }
237:
238:            /**
239:             * Utility method that creates a Class from a resource
240:             * located realtive to the given base class. Uses the ClassLoader
241:             * of the LookAndFeel
242:             *
243:             * @param className name of the class
244:             * @return a class instance
245:             */
246:            public static Class makeClass(String className) {
247:                try {
248:                    return Class.forName(className, true, Thread
249:                            .currentThread().getContextClassLoader());
250:                } catch (ClassNotFoundException e) {
251:                    throw new RuntimeException(e);
252:                }
253:            }
254:
255:            /**
256:             * Utility method that creates an Object of class <code>clazz</code>
257:             * using the single String arg constructor.
258:             *
259:             * @param value object as a string
260:             * @param clazz class of the object
261:             * @return the object
262:             */
263:            public static Object makeObject(String value, Class clazz) {
264:                Object result;
265:                try {
266:                    if (value.startsWith("new ")) {
267:                        int bracket = value.indexOf("(");
268:                        String name = value.substring("new ".length(), bracket);
269:                        clazz = Class.forName(name, true, Thread
270:                                .currentThread().getContextClassLoader());
271:                        result = clazz.newInstance();
272:                    } else {
273:                        if (clazz.isPrimitive())
274:                            clazz = (Class) WRAPPERS.get(clazz);
275:                        Constructor constructor = clazz
276:                                .getConstructor(new Class[] { String.class });
277:                        result = constructor
278:                                .newInstance(new Object[] { value });
279:                    }
280:                } catch (Exception e) {
281:                    throw new RuntimeException(e);
282:                }
283:                return result;
284:            }
285:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.