Source Code Cross Referenced for PropertyEditorRegistry.java in  » Swing-Library » l2fprod-common » com » l2fprod » common » propertysheet » 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 » l2fprod common » com.l2fprod.common.propertysheet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * L2FProd.com Common Components 7.3 License.
003:         *
004:         * Copyright 2005-2007 L2FProd.com
005:         *
006:         * Licensed under the Apache License, Version 2.0 (the "License");
007:         * you may not use this file except in compliance with the License.
008:         * You may obtain a copy of the License at
009:         *
010:         *     http://www.apache.org/licenses/LICENSE-2.0
011:         *
012:         * Unless required by applicable law or agreed to in writing, software
013:         * distributed under the License is distributed on an "AS IS" BASIS,
014:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015:         * See the License for the specific language governing permissions and
016:         * limitations under the License.
017:         */package com.l2fprod.common.propertysheet;
018:
019:        import com.l2fprod.common.beans.editor.*;
020:
021:        import java.awt.Color;
022:        import java.awt.Dimension;
023:        import java.awt.Font;
024:        import java.awt.Insets;
025:        import java.awt.Rectangle;
026:        import java.beans.PropertyDescriptor;
027:        import java.beans.PropertyEditor;
028:        import java.beans.PropertyEditorManager;
029:        import java.io.File;
030:        import java.util.Date;
031:        import java.util.HashMap;
032:        import java.util.Map;
033:
034:        /**
035:         * Mapping between Properties, Property Types and Property Editors.
036:         */
037:        public class PropertyEditorRegistry implements  PropertyEditorFactory {
038:
039:            private Map typeToEditor;
040:            private Map propertyToEditor;
041:
042:            public PropertyEditorRegistry() {
043:                typeToEditor = new HashMap();
044:                propertyToEditor = new HashMap();
045:                registerDefaults();
046:            }
047:
048:            public PropertyEditor createPropertyEditor(Property property) {
049:                return getEditor(property);
050:            }
051:
052:            /**
053:             * Gets an editor for the given property. The lookup is as follow:
054:             * <ul>
055:             * <li>if propertyDescriptor.getPropertyEditorClass() returns a valid value, 
056:             * it is returned, else, 
057:             * <li>if an editor was registered with
058:             * {@link #registerEditor(Property, PropertyEditor)}, it is
059:             * returned, else</li>
060:             * <li>if an editor class was registered with
061:             * {@link #registerEditor(Property, Class)}, it is returned, else
062:             * <li>
063:             * <li>look for editor for the property type using
064:             * {@link #getEditor(Class)}.it is returned, else
065:             * </li>
066:             * <li>look for editor using PropertyEditorManager.findEditor(Class);
067:             * </li>
068:             * </ul>
069:             * 
070:             * @param property
071:             * @return an editor suitable for the Property.
072:             */
073:            public synchronized PropertyEditor getEditor(Property property) {
074:                PropertyEditor editor = null;
075:                if (property instanceof  PropertyDescriptorAdapter) {
076:                    PropertyDescriptor descriptor = ((PropertyDescriptorAdapter) property)
077:                            .getDescriptor();
078:                    if (descriptor != null) {
079:                        Class clz = descriptor.getPropertyEditorClass();
080:                        if (clz != null) {
081:                            editor = loadPropertyEditor(clz);
082:                        }
083:                    }
084:                }
085:                if (editor == null) {
086:                    Object value = propertyToEditor.get(property);
087:                    if (value instanceof  PropertyEditor) {
088:                        editor = (PropertyEditor) value;
089:                    } else if (value instanceof  Class) {
090:                        editor = loadPropertyEditor((Class) value);
091:                    } else {
092:                        editor = getEditor(property.getType());
093:                    }
094:                }
095:                if ((editor == null)
096:                        && (property instanceof  PropertyDescriptorAdapter)) {
097:                    PropertyDescriptor descriptor = ((PropertyDescriptorAdapter) property)
098:                            .getDescriptor();
099:                    Class clz = descriptor.getPropertyType();
100:                    editor = PropertyEditorManager.findEditor(clz);
101:                }
102:                return editor;
103:            }
104:
105:            /**
106:             * Load PropertyEditor from clz through reflection.
107:             * @param clz Class to load from.
108:             * @return Loaded propertyEditor
109:             */
110:            private PropertyEditor loadPropertyEditor(Class clz) {
111:                PropertyEditor editor = null;
112:                try {
113:                    editor = (PropertyEditor) clz.newInstance();
114:                } catch (Exception e) {
115:                    e.printStackTrace();
116:                }
117:                return editor;
118:            }
119:
120:            /**
121:             * Gets an editor for the given property type. The lookup is as
122:             * follow:
123:             * <ul>
124:             * <li>if an editor was registered with
125:             * {@link #registerEditor(Class, PropertyEditor)}, it is returned,
126:             * else</li>
127:             * <li>if an editor class was registered with
128:             * {@link #registerEditor(Class, Class)}, it is returned, else
129:             * <li>
130:             * <li>it returns null.</li>
131:             * </ul>
132:             * 
133:             * @param type
134:             * @return an editor suitable for the Property type or null if none
135:             *         found
136:             */
137:            public synchronized PropertyEditor getEditor(Class type) {
138:                PropertyEditor editor = null;
139:                Object value = typeToEditor.get(type);
140:                if (value instanceof  PropertyEditor) {
141:                    editor = (PropertyEditor) value;
142:                } else if (value instanceof  Class) {
143:                    try {
144:                        editor = (PropertyEditor) ((Class) value).newInstance();
145:                    } catch (Exception e) {
146:                        e.printStackTrace();
147:                    }
148:                }
149:                return editor;
150:            }
151:
152:            public synchronized void registerEditor(Class type,
153:                    Class editorClass) {
154:                typeToEditor.put(type, editorClass);
155:            }
156:
157:            public synchronized void registerEditor(Class type,
158:                    PropertyEditor editor) {
159:                typeToEditor.put(type, editor);
160:            }
161:
162:            public synchronized void unregisterEditor(Class type) {
163:                typeToEditor.remove(type);
164:            }
165:
166:            public synchronized void registerEditor(Property property,
167:                    Class editorClass) {
168:                propertyToEditor.put(property, editorClass);
169:            }
170:
171:            public synchronized void registerEditor(Property property,
172:                    PropertyEditor editor) {
173:                propertyToEditor.put(property, editor);
174:            }
175:
176:            public synchronized void unregisterEditor(Property property) {
177:                propertyToEditor.remove(property);
178:            }
179:
180:            /**
181:             * Adds default editors. This method is called by the constructor
182:             * but may be called later to reset any customizations made through
183:             * the <code>registerEditor</code> methods. <b>Note: if overriden,
184:             * <code>super.registerDefaults()</code> must be called before
185:             * plugging custom defaults. </b>
186:             */
187:            public void registerDefaults() {
188:                typeToEditor.clear();
189:                propertyToEditor.clear();
190:
191:                // our editors
192:                registerEditor(String.class, StringPropertyEditor.class);
193:
194:                registerEditor(double.class, DoublePropertyEditor.class);
195:                registerEditor(Double.class, DoublePropertyEditor.class);
196:
197:                registerEditor(float.class, FloatPropertyEditor.class);
198:                registerEditor(Float.class, FloatPropertyEditor.class);
199:
200:                registerEditor(int.class, IntegerPropertyEditor.class);
201:                registerEditor(Integer.class, IntegerPropertyEditor.class);
202:
203:                registerEditor(long.class, LongPropertyEditor.class);
204:                registerEditor(Long.class, LongPropertyEditor.class);
205:
206:                registerEditor(short.class, ShortPropertyEditor.class);
207:                registerEditor(Short.class, ShortPropertyEditor.class);
208:
209:                registerEditor(boolean.class,
210:                        BooleanAsCheckBoxPropertyEditor.class);
211:                registerEditor(Boolean.class,
212:                        BooleanAsCheckBoxPropertyEditor.class);
213:
214:                registerEditor(File.class, FilePropertyEditor.class);
215:
216:                // awt object editors
217:                registerEditor(Color.class, ColorPropertyEditor.class);
218:                registerEditor(Dimension.class, DimensionPropertyEditor.class);
219:                registerEditor(Insets.class, InsetsPropertyEditor.class);
220:                try {
221:                    Class fontEditor = Class
222:                            .forName("com.l2fprod.common.beans.editor.FontPropertyEditor");
223:                    registerEditor(Font.class, fontEditor);
224:                } catch (Exception e) {
225:                    // FontPropertyEditor might not be there when using the split jars
226:                }
227:                registerEditor(Rectangle.class, RectanglePropertyEditor.class);
228:
229:                //
230:                // Date Editors based on what we have in the classpath
231:                //
232:
233:                boolean foundDateEditor = false;
234:
235:                // if JCalendar jar is available, use it as the default date
236:                // editor
237:                try {
238:                    Class.forName("com.toedter.calendar.JDateChooser");
239:                    registerEditor(
240:                            Date.class,
241:                            Class
242:                                    .forName("com.l2fprod.common.beans.editor.JCalendarDatePropertyEditor"));
243:                    foundDateEditor = true;
244:                } catch (ClassNotFoundException e) {
245:                    // No JCalendar found
246:                }
247:
248:                if (!foundDateEditor) {
249:                    // try NachoCalendar
250:                    try {
251:                        Class
252:                                .forName("net.sf.nachocalendar.components.DateField");
253:                        registerEditor(
254:                                Date.class,
255:                                Class
256:                                        .forName("com.l2fprod.common.beans.editor.NachoCalendarDatePropertyEditor"));
257:                        foundDateEditor = true;
258:                    } catch (ClassNotFoundException e) {
259:                        // No NachoCalendar found
260:                    }
261:                }
262:            }
263:
264:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.