1 19 20 package org.netbeans.modules.java.ui.nodes.elements; 21 22 import org.openide.src.ElementProperties; 23 import org.openide.nodes.Node; 24 import org.openide.nodes.AbstractNode; 25 import org.openide.nodes.Children; 26 import org.openide.util.datatransfer.NewType; 27 import org.openide.util.actions.SystemAction; 28 import org.openide.util.NbBundle; 29 import org.openide.actions.NewAction; 30 import org.openide.actions.ToolsAction; 31 import org.openide.actions.PropertiesAction; 32 import org.netbeans.jmi.javamodel.JavaClass; 33 import org.netbeans.jmi.javamodel.JavaEnum; 34 import org.netbeans.jmi.javamodel.ClassDefinition; 35 import org.netbeans.jmi.javamodel.AnnotationType; 36 import org.netbeans.modules.java.ui.nodes.SourceNodeFactory; 37 38 import java.util.Collection ; 39 import java.util.ArrayList ; 40 import java.util.List ; 41 42 48 public final class Categories { 49 52 private static final Object CATEGORY_FIELDS = new Object (); 53 56 private static final Object CATEGORY_METHODS = new Object (); 57 60 private static final Object CATEGORY_CONSTRUCTORS = new Object (); 61 64 private static final Object CATEGORY_CONSTANTS = new Object (); 65 68 private static final Object CATEGORY_ANN_TYPE_METHODS = new Object (); 69 70 private static final List CLASS_CATEGORIES; 71 private static final List INTERFACE_CATEGORIES; 72 private static final List ENUM_CATEGORIES; 73 private static final List ANNTYPES_CATEGORIES; 74 public static final int FILTER_CATEGORIES = 0x1000; 75 76 private static final SystemAction[] CATEGORY_ACTIONS = new SystemAction[] { 77 80 SystemAction.get(NewAction.class), 84 null, 85 SystemAction.get(ToolsAction.class), 86 SystemAction.get(PropertiesAction.class) 87 }; 88 89 static final int[][] FILTERS = new int[][] { 90 { ClassElementFilter.FIELD }, 91 { ClassElementFilter.CONSTRUCTOR }, 92 { ClassElementFilter.METHOD }, 93 { EnumFilter.CONSTANTS }, 94 { AnnotationTypeFilter.MEMBER }, 95 }; 96 97 static final String [] NAMES = new String [] { 98 getString("Fields"), getString("Constructors"), getString("Methods"), getString("Constants"), getString("AnnTypeMethods") }; 104 105 static final String [] SHORTDESCRS = new String [] { 106 getString("Fields_HINT"), getString("Constructors_HINT"), getString("Methods_HINT"), getString("Constants_HINT"), getString("AnnTypeMethods_HINT") }; 112 113 static final String [] CATEGORY_ICONS = new String [] { 114 IconStrings.FIELDS_CATEGORY, 115 IconStrings.CONSTRUCTORS_CATEGORY, 116 IconStrings.METHODS_CATEGORY, 117 IconStrings.CONSTANTS_CATEGORY, 118 IconStrings.ANN_TYPE_METHODS_CATEGORY, 119 }; 120 121 127 public static final Children createClassChildren(ClassDefinition element, SourceNodeFactory factory, boolean writable) { 128 if (ElementNode.getSourceOptions().getCategoriesUsage()) { 129 ClassChildren children = new Categories.CategorizingChildren(factory, element, writable); 130 ClassElementFilter filter = new ClassElementFilter(); 131 filter.setOrder(new int[] { 132 SourceElementFilter.CLASS, 133 SourceElementFilter.INTERFACE, 134 SourceElementFilter.ENUM, 135 SourceElementFilter.ANNOTATION_TYPE, 136 Categories.FILTER_CATEGORIES 137 }); 138 children.setFilter(filter); 139 return children; 140 } else { 141 return new ClassChildren(factory, element); 142 } 143 } 144 145 150 public static final Children createEnumChildren(JavaEnum element, SourceNodeFactory factory, boolean writable) { 151 if (ElementNode.getSourceOptions().getCategoriesUsage()) { 152 ClassChildren children = new Categories.CategorizingChildren(factory, element, writable); 153 ClassElementFilter filter = new EnumFilter(); 154 filter.setOrder(new int[] { 155 SourceElementFilter.CLASS, 156 SourceElementFilter.INTERFACE, 157 SourceElementFilter.ENUM, 158 SourceElementFilter.ANNOTATION_TYPE, 159 Categories.FILTER_CATEGORIES 160 }); 161 children.setFilter(filter); 162 return children; 163 } else { 164 return new EnumChildren(factory, element); 165 } 166 } 167 168 173 public static final Children createAnnotationTypeChildren(AnnotationType element, SourceNodeFactory factory, boolean writable) { 174 if (ElementNode.getSourceOptions().getCategoriesUsage()) { 175 ClassChildren children = new Categories.CategorizingChildren(factory, element, writable); 176 ClassElementFilter filter = new AnnotationTypeFilter(); 177 filter.setOrder(new int[] { 178 SourceElementFilter.CLASS, 179 SourceElementFilter.INTERFACE, 180 SourceElementFilter.ENUM, 181 SourceElementFilter.ANNOTATION_TYPE, 182 Categories.FILTER_CATEGORIES 183 }); 184 children.setFilter(filter); 185 return children; 186 } else { 187 return new AnnotationTypeChildren(factory, element); 188 } 189 } 190 191 static { 192 CLASS_CATEGORIES = new ArrayList (4); 193 CLASS_CATEGORIES.add(CATEGORY_FIELDS); 194 CLASS_CATEGORIES.add(CATEGORY_CONSTRUCTORS); 195 CLASS_CATEGORIES.add(CATEGORY_METHODS); 196 197 INTERFACE_CATEGORIES = new ArrayList (2); 198 INTERFACE_CATEGORIES.add(CATEGORY_FIELDS); 199 INTERFACE_CATEGORIES.add(CATEGORY_METHODS); 200 201 ENUM_CATEGORIES = new ArrayList (4); 202 ENUM_CATEGORIES.add(CATEGORY_CONSTANTS); 203 ENUM_CATEGORIES.add(CATEGORY_FIELDS); 204 ENUM_CATEGORIES.add(CATEGORY_CONSTRUCTORS); 205 ENUM_CATEGORIES.add(CATEGORY_METHODS); 206 207 ANNTYPES_CATEGORIES = new ArrayList (2); 208 ANNTYPES_CATEGORIES.add(CATEGORY_FIELDS); 209 ANNTYPES_CATEGORIES.add(CATEGORY_ANN_TYPE_METHODS); 210 } 211 212 217 static final class CategorizingChildren extends ClassChildren { 218 boolean writeable; 219 220 static { 221 ClassChildren.propToFilter.put(ElementProperties.PROP_CLASS_OR_INTERFACE, 222 new Integer (FILTER_CATEGORIES)); 223 } 224 225 CategorizingChildren(SourceNodeFactory factory, ClassDefinition data, boolean wr) { 226 super(factory, data); 227 writeable = wr; 228 } 229 230 protected Node[] createNodesImpl(Object key) { 231 if (key == CATEGORY_FIELDS) { 232 return new Node[] { 233 new ElementCategoryNode(0, getFactory(), element, writeable) 234 }; 235 } else if (key == CATEGORY_METHODS) { 236 return new Node[] { 237 new ElementCategoryNode(2, getFactory(), element, writeable) 238 }; 239 } else if (key == CATEGORY_CONSTRUCTORS) { 240 return new Node[] { 241 new ElementCategoryNode(1, getFactory(), element, writeable) 242 }; 243 } else if (key == CATEGORY_CONSTANTS) { 244 return new Node[] { 245 new ElementCategoryNode(3, getFactory(), element, writeable) 246 }; 247 } else if (key == CATEGORY_ANN_TYPE_METHODS) { 248 return new Node[] { 249 new ElementCategoryNode(4, getFactory(), element, writeable) 250 }; 251 } 252 return super.createNodesImpl(key); 253 } 254 255 protected List getKeysOfType(Collection elements, int type) { 256 if (type != FILTER_CATEGORIES) 257 return super.getKeysOfType(elements, type); 258 if (element instanceof JavaEnum) { 259 return ENUM_CATEGORIES; 260 } else if (element instanceof AnnotationType) { 261 return ANNTYPES_CATEGORIES; 262 } else if (element instanceof JavaClass && ((JavaClass) element).isInterface()) { 263 return INTERFACE_CATEGORIES; 264 } else { 265 return CLASS_CATEGORIES; 266 } 267 } 268 } 269 270 274 static final class ElementCategoryNode extends AbstractNode { 275 276 277 ClassDefinition element; 278 279 280 int newTypeIndex; 281 282 private static ClassChildren createChildren(SourceNodeFactory f, ClassDefinition jc) { 283 ClassChildren cc; 284 if (jc instanceof AnnotationType) { 285 cc = new AnnotationTypeChildren(f, (AnnotationType) jc); 286 } else if (jc instanceof JavaEnum) { 287 cc = new EnumChildren(f, (JavaEnum) jc); 288 } else { 289 cc = new ClassChildren(f, jc); 290 } 291 return cc; 292 } 293 294 299 ElementCategoryNode(int index, SourceNodeFactory factory, ClassDefinition element, boolean writeable) { 300 this(index, createChildren(factory, element)); 301 this.element = element; 302 newTypeIndex = writeable ? index : -1; 303 switch (index) { 304 case 0: setName("Fields"); break; case 1: setName("Constructors"); break; case 2: setName("Methods"); break; case 3: setName("Constants"); break; case 4: setName("AnnTypeMethods"); break; } 310 } 311 312 316 private ElementCategoryNode(int index, ClassChildren children) { 317 super(children); 318 setDisplayName(NAMES[index]); 319 setShortDescription(SHORTDESCRS[index]); 320 ClassElementFilter filter = new ClassElementFilter(); 321 filter.setOrder(FILTERS[index]); 322 children.setFilter(filter); 323 systemActions = CATEGORY_ACTIONS; 324 setIconBase(CATEGORY_ICONS[index]); 325 } 326 327 329 public boolean canCopy() { 330 return false; 331 } 332 333 336 public NewType[] getNewTypes() { 337 if (!SourceEditSupport.isWriteable(element) || !(element instanceof JavaClass)) { 338 return new NewType[0]; 339 } 340 341 JavaClass jc = (JavaClass) this.element; 342 343 switch(newTypeIndex) { 344 case 0: 345 return new NewType[] { 346 new SourceEditSupport.ElementNewType(jc, SourceEditSupport.NT_FIELD) 347 }; 348 case 1: 349 return new NewType[] { 350 new SourceEditSupport.ElementNewType(jc, SourceEditSupport.NT_INITIALIZER), 351 new SourceEditSupport.ElementNewType(jc, SourceEditSupport.NT_CONSTRUCTOR) 352 }; 353 case 2: 354 return new NewType[] { 355 new SourceEditSupport.ElementNewType(jc, SourceEditSupport.NT_METHOD) 356 }; 357 case 3: 358 return new NewType[] { 359 new SourceEditSupport.ElementNewType(jc, SourceEditSupport.NT_ENUMCONSTANT) 360 }; 361 case 4: 362 return new NewType[] { 363 new SourceEditSupport.ElementNewType(jc, SourceEditSupport.NT_ANNOTATION_TYPE_METHOD) 364 }; 365 default: 366 return super.getNewTypes(); 367 } 368 } 369 370 } 379 380 private static String getString(String key) { 381 return NbBundle.getMessage(Categories.class, key); 382 } 383 384 } 385 | Popular Tags |