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 1997-2006 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.beaninfo.editors;
043:
044: import java.text.MessageFormat;
045: import org.netbeans.core.UIExceptions;
046:
047: import org.openide.util.Lookup;
048: import org.openide.util.NbBundle;
049:
050: /** A property editor for Class.
051: * @author Jan Jancura
052: */
053: public class ClassEditor extends java.beans.PropertyEditorSupport {
054:
055: /**
056: * This method is intended for use when generating Java code to set
057: * the value of the property. It should return a fragment of Java code
058: * that can be used to initialize a variable with the current property
059: * value.
060: * <p>
061: * Example results are "2", "new Color(127,127,34)", "Color.orange", etc.
062: *
063: * @return A fragment of Java code representing an initializer for the
064: * current value.
065: */
066: public String getJavaInitializationString() {
067: Class clazz = (Class) getValue();
068: if (clazz == null)
069: return "null"; // NOI18N
070: return "Class.forName (\"" + clazz.getName() + "\")"; // NOI18N
071: }
072:
073: //----------------------------------------------------------------------
074:
075: /**
076: * @return The property value as a human editable string.
077: * <p> Returns null if the value can't be expressed as an editable string.
078: * <p> If a non-null value is returned, then the PropertyEditor should
079: * be prepared to parse that string back in setAsText().
080: */
081: public String getAsText() {
082: Class clazz = (Class) getValue();
083: if (clazz == null)
084: return "null"; // NOI18N
085: return clazz.getName();
086: }
087:
088: /** Set the property value by parsing a given String. May raise
089: * java.lang.IllegalArgumentException if either the String is
090: * badly formatted or if this kind of property can't be expressed
091: * as text.
092: * @param text The string to be parsed.
093: */
094: public void setAsText(String text)
095: throws java.lang.IllegalArgumentException {
096: try {
097: ClassLoader loader = Lookup.getDefault().lookup(
098: ClassLoader.class);
099: setValue(loader.loadClass(text));
100: } catch (ClassNotFoundException e) {
101: IllegalArgumentException iae = new IllegalArgumentException(
102: e.getMessage());
103: String msg = MessageFormat.format(NbBundle.getMessage(
104: ClassEditor.class, "FMT_EXC_CANT_LOAD_CLASS"),
105: new Object[] { text }); //NOI18N
106: UIExceptions.annotateUser(iae, e.getMessage(), msg, e,
107: new java.util.Date());
108: throw iae;
109: }
110: }
111: }
|