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.swing.renderer.BooleanCellRenderer;
020: import com.l2fprod.common.swing.renderer.ColorCellRenderer;
021: import com.l2fprod.common.swing.renderer.DateRenderer;
022: import com.l2fprod.common.swing.renderer.DefaultCellRenderer;
023: import com.l2fprod.common.beans.ExtendedPropertyDescriptor;
024:
025: import java.awt.Color;
026: import java.util.Date;
027: import java.util.HashMap;
028: import java.util.Map;
029: import java.beans.PropertyDescriptor;
030:
031: import javax.swing.table.TableCellRenderer;
032:
033: /**
034: * Mapping between Properties, Property Types and Renderers.
035: */
036: public class PropertyRendererRegistry implements
037: PropertyRendererFactory {
038:
039: private Map typeToRenderer;
040: private Map propertyToRenderer;
041:
042: public PropertyRendererRegistry() {
043: typeToRenderer = new HashMap();
044: propertyToRenderer = new HashMap();
045: registerDefaults();
046: }
047:
048: public TableCellRenderer createTableCellRenderer(Property property) {
049: return getRenderer(property);
050: }
051:
052: public TableCellRenderer createTableCellRenderer(Class type) {
053: return getRenderer(type);
054: }
055:
056: /**
057: * Gets a renderer for the given property. The lookup is as follow:
058: * <ul>
059: * <li>if a renderer was registered with
060: * {@link ExtendedPropertyDescriptor#setPropertyTableRendererClass(Class)} - BeanInfo, it is
061: * returned, else</li>
062: * <li>if a renderer was registered with
063: * {@link #registerRenderer(Property, TableCellRenderer)}, it is
064: * returned, else</li>
065: * <li>if a renderer class was registered with
066: * {@link #registerRenderer(Property, Class)}, it is returned, else
067: * <li>
068: * <li>look for renderer for the property type using
069: * {@link #getRenderer(Class)}.</li>
070: * </ul>
071: *
072: * @param property
073: * @return a renderer suitable for the Property.
074: */
075: public synchronized TableCellRenderer getRenderer(Property property) {
076:
077: // editors bound to the property descriptor have the highest priority
078: TableCellRenderer renderer = null;
079: if (property instanceof PropertyDescriptorAdapter) {
080: PropertyDescriptor descriptor = ((PropertyDescriptorAdapter) property)
081: .getDescriptor();
082: if (descriptor instanceof ExtendedPropertyDescriptor) {
083: if (((ExtendedPropertyDescriptor) descriptor)
084: .getPropertyTableRendererClass() != null) {
085: try {
086: return (TableCellRenderer) (((ExtendedPropertyDescriptor) descriptor)
087: .getPropertyTableRendererClass())
088: .newInstance();
089: } catch (Exception ex) {
090: ex.printStackTrace();
091: }
092: }
093: }
094: }
095: Object value = propertyToRenderer.get(property);
096: if (value instanceof TableCellRenderer) {
097: renderer = (TableCellRenderer) value;
098: } else if (value instanceof Class) {
099: try {
100: renderer = (TableCellRenderer) ((Class) value)
101: .newInstance();
102: } catch (Exception e) {
103: e.printStackTrace();
104: }
105: } else {
106: renderer = getRenderer(property.getType());
107: }
108: return renderer;
109: }
110:
111: /**
112: * Gets a renderer for the given property type. The lookup is as
113: * follow:
114: * <ul>
115: * <li>if a renderer was registered with
116: * {@link #registerRenderer(Class, TableCellRenderer)}, it is returned,
117: * else</li>
118: * <li>if a renderer class was registered with
119: * {@link #registerRenderer(Class, Class)}, it is returned, else
120: * <li>
121: * <li>it returns null.</li>
122: * </ul>
123: *
124: * @param type
125: * @return a renderer editor suitable for the Property type or null if none
126: * found
127: */
128: public synchronized TableCellRenderer getRenderer(Class type) {
129: TableCellRenderer renderer = null;
130: Object value = typeToRenderer.get(type);
131: if (value instanceof TableCellRenderer) {
132: renderer = (TableCellRenderer) value;
133: } else if (value instanceof Class) {
134: try {
135: renderer = (TableCellRenderer) ((Class) value)
136: .newInstance();
137: } catch (Exception e) {
138: e.printStackTrace();
139: }
140: }
141: return renderer;
142: }
143:
144: public synchronized void registerRenderer(Class type,
145: Class rendererClass) {
146: typeToRenderer.put(type, rendererClass);
147: }
148:
149: public synchronized void registerRenderer(Class type,
150: TableCellRenderer renderer) {
151: typeToRenderer.put(type, renderer);
152: }
153:
154: public synchronized void unregisterRenderer(Class type) {
155: typeToRenderer.remove(type);
156: }
157:
158: public synchronized void registerRenderer(Property property,
159: Class rendererClass) {
160: propertyToRenderer.put(property, rendererClass);
161: }
162:
163: public synchronized void registerRenderer(Property property,
164: TableCellRenderer renderer) {
165: propertyToRenderer.put(property, renderer);
166: }
167:
168: public synchronized void unregisterRenderer(Property property) {
169: propertyToRenderer.remove(property);
170: }
171:
172: /**
173: * Adds default renderers. This method is called by the constructor
174: * but may be called later to reset any customizations made through
175: * the <code>registerRenderer</code> methods. <b>Note: if overriden,
176: * <code>super.registerDefaults()</code> must be called before
177: * plugging custom defaults. </b>
178: */
179: public void registerDefaults() {
180: typeToRenderer.clear();
181: propertyToRenderer.clear();
182:
183: // use the default renderer for Object and all primitives
184: DefaultCellRenderer renderer = new DefaultCellRenderer();
185: renderer.setShowOddAndEvenRows(false);
186:
187: ColorCellRenderer colorRenderer = new ColorCellRenderer();
188: colorRenderer.setShowOddAndEvenRows(false);
189:
190: BooleanCellRenderer booleanRenderer = new BooleanCellRenderer();
191:
192: DateRenderer dateRenderer = new DateRenderer();
193: dateRenderer.setShowOddAndEvenRows(false);
194:
195: registerRenderer(Object.class, renderer);
196: registerRenderer(Color.class, colorRenderer);
197: registerRenderer(boolean.class, booleanRenderer);
198: registerRenderer(Boolean.class, booleanRenderer);
199: registerRenderer(byte.class, renderer);
200: registerRenderer(Byte.class, renderer);
201: registerRenderer(char.class, renderer);
202: registerRenderer(Character.class, renderer);
203: registerRenderer(double.class, renderer);
204: registerRenderer(Double.class, renderer);
205: registerRenderer(float.class, renderer);
206: registerRenderer(Float.class, renderer);
207: registerRenderer(int.class, renderer);
208: registerRenderer(Integer.class, renderer);
209: registerRenderer(long.class, renderer);
210: registerRenderer(Long.class, renderer);
211: registerRenderer(short.class, renderer);
212: registerRenderer(Short.class, renderer);
213: registerRenderer(Date.class, dateRenderer);
214: }
215:
216: }
|