1 19 20 package org.netbeans.modules.java; 21 22 import java.beans.*; 23 import java.util.Collection ; 24 import java.util.LinkedList ; 25 26 import org.openide.actions.*; 27 import org.openide.cookies.OpenCookie; 28 import org.openide.cookies.FilterCookie; 29 import org.openide.nodes.*; 30 import org.openide.loaders.DataObject; 31 import org.openide.src.*; 32 import org.openide.src.nodes.*; 33 import org.openide.util.NbBundle; 34 import org.openide.util.actions.SystemAction; 35 import org.openide.*; 36 37 import org.netbeans.modules.java.tools.*; 38 import org.netbeans.modules.java.parser.JavaParser; 39 40 44 class JavaElementNodeFactory extends DefaultFactory { 45 46 public static final DefaultFactory DEFAULT = new JavaElementNodeFactory(); 47 48 49 private static final SystemAction[] DEFAULT_ACTIONS = new SystemAction[] { 50 SystemAction.get(OpenAction.class), 51 null, 52 SystemAction.get(CutAction.class), 53 SystemAction.get(CopyAction.class), 54 null, 55 SystemAction.get(DeleteAction.class), 56 SystemAction.get(RenameAction.class), 57 null, 58 SystemAction.get(ToolsAction.class), 59 SystemAction.get(PropertiesAction.class) 60 }; 61 62 63 private static final SystemAction[] INITIALIZER_ACTIONS = new SystemAction[] { 64 SystemAction.get(OpenAction.class), 65 null, 66 SystemAction.get(CutAction.class), 67 SystemAction.get(CopyAction.class), 68 null, 69 SystemAction.get(DeleteAction.class), 70 null, 71 SystemAction.get(ToolsAction.class), 72 SystemAction.get(PropertiesAction.class) 73 }; 74 75 76 private static final SystemAction[] CLASS_ACTIONS = new SystemAction[] { 77 SystemAction.get(OpenAction.class), 78 null, 79 SystemAction.get(CutAction.class), 80 SystemAction.get(CopyAction.class), 81 SystemAction.get(PasteAction.class), 82 null, 83 SystemAction.get(DeleteAction.class), 84 SystemAction.get(RenameAction.class), 85 null, 86 SystemAction.get(NewAction.class), 87 null, 88 SystemAction.get(ToolsAction.class), 89 SystemAction.get(PropertiesAction.class) 90 }; 91 92 93 private final Node FACTORY_GETTER_NODE = new FactoryGetterNode(); 94 95 96 97 private boolean tree = false; 98 99 100 public JavaElementNodeFactory() { 101 super(true); 102 } 103 104 106 public void setGenerateForTree (boolean tree) { 107 this.tree = tree; 108 } 109 110 113 public boolean getGenerateForTree () { 114 return tree; 115 } 116 117 120 public Node createMethodNode(MethodElement element) { 121 MethodElementNode n = new MethodElementNode(element, true); 122 n.setDefaultAction(SystemAction.get(OpenAction.class)); 123 n.setActions(DEFAULT_ACTIONS); 124 return n; 125 } 126 127 130 public Node createConstructorNode(ConstructorElement element) { 131 ConstructorElementNode n = new ConstructorElementNode(element, true); 132 n.setDefaultAction(SystemAction.get(OpenAction.class)); 133 n.setActions(DEFAULT_ACTIONS); 134 return n; 135 } 136 137 140 public Node createFieldNode(FieldElement element) { 141 FieldElementNode n = new FieldElementNode(element, true); 142 n.setDefaultAction(SystemAction.get(OpenAction.class)); 143 n.setActions(DEFAULT_ACTIONS); 144 return n; 145 } 146 147 150 public Node createInitializerNode(InitializerElement element) { 151 InitializerElementNode n = new InitializerElementNode(element, true); 152 n.setDefaultAction(SystemAction.get(OpenAction.class)); 153 n.setActions(INITIALIZER_ACTIONS); 154 return n; 155 } 156 157 160 public Node createClassNode(final ClassElement element) { 161 162 if ( element == null ) { 163 return FACTORY_GETTER_NODE; 164 } 165 166 167 ClassElementNode n; 168 if (tree) { 169 ClassChildren children = new ClassChildren( JavaDataObject.getBrowserFactory(), element); 170 ClassElementFilter filter = new ClassElementFilter(); 171 n = new ClassElementNode(element, children ,true) { 172 { 173 getCookieSet().add((FilterCookie) getChildren ()); 174 } 175 }; 176 177 n.setElementFormat(new ElementFormat ( 178 NbBundle.getBundle (JavaElementNodeFactory.class).getString("CTL_Class_name_format") 179 )); 180 181 filter.setOrder (new int[] { 183 ClassElementFilter.FIELD, 184 ClassElementFilter.CONSTRUCTOR, 185 ClassElementFilter.METHOD, 186 }); 187 children.setFilter (filter); 188 } 189 else { 190 n = (ClassElementNode) super.createClassNode(element); 191 } 192 n.setDefaultAction(SystemAction.get(OpenAction.class)); 193 n.setActions(CLASS_ACTIONS); 194 return n; 195 } 196 197 protected Children createClassChildren( ClassElement element ) { 198 return createClassChildren( element, JavaDataObject.getExplorerFactory() ); 199 } 200 201 204 public Node createErrorNode() { 205 final Node n = super.createErrorNode(); 206 207 n.addNodeListener(new NodeListener() { 208 public void propertyChange(PropertyChangeEvent e) { 209 Node parent = n.getParentNode(); 210 DataObject d = null; 211 212 if (parent == null) 213 return; 214 n.removeNodeListener(this); 215 do { 216 d = (DataObject)parent.getCookie(DataObject.class); 217 parent = parent.getParentNode(); 218 } while (parent != null && d == null); 219 if (d == null) 220 return; 221 setErrorDescription(n, (JavaParser)d.getCookie(JavaParser.class)); 222 } 223 224 public void childrenAdded(NodeMemberEvent e) {} 225 public void childrenRemoved(NodeMemberEvent e) {} 226 public void childrenReordered(NodeReorderEvent e) {} 227 public void nodeDestroyed(NodeEvent e) {} 228 }); 229 return n; 230 } 231 232 private void setErrorDescription(Node n, JavaParser p) { 233 if (p == null) 234 return; 235 SourceException e = p.getErrorCause(); 236 String msg = findErrorMessage(e); 237 if (msg == null) 238 return; 239 240 if (e instanceof SourceException.IO) { 241 n.setDisplayName(Util.getString("MSG_PARSE_ERROR_IO")); 242 } 243 n.setShortDescription(msg); 244 } 245 246 private String findErrorMessage(Throwable t) { 247 if (t == null) { 248 return null; 249 } 250 251 ErrorManager.Annotation[] ann = ErrorManager.getDefault().findAnnotations(t); 252 if (ann == null) 253 return t.getLocalizedMessage(); 254 for (int i = 0; i < ann.length; i++) { 255 String normal = ann[i].getMessage(); 256 String localized = ann[i].getLocalizedMessage(); 257 if (localized != null) 258 return localized; 259 Throwable t2 = ann[i].getStackTrace(); 260 if (t2 == null) 261 continue; 262 localized = t2.getLocalizedMessage(); 263 if (localized != null) { 264 if (!localized.equals(normal)) 265 return localized; 266 } 267 } 268 return null; 269 } 270 271 272 273 private class FactoryGetterNode extends AbstractNode implements FilterCookie { 274 275 FactoryGetterNode( ) { 276 super ( Children.LEAF ); 277 } 278 279 public synchronized Node.Cookie getCookie( Class clazz ) { 280 if ( clazz == FilterFactory.class ) 281 return this; 282 else 283 return super.getCookie( clazz ); 284 } 285 286 public Class getFilterClass() { 287 return null; 288 } 289 290 public void setFilter( Object filter ) {} 291 292 public Object getFilter( ) { 293 if ( tree ) 294 return JavaDataObject.getBrowserFactory(); 295 else 296 return JavaDataObject.getExplorerFactory(); 297 } 298 299 } 300 301 } 302 303 | Popular Tags |