1 19 20 package org.openide.src.nodes; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeEvent ; 24 import java.util.Collection ; 25 import java.util.LinkedList ; 26 import java.util.Arrays ; 27 import java.util.List ; 28 29 import org.openide.nodes.Children; 30 import org.openide.nodes.Node; 31 import org.openide.cookies.FilterCookie; 32 import org.openide.src.ElementProperties; 33 import org.openide.src.SourceElement; 34 import org.openide.src.ClassElement; 35 import org.openide.util.WeakListeners; 36 37 53 public class SourceChildren extends Children.Keys implements FilterCookie { 54 55 56 static final Object NOT_KEY = new Object (); 57 58 static final Object ERROR_KEY = new Object (); 59 60 private static int PPP_MASK = SourceElementFilter.PUBLIC + 61 SourceElementFilter.PRIVATE + 62 SourceElementFilter.PROTECTED; 63 64 65 protected SourceElement element; 66 68 protected SourceElementFilter filter; 69 70 protected ElementNodeFactory factory; 71 72 private PropertyChangeListener wPropL; 73 75 private ElementListener propL; 76 77 private boolean nodesInited = false; 78 79 80 82 84 public SourceChildren () { 85 this (DefaultFactory.READ_WRITE, null); 86 } 87 88 91 public SourceChildren (final SourceElement element) { 92 this(DefaultFactory.READ_WRITE, element); 93 } 94 95 98 public SourceChildren (final ElementNodeFactory factory) { 99 this(factory, null); 100 } 101 102 106 public SourceChildren (final ElementNodeFactory factory, 107 final SourceElement element) { 108 super(); 109 this.element = element; 110 this.factory = factory; 111 this.filter = new SourceElementFilter (); 112 } 113 114 115 117 120 public Class getFilterClass () { 121 return SourceElementFilter.class; 122 } 123 124 126 public Object getFilter () { 127 return filter; 128 } 129 130 133 public void setFilter (final Object filter) { 134 if (!(filter instanceof SourceElementFilter)) 135 throw new IllegalArgumentException (); 136 137 this.filter = (SourceElementFilter)filter; 138 if (nodesInited) 140 refreshKeys (); 141 } 142 143 144 146 149 protected void addNotify () { 150 if (element != null) { 151 if (wPropL == null) { 153 propL = new ElementListener(); 154 wPropL = WeakListeners.propertyChange(propL, element); 155 } 156 element.addPropertyChangeListener (wPropL); 157 element.prepare(); 158 } 159 refreshKeys (); 160 nodesInited = true; 161 } 162 163 protected void removeNotify () { 164 setKeys (java.util.Collections.EMPTY_SET); 165 nodesInited = false; 166 } 167 168 171 protected Node[] createNodes (final Object key) { 172 if (key instanceof ClassElement) 174 return new Node[] { factory.createClassNode((ClassElement)key) }; 175 if (NOT_KEY.equals(key)) 176 return new Node[] { factory.createWaitNode() }; 177 return new Node[] { factory.createErrorNode() }; 179 } 180 181 186 public Node[] getNodes(boolean initialize) { 187 SourceElement el = element; 188 if (!initialize || el == null) { 189 return super.getNodes(); 190 } 191 org.openide.util.Task t = el.prepare(); 192 Node[] result; 193 194 t.waitFinished(); 195 refreshKeys(); 196 result = getNodes(); 197 t.isFinished(); 198 return result; 199 } 200 201 204 public Node findChild (String name) { 205 Node supe = super.findChild (name); 206 if (supe != null) { 207 return supe; 208 } else { 209 if (element != null) { 210 org.openide.util.Task t = element.prepare (); 214 t.waitFinished (); 215 refreshKeys (); 216 Node n = super.findChild (name); 217 t.isFinished(); 218 return n; 219 } else { 220 return null; 221 } 222 } 223 } 224 225 226 228 231 public SourceElement getElement () { 232 return element; 233 } 234 235 238 public void setElement (final SourceElement element) { 239 if (this.element != null) { 240 this.element.removePropertyChangeListener(wPropL); 241 } 242 this.element = element; 243 if (this.element != null) { 244 if (wPropL == null) { 245 propL = new ElementListener(); 246 wPropL = WeakListeners.propertyChange(propL, this.element); 247 } 248 this.element.addPropertyChangeListener(wPropL); 249 } 250 if (nodesInited) { 252 if (this.element != null) this.element.prepare(); 253 refreshKeys (); 254 } 255 } 256 257 259 268 private void refreshKeys () { 269 int status = (element == null) ? SourceElement.STATUS_ERROR 270 : element.getStatus(); 271 switch (status) { 272 case SourceElement.STATUS_NOT: 273 setKeys(new Object [] { NOT_KEY }); 274 275 element.prepare (); 277 278 break; 279 case SourceElement.STATUS_ERROR: 280 setKeys(new Object [] { ERROR_KEY }); 281 break; 282 case SourceElement.STATUS_PARTIAL: 283 case SourceElement.STATUS_OK: 284 refreshAllKeys(); 285 break; 286 } 287 } 288 289 291 private void refreshAllKeys () { 292 int[] order = (filter == null || (filter.getOrder() == null)) 293 ? SourceElementFilter.DEFAULT_ORDER : filter.getOrder(); 294 295 final LinkedList keys = new LinkedList (); 296 for (int i = 0; i < order.length; i++) 298 addKeysOfType(keys, order[i]); 299 300 org.openide.src.nodes.SourceChildren.this.setKeys(keys); 302 } 303 304 307 private void addKeysOfType (Collection keys, final int elementType) { 308 if (elementType == SourceElementFilter.IMPORT) { 309 return; 312 } else { 313 List cls; 314 if ((filter != null) && filter.isAllClasses()) { 315 cls = Arrays.asList (element.getAllClasses ()); 316 } else { 317 cls = Arrays.asList (element.getClasses ()); 318 } 319 for (int i = 0; i < cls.size() ; i++) { 320 ClassElement classElement = (ClassElement)cls.get (i); 321 int modifiers = classElement.getModifiers (); 322 if ((modifiers & PPP_MASK) == 0) modifiers += SourceElementFilter.PACKAGE; 323 if ((filter.getModifiers () & modifiers) == 0) continue; 324 if (classElement.isClass ()) { 325 if ((elementType & SourceElementFilter.CLASS) != 0) keys.add (classElement); 326 } else 327 if ((elementType & SourceElementFilter.INTERFACE) != 0) keys.add (classElement); 328 } 329 } 330 } 331 332 333 335 337 private final class ElementListener implements PropertyChangeListener { 338 public void propertyChange (PropertyChangeEvent evt) { 339 boolean refresh = ElementProperties.PROP_CLASSES.equals(evt.getPropertyName()); 340 if (!refresh && ElementProperties.PROP_STATUS.equals(evt.getPropertyName())) { 341 Integer val = (Integer ) evt.getNewValue(); 342 refresh = ((val == null) || (val.intValue() != SourceElement.STATUS_NOT)); 343 } 344 if (refresh) 345 javax.swing.SwingUtilities.invokeLater(new Runnable () { 346 public void run() { 347 refreshKeys(); 348 } 349 }); 350 } 351 352 } } 354 | Popular Tags |