1 19 package org.netbeans.modules.java.classpath; 20 21 import org.netbeans.spi.java.classpath.ClassPathImplementation; 22 import org.netbeans.spi.java.classpath.PathResourceImplementation; 23 24 import java.util.List ; 25 import java.util.ArrayList ; 26 import java.util.Iterator ; 27 import java.util.Collections ; 28 import java.beans.PropertyChangeListener ; 29 import java.beans.PropertyChangeEvent ; 30 import org.openide.util.WeakListeners; 31 32 37 public class ProxyClassPathImplementation implements ClassPathImplementation { 38 39 private ClassPathImplementation[] classPaths; 40 private List <PathResourceImplementation> resourcesCache; 41 private ArrayList <PropertyChangeListener > listeners; 42 private PropertyChangeListener classPathsListener; 43 44 public ProxyClassPathImplementation (ClassPathImplementation[] classPaths) { 45 if (classPaths == null) 46 throw new IllegalArgumentException (); 47 List <ClassPathImplementation> impls = new ArrayList <ClassPathImplementation> (); 48 classPathsListener = new DelegatesListener (); 49 for (ClassPathImplementation cpImpl : classPaths) { 50 if (cpImpl == null) 51 continue; 52 cpImpl.addPropertyChangeListener (WeakListeners.propertyChange(classPathsListener,cpImpl)); 53 impls.add (cpImpl); 54 } 55 this.classPaths = impls.toArray(new ClassPathImplementation[impls.size()]); 56 } 57 58 59 60 public List <? extends PathResourceImplementation> getResources() { 61 synchronized (this) { 62 if (this.resourcesCache != null) { 63 return this.resourcesCache; 64 } 65 } 66 67 ArrayList <PathResourceImplementation> result = new ArrayList <PathResourceImplementation> (classPaths.length*10); 68 for (ClassPathImplementation cpImpl : classPaths) { 69 List <? extends PathResourceImplementation> subPath = cpImpl.getResources(); 70 assert subPath != null : "ClassPathImplementation.getResources() returned null. ClassPathImplementation.class: " 71 + cpImpl.getClass().toString() + " ClassPathImplementation: " + cpImpl.toString(); 72 result.addAll (subPath); 73 } 74 75 synchronized (this) { 76 if (this.resourcesCache == null) { 77 resourcesCache = Collections.unmodifiableList (result); 78 } 79 return this.resourcesCache; 80 } 81 } 82 83 public synchronized void addPropertyChangeListener(PropertyChangeListener listener) { 84 if (this.listeners == null) 85 this.listeners = new ArrayList <PropertyChangeListener > (); 86 this.listeners.add (listener); 87 } 88 89 public synchronized void removePropertyChangeListener(PropertyChangeListener listener) { 90 if (this.listeners == null) 91 return; 92 this.listeners.remove (listener); 93 } 94 95 public String toString () { 96 StringBuilder builder = new StringBuilder ("["); for (ClassPathImplementation cpImpl : this.classPaths) { 98 builder.append (cpImpl.toString()); 99 builder.append(", "); } 101 builder.append ("]"); return builder.toString (); 103 } 104 105 106 private class DelegatesListener implements PropertyChangeListener { 107 108 public void propertyChange(PropertyChangeEvent evt) { 109 PropertyChangeListener [] _listeners; 110 synchronized (ProxyClassPathImplementation.this) { 111 ProxyClassPathImplementation.this.resourcesCache = null; if (ProxyClassPathImplementation.this.listeners == null) 113 return; 114 _listeners = ProxyClassPathImplementation.this.listeners.toArray(new PropertyChangeListener [ProxyClassPathImplementation.this.listeners.size()]); 115 } 116 PropertyChangeEvent event = new PropertyChangeEvent (ProxyClassPathImplementation.this, evt.getPropertyName(),null,null); 117 for (PropertyChangeListener l : _listeners) { 118 l.propertyChange (event); 119 } 120 } 121 } 122 123 } 124 | Popular Tags |