KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > propertysheet > editors > TypeEditor


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.openide.explorer.propertysheet.editors;
21
22 import java.awt.*;
23 import java.beans.*;
24 import java.util.Locale JavaDoc;
25
26 import org.openide.ErrorManager;
27 import org.openide.src.Type;
28 import org.openide.explorer.propertysheet.editors.EnhancedPropertyEditor;
29 import org.openide.explorer.propertysheet.ExPropertyEditor;
30 import org.openide.explorer.propertysheet.PropertyEnv;
31 import org.openide.util.NbBundle;
32
33
34 /** Property editor for the org.netbeans.src.Type
35 *
36 * @author Petr Hamernik
37 */

38 public class TypeEditor extends PropertyEditorSupport
39     implements EnhancedPropertyEditor, ExPropertyEditor {
40
41     /**
42      * Value tags for L-values. It does not list "void" type.
43      */

44     private static final String JavaDoc[] LVALUE_TYPES = {
45         "boolean", "int", "char", "byte", "short", "long", "float", "double", // NOI18N
46
"Object", "String" // NOI18N
47
};
48
49     /**
50      * Value tags for R-values.
51      */

52     private static final String JavaDoc[] RVALUE_TYPES = {
53         "void", "boolean", "int", "char", "byte", "short", "long", "float", "double", // NOI18N
54
"Object", "String" // NOI18N
55
};
56     
57     /** generated Serialized Version UID */
58     static final long serialVersionUID = 1423443523462351952L;
59
60     private boolean acceptsVoid;
61     
62     /** Creates new editor */
63     public TypeEditor () {
64     }
65
66     /**
67     * @return The property value as a human editable string.
68     * <p> Returns null if the value can't be expressed as an editable string.
69     * <p> If a non-null value is returned, then the PropertyEditor should
70     * be prepared to parse that string back in setAsText().
71     */

72     public String JavaDoc getAsText () {
73         Object JavaDoc val = getValue();
74         return (val == null) ? "" : val.toString(); // NOI18N
75
}
76
77     /**
78     * Set the property value by parsing a given String.
79     * @param string The string to be parsed.
80     */

81     public void setAsText (String JavaDoc string) throws IllegalArgumentException JavaDoc {
82         Type t;
83         
84         try {
85             t = Type.parse(string);
86         } catch (IllegalArgumentException JavaDoc ex) {
87             ErrorManager.getDefault().annotate(ex,
88             ErrorManager.USER, null,
89                     getString("MSG_InvalidTypeDecl"), // NOI18N
90
null, null);
91             throw ex;
92         }
93         if (!acceptsVoid && t == Type.VOID) {
94             IllegalArgumentException JavaDoc ex = new IllegalArgumentException JavaDoc("Void not allowed"); // NOI18N
95
ErrorManager.getDefault().annotate(ex,
96             ErrorManager.USER, null,
97                     getString("MSG_VoidTypeNotPermitted"), // NOI18N
98
null, null);
99             throw ex;
100         }
101         setValue(t);
102     }
103
104     /**
105     * @param v new value
106     */

107     public void setValue(Object JavaDoc v) {
108         
109         if ( v == null || v instanceof Type )
110             super.setValue(v);
111         else
112             throw new IllegalArgumentException JavaDoc();
113     }
114
115     /**
116     * @return A fragment of Java code representing an initializer for the
117     * current value.
118     */

119     public String JavaDoc getJavaInitializationString () {
120         return getAsText();
121     }
122
123     /**
124     * @return The tag values for this property.
125     */

126     public String JavaDoc[] getTags () {
127         return acceptsVoid ? RVALUE_TYPES : LVALUE_TYPES;
128     }
129
130     /**
131     * @return Returns custom property editor to be showen inside the property
132     * sheet.
133     */

134     public Component getInPlaceCustomEditor () {
135         return null;
136     }
137
138     /**
139     * @return true if this PropertyEditor provides a enhanced in-place custom
140     * property editor, false otherwise
141     */

142     public boolean hasInPlaceCustomEditor () {
143         return false;
144     }
145
146     /**
147     * @return true if this property editor provides tagged values and
148     * a custom strings in the choice should be accepted too, false otherwise
149     */

150     public boolean supportsEditingTaggedValues () {
151         return true;
152     }
153     
154     /**
155      * This method is called by the IDE to pass
156      * the environment to the property editor.
157      */

158     public void attachEnv(PropertyEnv env) {
159         FeatureDescriptor desc = env.getFeatureDescriptor();
160         Object JavaDoc o;
161         
162         o = desc.getValue("acceptVoidType"); // NOI18N
163
if (o instanceof Boolean JavaDoc) {
164             acceptsVoid = ((Boolean JavaDoc)o).booleanValue();
165         } else {
166             acceptsVoid = true;
167         }
168     }
169     
170     private static String JavaDoc getString(String JavaDoc key) {
171         return NbBundle.getBundle("org.openide.explorer.propertysheet.editors.Bundle2", Locale.getDefault(), TypeEditor.class.getClassLoader()).getString(key);
172     }
173
174 }
175
Popular Tags