Source Code Cross Referenced for ComponentAdapter.java in  » XML-UI » XUI » net » xoetrope » optional » registry » 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 » XML UI » XUI » net.xoetrope.optional.registry 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package net.xoetrope.optional.registry;
002:
003:        import java.lang.reflect.InvocationTargetException;
004:        import java.lang.reflect.Method;
005:        import java.util.Hashtable;
006:
007:        import java.awt.Component;
008:
009:        /**
010:         * An adapter of component properties and settings
011:         * <p> Copyright (c) Xoetrope Ltd., 2002-2004</p>
012:         * <p> $Revision: 1.7 $</p>
013:         * <p> License: see License.txt</p>
014:         */
015:        public class ComponentAdapter {
016:            protected Class clazz;
017:            protected String UI;
018:            protected String className;
019:            protected Hashtable setters;
020:            protected Hashtable getters;
021:
022:            /**
023:             * Construct a new adapter for the specified class.
024:             * @param clazzName the class name e.g. com.myorg.MyClass
025:             * @param ui the type of user interface this component supports, e.g. "swing"
026:             * @throws ClassNotFoundException
027:             */
028:            public ComponentAdapter(String clazzName, String ui)
029:                    throws ClassNotFoundException {
030:                UI = ui;
031:                className = clazzName;
032:                setters = new Hashtable();
033:                clazz = Class.forName(className);
034:            }
035:
036:            /**
037:             * Construct a new adapter for the specified class.
038:             * @param cl the class loader to use to load the class
039:             * @param clazzName the class name e.g. com.myorg.MyClass
040:             * @param ui the type of user interface this component supports, e.g. "swing"
041:             * @throws ClassNotFoundException
042:             */
043:            public ComponentAdapter(ClassLoader cl, String clazzName, String ui)
044:                    throws ClassNotFoundException {
045:                UI = ui;
046:                className = clazzName;
047:                setters = new Hashtable();
048:                clazz = cl.loadClass(className);
049:            }
050:
051:            /**
052:             * Set a property value. If the method has not been registered an attempt
053:             * will be made to get the method using the bean naming convention.
054:             * @param comp the comp whose property is set
055:             * @param propertyName the name of the property
056:             * @param paramType  the type of parameter (String, int, double, boolean, Object) or null for no parameter
057:             * @param content the value to set
058:             */
059:            public void setProperty(Component comp, String propertyName,
060:                    String content, String paramType) {
061:                /**
062:                 * @todo add a method of evaluating attributes so that helper classes can be
063:                 * used to set the properties, e.g. if some interpretation or type conversion
064:                 * of the attribute values is needed
065:                 */
066:                try {
067:                    if (setters != null) {
068:                        Method m = (Method) setters.get(propertyName);
069:                        if (m == null) {
070:                            String methodName = propertyName.substring(0, 1)
071:                                    .toUpperCase()
072:                                    + propertyName.substring(1);
073:                            m = addProperty("set", propertyName, "set"
074:                                    + propertyName, paramType, false);
075:                        }
076:
077:                        if (m != null) {
078:                            Object[] args = new Object[1];
079:                            args[0] = content;
080:
081:                            m.invoke(comp, args);
082:                        }
083:                    }
084:                } catch (InvocationTargetException ex) {
085:                    ex.printStackTrace();
086:                } catch (IllegalArgumentException ex) {
087:                    ex.printStackTrace();
088:                } catch (IllegalAccessException ex) {
089:                    ex.printStackTrace();
090:                }
091:            }
092:
093:            /**
094:             * Get a property value. If the method has not been registered an attempt
095:             * will be made to get the method using the bean naming convention.
096:             * @param comp the comp whose property is to be retrieved
097:             * @param propertyName the name of the property
098:             */
099:            public Object getProperty(Component comp, String propertyName) {
100:                try {
101:                    if (setters != null) {
102:                        Method m = (Method) setters.get(propertyName);
103:                        if (m == null) {
104:                            String methodName = propertyName.substring(0, 1)
105:                                    .toUpperCase()
106:                                    + propertyName.substring(1);
107:                            m = addProperty("get", propertyName, "get"
108:                                    + propertyName, null, false);
109:                        }
110:
111:                        if (m != null) {
112:                            Object[] args = new Object[0];
113:                            return m.invoke(comp, args);
114:                        }
115:                    }
116:                } catch (InvocationTargetException ex) {
117:                    ex.printStackTrace();
118:                } catch (IllegalArgumentException ex) {
119:                    ex.printStackTrace();
120:                } catch (IllegalAccessException ex) {
121:                    ex.printStackTrace();
122:                }
123:                return null;
124:            }
125:
126:            /**
127:             * Try to get and register a method
128:             * @param type either "get" or "set"
129:             * @param name the name by which the method is referred to in the page XML
130:             * @param methodName the method name
131:             * @param paramType  the type of parameter (String, int, double, boolean, Object)
132:             * @param attributed is the property set via the XAttributedComponent interface
133:             * @return the method or null if not found
134:             */
135:            public Method addProperty(String type, String name,
136:                    String methodName, String paramType, boolean attributed) {
137:                try {
138:                    boolean isSetter = (type.compareTo("set") == 0);
139:                    boolean isGetter = (type.compareTo("get") == 0);
140:                    if (type.compareTo("both") == 0) {
141:                        isSetter = true;
142:                        isGetter = true;
143:                    }
144:
145:                    Class[] params = new Class[isSetter ? 1 : 0];
146:                    if (isSetter && !attributed) {
147:                        if ((paramType == null) || (paramType.length() == 0)
148:                                || (paramType.equals("String")))
149:                            params[0] = String.class;
150:                        else if (paramType.equals("int"))
151:                            params[0] = int.class;
152:                        else if (paramType.equals("double"))
153:                            params[0] = double.class;
154:                        else if (paramType.equals("boolean"))
155:                            params[0] = boolean.class;
156:                        else if (paramType.equals("Object"))
157:                            params[0] = Object.class;
158:
159:                        try {
160:                            Method m = clazz.getMethod(methodName, params);
161:                            setters.put(name, m);
162:                        } catch (Exception ex) {
163:                            System.err.println("Couldn't find method '"
164:                                    + methodName + "'");
165:                        }
166:                    }
167:
168:                    if (isGetter) {
169:                        Method m = clazz.getMethod(methodName, params);
170:                        getters.put(name, m);
171:                    }
172:                } catch (NoSuchMethodException ex) {
173:                    ex.printStackTrace();
174:                } catch (SecurityException ex) {
175:                    ex.printStackTrace();
176:                }
177:                return null;
178:            }
179:
180:            /**
181:             * Get the UI type for this component e.g. "swing" or "awt"
182:             * @return the UI name
183:             */
184:            public String getUI() {
185:                return UI;
186:            }
187:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.