1 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 ; 31 import java.beans.PropertyEditorSupport ; 32 import java.util.StringTokenizer ; 33 34 35 39 public final class TypeEditor extends PropertyEditorSupport implements ExPropertyEditor { 40 41 44 public static final String [] LVALUE_TYPES = { 45 "boolean", "int", "char", "byte", "short", "long", "float", "double", "Object", "String" }; 48 49 52 public static final String [] RVALUE_TYPES = { 53 "void", "boolean", "int", "char", "byte", "short", "long", "float", "double", "Object", "String" }; 56 57 60 public static final String [] ANNTYPE_VALUE_TYPES = { 61 "boolean", "int", "char", "byte", "short", "long", "float", "double", "String", "String[]" }; 64 65 public static final String ANN_TYPE_EDITOR = "annTypeEditor"; 67 private boolean acceptsVoid; 68 private boolean isAnnTypeEditor = false; 69 70 private JavaModelPackage model; 71 72 73 public TypeEditor () { 74 } 75 76 public String getAsText () { 77 Object val = getValue(); 78 Type t = (Type) val; 79 return (t == null) ? "" : ElementFormat.elementName(t); } 81 82 86 public void setAsText (String text) throws IllegalArgumentException { 87 Type type = model.getType().resolve(text); 88 89 if (!isValidTypeSyntax(text)) { 90 throwUserWarning("MSG_InvalidTypeDecl"); } 92 if (isAnnTypeEditor) { 93 if (!isValidTypeOfAnnTypeMethod(type)) { 94 throwUserWarning("MSG_InvalidTypeDecl"); } 96 } else if (!acceptsVoid && type instanceof PrimitiveType && 97 PrimitiveTypeKindEnum.VOID.equals(((PrimitiveType) type).getKind())) { 98 throwUserWarning("MSG_VoidTypeNotPermitted"); } 100 setValue(type); 101 } 102 103 private static void throwUserWarning(String key) throws IllegalArgumentException { 104 IllegalArgumentException ex = new IllegalArgumentException (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 cname = t.getName(); 126 return cname.equals(String .class.getName()) || cname.equals(Class .class.getName()); 127 } 128 throw new IllegalStateException ("unknown type: " + type); } 130 131 136 public static boolean isValidTypeSyntax(String type) { 137 if (type.length() == 0 || type.charAt(0) == '<') { 138 return false; 139 } 140 141 StringTokenizer tukac = new StringTokenizer (type, "<,>", true); int depth = 0; 143 while (tukac.hasMoreTokens()) { 144 String token = tukac.nextToken(); 145 if (depth <= 0 && ",".equals(token)) return false; 147 else if ("<".equals(token)) depth++; 149 else if (">".equals(token)) { if (depth <= 0) return false; 151 depth--; 152 } 153 } 154 155 return depth == 0; 156 } 157 158 161 public void setValue(Object v) { 162 163 if ( v == null || v instanceof Type ) 164 super.setValue(v); 165 else 166 throw new IllegalArgumentException (); 167 } 168 169 173 public String getJavaInitializationString () { 174 return getAsText(); 175 } 176 177 180 public String [] getTags () { 181 if (isAnnTypeEditor) { 182 return ANNTYPE_VALUE_TYPES; 183 } 184 return acceptsVoid ? RVALUE_TYPES : LVALUE_TYPES; 185 } 186 187 191 public void attachEnv(PropertyEnv env) { 192 FeatureDescriptor desc = env.getFeatureDescriptor(); 193 Object o; 194 195 o = desc.getValue("acceptVoidType"); if (o instanceof Boolean ) { 197 acceptsVoid = ((Boolean )o).booleanValue(); 198 } else { 199 acceptsVoid = true; 200 } 201 202 o = desc.getValue(ANN_TYPE_EDITOR); 203 if (o instanceof Boolean ) { 204 isAnnTypeEditor = ((Boolean ) o).booleanValue(); 205 } 206 207 desc.setValue("canEditAsText", Boolean.TRUE); 210 model = ElementNode.getModel(desc); 211 } 212 213 private static String getString(String key) { 214 return NbBundle.getMessage(TypeEditor.class, key); 215 } 216 217 } 218 | Popular Tags |