View Javadoc
1 /* 2 * Scope: a generic MVC framework. 3 * Copyright (c) 2000-2002, The Scope team 4 * All rights reserved. 5 * 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * Neither the name "Scope" nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 * 35 * 36 * $Id: PropertyEditorFactory.java,v 1.6 2002/09/05 15:41:45 ludovicc Exp $ 37 */ 38 package org.scopemvc.view.util; 39 40 41 import java.util.HashMap; 42 import java.util.Iterator; 43 import org.apache.commons.logging.Log; 44 import org.apache.commons.logging.LogFactory; 45 import org.scopemvc.core.View; 46 import org.scopemvc.util.Debug; 47 import org.scopemvc.util.ScopeConfig; 48 49 /*** 50 * <P> 51 * 52 * Factory for Property Editors/Viewers of different Types (eg Swing, AWT etc) 53 * loaded from Scope Config. </P> <P> 54 * 55 * Format of config is this: <PRE> 56 * PropertyEditor.<viewtype>.<property class> 57 * 58 * =<editor class> PropertyViewer.<viewtype>.<property class> 59 * 60 * =<viewer class> </PRE> Defaults are provided in DefaultScopeConfig for java 61 * primitive types for the "Swing" viewtype. </P> 62 * 63 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</A> 64 * @created 05 September 2002 65 * @version $Revision: 1.6 $ $Date: 2002/09/05 15:41:45 $ 66 */ 67 public final class PropertyEditorFactory { 68 69 /*** 70 * Viewtype of viewers/editors to use in a Swing UI. 71 */ 72 public final static String SWING = "swing"; 73 74 private final static Log LOG = LogFactory.getLog(PropertyEditorFactory.class); 75 76 private final static String EDITOR_PROPERTY_PREFIX = "PropertyEditor."; 77 private final static String VIEWER_PROPERTY_PREFIX = "PropertyViewer."; 78 79 /*** 80 * Map of editor viewtype against HashMap of property class against editor 81 * class. 82 */ 83 private static HashMap propertyEditors; 84 85 /*** 86 * Map of viewer viewtype against HashMap of property class against viewer 87 * class. 88 */ 89 private static HashMap propertyViewers; 90 91 private static char DELIMITER = '-'; 92 // can't be '.' else parsing fails below 93 94 /*** 95 * Load the defaults from ScopeConfig. 96 */ 97 static { 98 propertyEditors = load(EDITOR_PROPERTY_PREFIX); 99 propertyViewers = load(VIEWER_PROPERTY_PREFIX); 100 } 101 102 103 private PropertyEditorFactory() { } 104 105 106 /*** 107 * Return a newly created editor for a property class for a certain 108 * viewtype. Viewtype is arbitrary and could be for instance Swing or XML or 109 * AWT: define the editors in ScopeConfig. 110 * 111 * @param inViewType find an editor of this type. 112 * @param inPropertyClass TODO: Describe the Parameter 113 * @return a newly created editor View or null if nothing appropriate can be 114 * found. 115 */ 116 public static View getPropertyEditor(String inViewType, Class inPropertyClass) { 117 if (LOG.isDebugEnabled()) { 118 LOG.debug("getPropertyEditor: " + inViewType + ", " + inPropertyClass); 119 } 120 try { 121 // Get editors of the right type 122 HashMap editorsForViewType = (HashMap) propertyEditors.get(inViewType); 123 if (editorsForViewType == null) { 124 return null; 125 } 126 if (LOG.isDebugEnabled()) { 127 LOG.debug("getPropertyEditor: editorsForViewType: " + editorsForViewType); 128 } 129 130 // Search for matching property class, climbing superclasses but not interfaces 131 Class propertyClass = inPropertyClass; 132 while (propertyClass != null) { 133 Class editorClass = (Class) editorsForViewType.get(propertyClass.getName()); 134 if (LOG.isDebugEnabled()) { 135 LOG.debug("getPropertyEditor: editorClass: " + editorClass); 136 } 137 if (editorClass != null) { 138 return (View) editorClass.newInstance(); 139 } 140 propertyClass = propertyClass.getSuperclass(); 141 } 142 } catch (Exception e) { 143 LOG.warn("getPropertyEditor: default: " + inPropertyClass + ", " + inViewType, e); 144 // ignore can't create instance of the editor 145 } 146 return null; 147 } 148 149 150 /*** 151 * Return a newly created viewer a property class for a certain viewtype. 152 * Viewtype is arbitrary and could be for instance Swing or XML or AWT: 153 * define the viewers in ScopeConfig. 154 * 155 * @param inViewType find an viewer of this type. 156 * @param inPropertyClass TODO: Describe the Parameter 157 * @return a newly created viewer View or null if nothing appropriate can be 158 * found. 159 */ 160 public static View getPropertyViewer(String inViewType, Class inPropertyClass) { 161 try { 162 // Get viewers of the right type 163 HashMap viewersForViewType = (HashMap) propertyViewers.get(inViewType); 164 if (viewersForViewType == null) { 165 return null; 166 } 167 168 // Search for matching property class, climbing superclasses but not interfaces 169 Class propertyClass = inPropertyClass; 170 while (propertyClass != null) { 171 Class viewerClass = (Class) viewersForViewType.get(propertyClass.getName()); 172 if (viewerClass != null) { 173 return (View) viewerClass.newInstance(); 174 } 175 propertyClass = propertyClass.getSuperclass(); 176 } 177 } catch (Exception e) { 178 LOG.warn("getPropertyViewer: default: " + inPropertyClass + ", " + inViewType, e); 179 // ignore can't create instance of the editor 180 } 181 return null; 182 } 183 184 185 private static HashMap load(String inPrefix) { 186 HashMap result = new HashMap(); 187 for (Iterator iter = ScopeConfig.getKeysMatching(inPrefix); iter.hasNext(); ) { 188 String key = (String) iter.next(); 189 if (key.lastIndexOf(DELIMITER) <= key.indexOf('.') + 1 || key.lastIndexOf(DELIMITER) >= key.length() - 2) { 190 LOG.error("Bad " + inPrefix + " configuration: no type or property class: " + key); 191 } else { 192 String type = key.substring(key.indexOf('.') + 1, key.lastIndexOf(DELIMITER)); 193 String propertyClass = key.substring(key.lastIndexOf(DELIMITER) + 1); 194 Class editorClass = ScopeConfig.getClass(key); 195 if (editorClass == null) { 196 LOG.error("Bad class: " + ScopeConfig.getString(key)); 197 } else { 198 if (result.get(type) == null) { 199 result.put(type, new HashMap()); 200 } 201 HashMap editors = (HashMap) result.get(type); 202 if (Debug.ON) { 203 Debug.assertTrue(editors != null, "null editors"); 204 } 205 if (LOG.isDebugEnabled()) { 206 LOG.debug("load: " + type + ", " + propertyClass + ", " + editorClass); 207 } 208 editors.put(propertyClass, editorClass); 209 } 210 } 211 } 212 return result; 213 } 214 } 215

This page was automatically generated by Maven