KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > classpath > BootClassPathImplementation


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.j2seproject.classpath;
20
21 import java.beans.PropertyChangeEvent JavaDoc;
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 JavaDoc;
30 import java.beans.PropertyChangeSupport JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collections JavaDoc;
34 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
35 import org.openide.util.WeakListeners;
36
37 final class BootClassPathImplementation implements ClassPathImplementation, PropertyChangeListener JavaDoc {
38
39     private static final String JavaDoc PLATFORM_ACTIVE = "platform.active"; //NOI18N
40
private static final String JavaDoc ANT_NAME = "platform.ant.name"; //NOI18N
41
private static final String JavaDoc J2SE = "j2se"; //NOI18N
42

43     private final PropertyEvaluator evaluator;
44     private JavaPlatformManager platformManager;
45     //name of project active platform
46
private String JavaDoc activePlatformName;
47     //active platform is valid (not broken reference)
48
private boolean isActivePlatformValid;
49     private List JavaDoc<PathResourceImplementation> resourcesCache;
50     private PropertyChangeSupport JavaDoc support = new PropertyChangeSupport JavaDoc(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 JavaDoc<PathResourceImplementation> getResources() {
59         if (this.resourcesCache == null) {
60             JavaPlatform jp = findActivePlatform ();
61             if (jp != null) {
62                 //TODO: May also listen on CP, but from Platform it should be fixed.
63
List JavaDoc<PathResourceImplementation> result = new ArrayList JavaDoc<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 JavaDoc listener) {
77         this.support.addPropertyChangeListener (listener);
78     }
79
80     public void removePropertyChangeListener(PropertyChangeListener JavaDoc 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 JavaDoc evt) {
96         if (evt.getSource() == this.evaluator && evt.getPropertyName().equals(PLATFORM_ACTIVE)) {
97             //Active platform was changed
98
resetCache ();
99         }
100         else if (evt.getSource() == this.platformManager && JavaPlatformManager.PROP_INSTALLED_PLATFORMS.equals(evt.getPropertyName()) && activePlatformName != null) {
101             //Platform definitions were changed, check if the platform was not resolved or deleted
102
if (this.isActivePlatformValid) {
103                 if (J2SEProjectUtil.getActivePlatform (this.activePlatformName) == null) {
104                     //the platform was not removed
105
this.resetCache();
106                 }
107             }
108             else {
109                 if (J2SEProjectUtil.getActivePlatform (this.activePlatformName) != null) {
110                     this.resetCache();
111                 }
112             }
113         }
114     }
115     
116     /**
117      * Resets the cache and firesPropertyChange
118      */

119     private void resetCache () {
120         synchronized (this) {
121             resourcesCache = null;
122         }
123         support.firePropertyChange(PROP_RESOURCES, null, null);
124     }
125     
126 }
127
Popular Tags