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: }
|