KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > beaninfo > editors > ClassEditor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.beaninfo.editors;
21
22 import java.text.MessageFormat JavaDoc;
23 import org.netbeans.core.UIExceptions;
24
25 import org.openide.util.Lookup;
26 import org.openide.util.NbBundle;
27
28 /** A property editor for Class.
29 * @author Jan Jancura
30 */

31 public class ClassEditor extends java.beans.PropertyEditorSupport JavaDoc {
32
33     /**
34      * This method is intended for use when generating Java code to set
35      * the value of the property. It should return a fragment of Java code
36      * that can be used to initialize a variable with the current property
37      * value.
38      * <p>
39      * Example results are "2", "new Color(127,127,34)", "Color.orange", etc.
40      *
41      * @return A fragment of Java code representing an initializer for the
42      * current value.
43      */

44     public String JavaDoc getJavaInitializationString() {
45         Class JavaDoc clazz = (Class JavaDoc)getValue();
46         if (clazz == null) return "null"; // NOI18N
47
return "Class.forName (\"" + clazz.getName () + "\")"; // NOI18N
48
}
49
50     //----------------------------------------------------------------------
51

52     /**
53     * @return The property value as a human editable string.
54     * <p> Returns null if the value can't be expressed as an editable string.
55     * <p> If a non-null value is returned, then the PropertyEditor should
56     * be prepared to parse that string back in setAsText().
57     */

58     public String JavaDoc getAsText() {
59         Class JavaDoc clazz = (Class JavaDoc)getValue();
60         if (clazz == null) return "null"; // NOI18N
61
return clazz.getName ();
62     }
63
64     /** Set the property value by parsing a given String. May raise
65     * java.lang.IllegalArgumentException if either the String is
66     * badly formatted or if this kind of property can't be expressed
67     * as text.
68     * @param text The string to be parsed.
69     */

70     public void setAsText(String JavaDoc text) throws java.lang.IllegalArgumentException JavaDoc {
71         try {
72             ClassLoader JavaDoc loader = Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
73             setValue (loader.loadClass (text));
74         } catch (ClassNotFoundException JavaDoc e) {
75             IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc (e.getMessage());
76             String JavaDoc msg = MessageFormat.format(
77                 NbBundle.getMessage(
78                     ClassEditor.class, "FMT_EXC_CANT_LOAD_CLASS"), new Object JavaDoc[] {text}); //NOI18N
79
UIExceptions.annotateUser(iae, e.getMessage(), msg, e,
80                                      new java.util.Date JavaDoc());
81             throw iae;
82         }
83     }
84 }
85
Popular Tags