001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.visualweb.propertyeditors.resolver;
043:
044: import org.netbeans.modules.visualweb.propertyeditors.*;
045: import com.sun.rave.propertyeditors.resolver.PropertyEditorResolver;
046: import org.netbeans.modules.visualweb.propertyeditors.binding.ValueBindingPropertyEditor;
047: import java.beans.PropertyDescriptor;
048: import java.beans.PropertyEditor;
049: import java.lang.reflect.Modifier;
050: import java.util.ResourceBundle;
051: import javax.el.MethodExpression;
052: import javax.el.ValueExpression;
053: import javax.faces.convert.Converter;
054: import javax.faces.el.MethodBinding;
055: import javax.faces.el.ValueBinding;
056: import javax.faces.validator.Validator;
057:
058: /**
059: * A utility class for matching property editors defined by this module with
060: * information specified in a property descriptor. This implementation is made
061: * available via a service provider look-up, specified in the {@code META-INF/services}
062: * directory.
063: *
064: * @author gjmurphy
065: */
066: public final class PropertyEditorResolverImpl implements
067: PropertyEditorResolver {
068:
069: private static final ResourceBundle bundle = ResourceBundle
070: .getBundle(PropertyEditorResolverImpl.class.getName());
071:
072: public PropertyEditor getEditor(
073: PropertyDescriptor propertyDescriptor) {
074:
075: if (propertyDescriptor == null) {
076: return null;
077: }
078: PropertyEditor editor = null;
079: Class propertyEditorClass = propertyDescriptor
080: .getPropertyEditorClass();
081:
082: if (propertyEditorClass == null) {
083: // If property descriptor does not specify a property editor, choose
084: // an appropriate default based on the property type.
085: Class propertyType = propertyDescriptor.getPropertyType();
086: if (propertyType.isPrimitive()) {
087: if (propertyType == Short.TYPE) {
088: editor = new IntegerPropertyEditor();
089: } else if (propertyType == Integer.TYPE) {
090: editor = new IntegerPropertyEditor();
091: } else if (propertyType == Long.TYPE) {
092: editor = new LongPropertyEditor();
093: } else if (propertyType == Float.TYPE) {
094: editor = new DoublePropertyEditor();
095: } else if (propertyType == Double.TYPE) {
096: editor = new DoublePropertyEditor();
097: }
098: }
099: if (String.class.isAssignableFrom(propertyType)) {
100: editor = new StringPropertyEditor();
101: } else if (Integer.class.isAssignableFrom(propertyType)) {
102: editor = new IntegerPropertyEditor();
103: } else if (Long.class.isAssignableFrom(propertyType)) {
104: editor = new LongPropertyEditor();
105: } else if (Double.class.isAssignableFrom(propertyType)) {
106: editor = new DoublePropertyEditor();
107: } else if (Converter.class.isAssignableFrom(propertyType)) {
108: editor = new ConverterPropertyEditor();
109: } else if (Validator.class.isAssignableFrom(propertyType)) {
110: editor = new ValidatorPropertyEditor();
111: } else if (ValueBinding.class
112: .isAssignableFrom(propertyType)
113: || ValueExpression.class
114: .isAssignableFrom(propertyType)) {
115: editor = new ValueBindingPropertyEditor();
116: } else if (MethodBinding.class
117: .isAssignableFrom(propertyType)
118: || MethodExpression.class
119: .isAssignableFrom(propertyType)) {
120: editor = new MethodBindingPropertyEditor();
121: }
122: } else {
123: if (isExportedPropertyEditorClass(propertyEditorClass)) {
124: try {
125: String propertyEditorClassName = propertyEditorClass
126: .getName();
127: String implClassName = bundle
128: .getString(propertyEditorClassName);
129: if (implClassName != null) {
130: ClassLoader classLoader = PropertyEditorResolverImpl.class
131: .getClassLoader();
132: Class implClass = classLoader
133: .loadClass(implClassName);
134: editor = (PropertyEditor) implClass
135: .newInstance();
136: }
137: } catch (Exception e) {
138: e.printStackTrace();
139: }
140: } else if (PropertyEditorBase.class
141: .isAssignableFrom(propertyEditorClass)) {
142: try {
143: editor = (PropertyEditor) propertyEditorClass
144: .newInstance();
145: } catch (Exception e) {
146: e.printStackTrace();
147: }
148: }
149: }
150:
151: return editor;
152: }
153:
154: private static final String[] exportedPropertyEditorPackageNames = new String[] {
155: "com.sun.rave.propertyeditors",
156: "com.sun.rave.propertyeditors.css",
157: "com.sun.rave.propertyeditors.binding",
158: "com.sun.jsfcl.std.css" };
159:
160: private static boolean isExportedPropertyEditorClass(
161: Class propertyEditorClass) {
162: for (String name : exportedPropertyEditorPackageNames) {
163: if (name.equals(propertyEditorClass.getPackage().getName())) {
164: return true;
165: }
166: }
167: return false;
168: }
169: }
|