KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > ui > nodes > 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.netbeans.modules.java.ui.nodes.editors;
21
22 import org.openide.ErrorManager;
23 import org.openide.explorer.propertysheet.ExPropertyEditor;
24 import org.openide.explorer.propertysheet.PropertyEnv;
25 import org.openide.util.NbBundle;
26 import org.netbeans.jmi.javamodel.*;
27 import org.netbeans.modules.java.ui.nodes.elements.ElementNode;
28 import org.netbeans.modules.java.ui.nodes.elements.ElementFormat;
29
30 import java.beans.FeatureDescriptor JavaDoc;
31 import java.beans.PropertyEditorSupport JavaDoc;
32 import java.util.StringTokenizer JavaDoc;
33
34
35 /** Property editor for the org.netbeans.jmi.javamodel.Type
36 *
37 * @author Petr Hamernik, Jan Pokorsky
38 */

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

44     public 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     public static final String JavaDoc[] RVALUE_TYPES = {
53         "void", "boolean", "int", "char", "byte", "short", "long", "float", "double", // NOI18N
54
"Object", "String" // NOI18N
55
};
56
57     /**
58      * Value tags for L-values.
59      */

60     public static final String JavaDoc[] ANNTYPE_VALUE_TYPES = {
61         "boolean", "int", "char", "byte", "short", "long", "float", "double", // NOI18N
62
"String", "String[]" // NOI18N
63
};
64     
65     public static final String JavaDoc ANN_TYPE_EDITOR = "annTypeEditor"; // NOI18N
66

67     private boolean acceptsVoid;
68     private boolean isAnnTypeEditor = false;
69     
70     private JavaModelPackage model;
71     
72     /** Creates new editor */
73     public TypeEditor () {
74     }
75
76     public String JavaDoc getAsText () {
77         Object JavaDoc val = getValue();
78         Type t = (Type) val;
79         return (t == null) ? "" : ElementFormat.elementName(t); // NOI18N
80
}
81
82     /**
83     * Set the property value by parsing a given String.
84     * @param text The string to be parsed.
85     */

86     public void setAsText (String JavaDoc text) throws IllegalArgumentException JavaDoc {
87         Type type = model.getType().resolve(text);
88
89         if (!isValidTypeSyntax(text)) {
90             throwUserWarning("MSG_InvalidTypeDecl"); // NOI18N
91
}
92         if (isAnnTypeEditor) {
93             if (!isValidTypeOfAnnTypeMethod(type)) {
94                 throwUserWarning("MSG_InvalidTypeDecl"); // NOI18N
95
}
96         } else if (!acceptsVoid && type instanceof PrimitiveType &&
97                 PrimitiveTypeKindEnum.VOID.equals(((PrimitiveType) type).getKind())) {
98             throwUserWarning("MSG_VoidTypeNotPermitted"); // NOI18N
99
}
100         setValue(type);
101     }
102     
103     private static void throwUserWarning(String JavaDoc key) throws IllegalArgumentException JavaDoc {
104         IllegalArgumentException JavaDoc ex = new IllegalArgumentException JavaDoc(key);
105         ErrorManager.getDefault().annotate(ex,
106                 ErrorManager.USER, null, getString(key), null, null);
107         throw ex;
108     }
109     
110     public static boolean isValidTypeOfAnnTypeMethod(Type type) {
111         Type t = type;
112         while (t instanceof Array) {
113             t = ((Array) t).getType();
114         }
115         
116         if (t instanceof PrimitiveType) {
117             return !PrimitiveTypeKindEnum.VOID.equals(((PrimitiveType) t).getKind());
118         }
119         
120         if (t instanceof JavaEnum || t instanceof AnnotationType || t instanceof UnresolvedClass) {
121             return true;
122         }
123         
124         if (t instanceof JavaClass) {
125             String JavaDoc cname = t.getName();
126             return cname.equals(String JavaDoc.class.getName()) || cname.equals(Class JavaDoc.class.getName());
127         }
128         throw new IllegalStateException JavaDoc("unknown type: " + type); // NOI18N
129
}
130     
131     /**
132      * validates generics syntax for now to not break the mdr
133      * @param type type to check
134      * @return is valid or not
135      */

136     public static boolean isValidTypeSyntax(String JavaDoc type) {
137         if (type.length() == 0 || type.charAt(0) == '<') {
138             return false;
139         }
140         
141         StringTokenizer JavaDoc tukac = new StringTokenizer JavaDoc(type, "<,>", true); // NOI18N
142
int depth = 0;
143         while (tukac.hasMoreTokens()) {
144             String JavaDoc token = tukac.nextToken();
145             if (depth <= 0 && ",".equals(token)) // NOI18N
146
return false;
147             else if ("<".equals(token)) // NOI18N
148
depth++;
149             else if (">".equals(token)) { // NOI18N
150
if (depth <= 0) return false;
151                 depth--;
152             }
153         }
154         
155         return depth == 0;
156     }
157
158     /**
159     * @param v new value
160     */

161     public void setValue(Object JavaDoc v) {
162         
163         if ( v == null || v instanceof Type )
164             super.setValue(v);
165         else
166             throw new IllegalArgumentException JavaDoc();
167     }
168
169     /**
170     * @return A fragment of Java code representing an initializer for the
171     * current value.
172     */

173     public String JavaDoc getJavaInitializationString () {
174         return getAsText();
175     }
176
177     /**
178     * @return The tag values for this property.
179     */

180     public String JavaDoc[] getTags () {
181         if (isAnnTypeEditor) {
182             return ANNTYPE_VALUE_TYPES;
183         }
184         return acceptsVoid ? RVALUE_TYPES : LVALUE_TYPES;
185     }
186
187     /**
188      * This method is called by the IDE to pass
189      * the environment to the property editor.
190      */

191     public void attachEnv(PropertyEnv env) {
192         FeatureDescriptor JavaDoc desc = env.getFeatureDescriptor();
193         Object JavaDoc o;
194         
195         o = desc.getValue("acceptVoidType"); // NOI18N
196
if (o instanceof Boolean JavaDoc) {
197             acceptsVoid = ((Boolean JavaDoc)o).booleanValue();
198         } else {
199             acceptsVoid = true;
200         }
201         
202         o = desc.getValue(ANN_TYPE_EDITOR);
203         if (o instanceof Boolean JavaDoc) {
204             isAnnTypeEditor = ((Boolean JavaDoc) o).booleanValue();
205         }
206         
207         // inplace editor will permit to type own Types
208
desc.setValue("canEditAsText", Boolean.TRUE); // NOI18N
209

210         model = ElementNode.getModel(desc);
211     }
212     
213     private static String JavaDoc getString(String JavaDoc key) {
214         return NbBundle.getMessage(TypeEditor.class, key);
215     }
216
217 }
218
Popular Tags