1 19 package org.netbeans.modules.java.j2seproject.classpath; 20 21 import java.beans.PropertyChangeEvent ; 22 import org.netbeans.modules.java.j2seproject.J2SEProjectUtil; 23 import org.netbeans.spi.java.classpath.ClassPathImplementation; 24 import org.netbeans.spi.java.classpath.PathResourceImplementation; 25 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 26 import org.netbeans.api.java.platform.JavaPlatform; 27 import org.netbeans.api.java.platform.JavaPlatformManager; 28 import org.netbeans.api.java.classpath.ClassPath; 29 import java.beans.PropertyChangeListener ; 30 import java.beans.PropertyChangeSupport ; 31 import java.util.List ; 32 import java.util.ArrayList ; 33 import java.util.Collections ; 34 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 35 import org.openide.util.WeakListeners; 36 37 final class BootClassPathImplementation implements ClassPathImplementation, PropertyChangeListener { 38 39 private static final String PLATFORM_ACTIVE = "platform.active"; private static final String ANT_NAME = "platform.ant.name"; private static final String J2SE = "j2se"; 43 private final PropertyEvaluator evaluator; 44 private JavaPlatformManager platformManager; 45 private String activePlatformName; 47 private boolean isActivePlatformValid; 49 private List <PathResourceImplementation> resourcesCache; 50 private PropertyChangeSupport support = new PropertyChangeSupport (this); 51 52 public BootClassPathImplementation(PropertyEvaluator evaluator) { 53 assert evaluator != null; 54 this.evaluator = evaluator; 55 evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator)); 56 } 57 58 public synchronized List <PathResourceImplementation> getResources() { 59 if (this.resourcesCache == null) { 60 JavaPlatform jp = findActivePlatform (); 61 if (jp != null) { 62 List <PathResourceImplementation> result = new ArrayList <PathResourceImplementation>(); 64 for (ClassPath.Entry entry : jp.getBootstrapLibraries().entries()) { 65 result.add(ClassPathSupport.createResource(entry.getURL())); 66 } 67 resourcesCache = Collections.unmodifiableList (result); 68 } 69 else { 70 resourcesCache = Collections.emptyList(); 71 } 72 } 73 return this.resourcesCache; 74 } 75 76 public void addPropertyChangeListener(PropertyChangeListener listener) { 77 this.support.addPropertyChangeListener (listener); 78 } 79 80 public void removePropertyChangeListener(PropertyChangeListener listener) { 81 this.support.removePropertyChangeListener (listener); 82 } 83 84 private JavaPlatform findActivePlatform () { 85 if (this.platformManager == null) { 86 this.platformManager = JavaPlatformManager.getDefault(); 87 this.platformManager.addPropertyChangeListener(WeakListeners.propertyChange(this, this.platformManager)); 88 } 89 this.activePlatformName = evaluator.getProperty(PLATFORM_ACTIVE); 90 final JavaPlatform activePlatform = J2SEProjectUtil.getActivePlatform (this.activePlatformName); 91 this.isActivePlatformValid = activePlatform != null; 92 return activePlatform; 93 } 94 95 public void propertyChange(PropertyChangeEvent evt) { 96 if (evt.getSource() == this.evaluator && evt.getPropertyName().equals(PLATFORM_ACTIVE)) { 97 resetCache (); 99 } 100 else if (evt.getSource() == this.platformManager && JavaPlatformManager.PROP_INSTALLED_PLATFORMS.equals(evt.getPropertyName()) && activePlatformName != null) { 101 if (this.isActivePlatformValid) { 103 if (J2SEProjectUtil.getActivePlatform (this.activePlatformName) == null) { 104 this.resetCache(); 106 } 107 } 108 else { 109 if (J2SEProjectUtil.getActivePlatform (this.activePlatformName) != null) { 110 this.resetCache(); 111 } 112 } 113 } 114 } 115 116 119 private void resetCache () { 120 synchronized (this) { 121 resourcesCache = null; 122 } 123 support.firePropertyChange(PROP_RESOURCES, null, null); 124 } 125 126 } 127 | Popular Tags |