1 19 20 package org.netbeans.modules.java.ui.nodes; 21 22 import org.netbeans.modules.javacore.JMManager; 23 import org.openide.nodes.Node; 24 import org.openide.nodes.Children; 25 import org.openide.nodes.FilterNode; 26 import org.openide.nodes.AbstractNode; 27 import org.openide.util.actions.SystemAction; 28 import org.openide.util.NbBundle; 29 import org.openide.util.Lookup; 30 import org.openide.util.lookup.ProxyLookup; 31 import org.openide.actions.*; 32 import org.openide.loaders.DataObject; 33 import org.openide.cookies.OpenCookie; 34 import org.openide.cookies.SaveCookie; 35 import org.netbeans.jmi.javamodel.*; 36 import org.netbeans.modules.java.ui.nodes.elements.*; 37 import org.netbeans.modules.java.JavaDataObject; 38 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 39 40 47 public final class JavaSourceNodeFactory extends FilterSourceNodeFactory { 48 49 private static final JavaSourceNodeFactory DEFAULT = new JavaSourceNodeFactory(); 50 51 54 private boolean tree = false; 55 56 public static SourceNodeFactory getDefault() { 57 return DEFAULT; 58 } 59 60 public Node createFieldNode(Field element) { 61 FieldNode n = new FieldNode(element, !JavaMetamodel.getManager().isElementGuarded(element)); 62 n.setActions(DEFAULT_ACTIONS, SystemAction.get(OpenAction.class)); 63 if (tree) { 64 return n; 65 } else { 66 Node oldNode = getCompatibleFactory().createFieldNode(element); 67 return new CompatibleNode(n, oldNode); 68 } 69 } 70 71 public Node createConstructorNode(Constructor element) { 72 ConstructorNode n = new ConstructorNode(element, !JavaMetamodel.getManager().isElementGuarded(element)); 73 n.setActions(DEFAULT_ACTIONS, SystemAction.get(OpenAction.class)); 74 if (tree) { 75 return n; 76 } else { 77 Node oldNode = getCompatibleFactory().createConstructorNode(element); 78 return new CompatibleNode(n, oldNode); 79 } 80 } 81 82 public Node createMethodNode(Method element) { 83 MethodNode n = new MethodNode(element, !JavaMetamodel.getManager().isElementGuarded(element)); 84 n.setActions(DEFAULT_ACTIONS, SystemAction.get(OpenAction.class)); 85 if (tree) { 86 return n; 87 } else { 88 Node oldNode = getCompatibleFactory().createMethodNode(element); 89 return new CompatibleNode(n, oldNode); 90 } 91 } 92 93 public Node createInitializerNode(Initializer element) { 94 InitializerNode n = new InitializerNode(element, !JavaMetamodel.getManager().isElementGuarded(element)); 95 n.setActions(INITIALIZER_ACTIONS, SystemAction.get(OpenAction.class)); 96 if (tree) { 97 return n; 98 } else { 99 Node oldNode = getCompatibleFactory().createInitializerNode(element); 100 return new CompatibleNode(n, oldNode); 101 } 102 } 103 104 public Node createEnumNode(JavaEnum element) { 105 SourceNodeFactory snf; 106 EnumNode n; 107 if (tree) { 108 snf = SourceNodes.getBrowserFactory(); 109 EnumChildren children = new EnumChildren(snf, element); 110 EnumFilter filter = new EnumFilter(); 111 n = new EnumNode(element, children, !JavaMetamodel.getManager().isElementGuarded(element)); 112 113 n.setElementFormat(new ElementFormat ( 114 NbBundle.getBundle(JavaDataObject.class).getString("CTL_Class_name_format") )); 116 117 filter.setOrder(new int[] { 119 EnumFilter.CONSTANTS, 120 ClassElementFilter.FIELD, 121 ClassElementFilter.CONSTRUCTOR, 122 ClassElementFilter.METHOD, 123 }); 124 children.setFilter(filter); 125 } else { 126 snf = SourceNodes.getExplorerFactory(); 127 n = new EnumNode(element, Categories.createEnumChildren(element, snf, true), !JavaMetamodel.getManager().isElementGuarded(element)); 128 } 129 n.setActions(CLASS_ACTIONS, SystemAction.get(OpenAction.class)); 130 return n; 131 } 132 133 public Node createEnumConstantNode(EnumConstant element) { 134 EnumConstantNode n = new EnumConstantNode(element, Children.LEAF, !JavaMetamodel.getManager().isElementGuarded(element)); 135 n.setActions(DEFAULT_ACTIONS, SystemAction.get(OpenAction.class)); 136 return n; 137 } 138 139 public Node createAnnotationTypeNode(AnnotationType element) { 140 SourceNodeFactory snf; 141 AnnotationTypeNode n; 142 143 if (tree) { 144 snf = SourceNodes.getBrowserFactory(); 145 AnnotationTypeChildren children = new AnnotationTypeChildren(snf, element); 146 AnnotationTypeFilter filter = new AnnotationTypeFilter(); 147 n = new AnnotationTypeNode(element, children, !JavaMetamodel.getManager().isElementGuarded(element)); 148 n.setElementFormat(new ElementFormat( 149 NbBundle.getBundle (JavaDataObject.class).getString("CTL_Class_name_format") )); 151 152 filter.setOrder(new int[] { 154 ClassElementFilter.FIELD, 155 ClassElementFilter.METHOD, 156 }); 157 } else { 158 snf = SourceNodes.getExplorerFactory(); 159 n = new AnnotationTypeNode( 160 element, Categories.createAnnotationTypeChildren(element, snf, true), 161 !JavaMetamodel.getManager().isElementGuarded(element)); 162 } 163 n.setActions(CLASS_ACTIONS, SystemAction.get(OpenAction.class)); 164 return n; 165 } 166 167 public Node createAnnotationTypeMethodNode(Attribute element) { 168 ElementNode n = new AnnotationTypeMethodNode(element, !JavaMetamodel.getManager().isElementGuarded(element)); 169 n.setActions(DEFAULT_ACTIONS, SystemAction.get(OpenAction.class)); 170 return n; 171 } 172 173 public Node createClassNode(JavaClass element) { 174 if (element == null) { 175 return super.createClassNode(element); 177 } 178 179 Node n; 180 if (tree) { 181 ClassNode cn ; 182 SourceNodeFactory snf = SourceNodes.getBrowserFactory(); 183 ClassChildren children = new ClassChildren(snf, element); 184 ClassElementFilter filter = new ClassElementFilter(); 185 cn = new ClassNode(element, children, !JavaMetamodel.getManager().isElementGuarded(element)); 186 187 cn.setElementFormat(new ElementFormat ( 188 NbBundle.getBundle (JavaDataObject.class).getString("CTL_Class_name_format") )); 190 191 filter.setOrder (new int[] { 193 ClassElementFilter.FIELD, 194 ClassElementFilter.CONSTRUCTOR, 195 ClassElementFilter.METHOD, 196 }); 197 children.setFilter (filter); 198 cn.setActions(CLASS_ACTIONS, SystemAction.get(OpenAction.class)); 199 n = cn; 200 } else { 201 ClassNode cn ; 202 SourceNodeFactory snf = SourceNodes.getExplorerFactory(); 203 cn = new ClassNode(element, Categories.createClassChildren(element, snf, true), !JavaMetamodel.getManager().isElementGuarded(element)); 204 cn.setActions(CLASS_ACTIONS, SystemAction.get(OpenAction.class)); 205 206 Node oldNode = getCompatibleFactory().createClassNode(element); 207 n = new CompatibleNode(cn, oldNode); 208 } 209 210 return n; 211 212 } 213 214 public Node createErrorNode() { 215 AbstractNode n = new AbstractNode(Children.LEAF); 216 n.setName(NbBundle.getMessage(ElementNode.class, "Error")); n.setIconBase(IconStrings.ERROR); 218 return n; 219 } 220 221 public Node createWaitNode() { 222 AbstractNode n = new AbstractNode(Children.LEAF); 223 n.setName(NbBundle.getMessage(ElementNode.class, JavaMetamodel.getManager().isScanInProgress()? "WaitScan": "Wait")); n.setIconBase(IconStrings.WAIT); 225 return n; 226 } 227 228 230 public void setGenerateForTree (boolean tree) { 231 this.tree = tree; 232 } 233 234 237 public boolean getGenerateForTree () { 238 return tree; 239 } 240 241 242 243 private static final SystemAction[] DEFAULT_ACTIONS = new SystemAction[] { 244 SystemAction.get(OpenAction.class), 245 null, 246 null, 249 SystemAction.get(DeleteAction.class), 250 null, 252 SystemAction.get(ToolsAction.class), 253 SystemAction.get(PropertiesAction.class) 254 }; 255 256 257 private static final SystemAction[] INITIALIZER_ACTIONS = new SystemAction[] { 258 SystemAction.get(OpenAction.class), 259 null, 260 null, 263 SystemAction.get(DeleteAction.class), 264 null, 265 SystemAction.get(ToolsAction.class), 266 SystemAction.get(PropertiesAction.class) 267 }; 268 269 270 private static final SystemAction[] CLASS_ACTIONS = new SystemAction[] { 271 SystemAction.get(OpenAction.class), 272 null, 273 SystemAction.get(DeleteAction.class), 278 null, 280 SystemAction.get(NewAction.class), 281 null, 282 SystemAction.get(ToolsAction.class), 283 SystemAction.get(PropertiesAction.class) 284 }; 285 286 private static SourceNodeFactory COMPATIBLE_FACTORY; 287 288 289 private static SourceNodeFactory getCompatibleFactory() { 290 if (COMPATIBLE_FACTORY == null) { 291 COMPATIBLE_FACTORY = SourceNodes.createSourceNodeFactory( 292 new ExFilterFactory(org.openide.src.nodes.DefaultFactory.READ_ONLY) 293 ); 294 } 295 return COMPATIBLE_FACTORY; 296 } 297 298 299 private static final class CompatibleNode extends FilterNode { 300 public CompatibleNode(Node current, Node srcModelNode) { 301 super(current, 302 null, 303 new ProxyLookup(new Lookup[] { 304 current.getLookup(), 305 new OldElementNodeWithoutDO(srcModelNode).getLookup() 306 }) 307 ); 308 } 309 } 310 311 315 private static final class OldElementNodeWithoutDO extends FilterNode { 316 public OldElementNodeWithoutDO(Node original) { 317 super(original); 318 } 319 320 public Node.Cookie getCookie(Class type) { 321 Node.Cookie nc = null; 322 if (!(DataObject.class.isAssignableFrom(type) 323 || OpenCookie.class.isAssignableFrom(type) 324 || SaveCookie.class.isAssignableFrom(type))) { 325 326 nc = super.getCookie(type); 327 } 328 return nc; 329 } 330 } 331 } 332 | Popular Tags |