KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > PlatformNode


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
20 package org.netbeans.modules.apisupport.project.ui;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.CharConversionException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.text.MessageFormat JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Set JavaDoc;
34 import javax.swing.Action JavaDoc;
35 import javax.swing.Icon JavaDoc;
36 import javax.swing.ImageIcon JavaDoc;
37 import javax.swing.event.ChangeEvent JavaDoc;
38 import javax.swing.event.ChangeListener JavaDoc;
39 import org.netbeans.api.java.classpath.ClassPath;
40 import org.netbeans.api.java.queries.JavadocForBinaryQuery;
41 import org.openide.filesystems.FileObject;
42 import org.openide.filesystems.FileStateInvalidException;
43 import org.openide.filesystems.FileUtil;
44 import org.openide.nodes.Children;
45 import org.openide.nodes.AbstractNode;
46 import org.openide.nodes.Node;
47 import org.openide.util.NbBundle;
48 import org.openide.util.RequestProcessor;
49 import org.openide.util.Utilities;
50 import org.openide.util.WeakListeners;
51 import org.openide.ErrorManager;
52 import org.netbeans.api.java.platform.JavaPlatform;
53 import org.netbeans.api.java.platform.JavaPlatformManager;
54 import org.netbeans.api.project.SourceGroup;
55 import org.netbeans.modules.apisupport.project.ui.customizer.ModuleProperties;
56 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
57 import org.netbeans.spi.java.project.support.ui.PackageView;
58 import org.openide.util.actions.SystemAction;
59 import org.openide.util.lookup.Lookups;
60 import org.openide.xml.XMLUtil;
61
62 // XXX this class is more or less copy-pasted from j2seproject.
63
// Get rid of it as soon as "some" Libraries Node API is provided.
64

65 /**
66  * PlatformNode represents Java platform in the logical view.
67  * Listens on the {@link PropertyEvaluator} for change of
68  * the ant property holding the platform name.
69  * It displays the content of boot classpath.
70  * @see JavaPlatform
71  * @author Tomas Zezula
72  */

73 final class PlatformNode extends AbstractNode implements ChangeListener JavaDoc {
74     
75     private static final String JavaDoc PLATFORM_ICON = "org/netbeans/modules/apisupport/project/ui/resources/platform.gif"; //NOI18N
76
private static final String JavaDoc ARCHIVE_ICON = "org/netbeans/modules/apisupport/project/ui/resources/jar.gif"; //NOI18N
77

78     private final PlatformProvider pp;
79     
80     private PlatformNode(PlatformProvider pp) {
81         super(new PlatformContentChildren(), Lookups.singleton(new JavadocProvider(pp)));
82         this.pp = pp;
83         this.pp.addChangeListener(this);
84         setIconBaseWithExtension(PLATFORM_ICON);
85     }
86     
87     public String JavaDoc getName() {
88         return this.getDisplayName();
89     }
90     
91     public String JavaDoc getDisplayName() {
92         JavaPlatform plat = pp.getPlatform();
93         String JavaDoc name;
94         if (plat != null) {
95             name = plat.getDisplayName();
96         } else {
97             String JavaDoc platformId = pp.getPlatformID();
98             if (platformId == null) {
99                 name = NbBundle.getMessage(PlatformNode.class,"TXT_BrokenPlatform");
100             } else {
101                 name = MessageFormat.format(NbBundle.getMessage(PlatformNode.class,"FMT_BrokenPlatform"), new Object JavaDoc[] {platformId});
102             }
103         }
104         return name;
105     }
106     
107     public String JavaDoc getHtmlDisplayName() {
108         if (pp.getPlatform() == null) {
109             String JavaDoc displayName = this.getDisplayName();
110             try {
111                 displayName = XMLUtil.toElementContent(displayName);
112             } catch (CharConversionException JavaDoc ex) {
113                 // OK, no annotation in this case
114
return null;
115             }
116             return "<font color=\"#A40000\">" + displayName + "</font>"; //NOI18N
117
} else {
118             return null;
119         }
120     }
121     
122     public boolean canCopy() {
123         return false;
124     }
125     
126     public Action JavaDoc[] getActions(boolean context) {
127         return new Action JavaDoc[] {
128             SystemAction.get(ShowJavadocAction.class)
129         };
130     }
131     
132     public void stateChanged(ChangeEvent JavaDoc e) {
133         this.fireNameChange(null, null);
134         this.fireDisplayNameChange(null, null);
135         //The caller holds ProjectManager.mutex() read lock
136
RequestProcessor.getDefault().post(new Runnable JavaDoc() {
137             public void run() {
138                 ((PlatformContentChildren) getChildren()).addNotify();
139             }
140         });
141     }
142     
143     /**
144      * Creates new PlatformNode
145      * @param eval the PropertyEvaluator used for obtaining the active platform name
146      * and listening on the active platform change
147      * @param platformPropName the name of ant property holding the platform name
148      */

149     static PlatformNode create(PropertyEvaluator eval, String JavaDoc platformPropName) {
150         PlatformProvider pp = new PlatformProvider(eval, platformPropName);
151         return new PlatformNode(pp);
152     }
153     
154     private static class PlatformContentChildren extends Children.Keys {
155         
156         PlatformContentChildren() {
157         }
158         
159         protected void addNotify() {
160             this.setKeys(this.getKeys());
161         }
162         
163         protected void removeNotify() {
164             this.setKeys(Collections.EMPTY_SET);
165         }
166         
167         protected Node[] createNodes(Object JavaDoc key) {
168             SourceGroup sg = (SourceGroup) key;
169             return new Node[] { ActionFilterNode.create(PackageView.createPackageView(sg)) };
170         }
171         
172         private List JavaDoc getKeys() {
173             JavaPlatform platform = ((PlatformNode)this.getNode()).pp.getPlatform();
174             if (platform == null) {
175                 return Collections.EMPTY_LIST;
176             }
177             //Todo: Should listen on returned classpath, but now the bootstrap libraries are read only
178
FileObject[] roots = platform.getBootstrapLibraries().getRoots();
179             List JavaDoc result = new ArrayList JavaDoc(roots.length);
180             for (int i=0; i<roots.length; i++) {
181                 try {
182                     FileObject file;
183                     Icon JavaDoc icon;
184                     if ("jar".equals(roots[i].getURL().getProtocol())) { //NOI18N
185
file = FileUtil.getArchiveFile(roots[i]);
186                         icon = new ImageIcon JavaDoc(Utilities.loadImage(ARCHIVE_ICON));
187                     } else {
188                         file = roots[i];
189                         icon = null;
190                     }
191                     if (file.isValid()) {
192                         result.add(new LibrariesSourceGroup(roots[i], file.getNameExt(), icon, icon));
193                     }
194                 } catch (FileStateInvalidException e) {
195                     ErrorManager.getDefault().notify(e);
196                 }
197             }
198             return result;
199         }
200     }
201     
202     private static class PlatformProvider implements PropertyChangeListener JavaDoc {
203         
204         private final PropertyEvaluator evaluator;
205         private final String JavaDoc platformPropName;
206         private JavaPlatform platformCache;
207         private List JavaDoc<ChangeListener JavaDoc> listeners;
208         
209         public PlatformProvider(PropertyEvaluator evaluator, String JavaDoc platformPropName) {
210             this.evaluator = evaluator;
211             this.platformPropName = platformPropName;
212             this.evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this,evaluator));
213         }
214         
215         public String JavaDoc getPlatformID() {
216             return this.evaluator.getProperty(this.platformPropName);
217         }
218         
219         public JavaPlatform getPlatform() {
220             if (platformCache == null) {
221                 final String JavaDoc platformPropName = getPlatformID();
222                 platformCache = ModuleProperties.findJavaPlatformByLocation(platformPropName);
223                 if (platformCache != null && platformCache.getInstallFolders().size() == 0) {
224                     //Deleted platform
225
platformCache = null;
226                 }
227                 //Issue: #57840: Broken platform 'default_platform'
228
if (ErrorManager.getDefault().isLoggable(ErrorManager.INFORMATIONAL) && platformCache == null) {
229                     StringBuffer JavaDoc message = new StringBuffer JavaDoc("RequestedPlatform: " + platformPropName + " not found.\nInstalled Platforms:\n"); // NOI18N
230
JavaPlatform[] platforms = JavaPlatformManager.getDefault().getInstalledPlatforms();
231                     for (int i=0; i<platforms.length; i++) {
232                         message.append("Name: "+platforms[i].getProperties().get("platform.ant.name")+ // NOI18N
233
" Broken: "+ (platforms[i].getInstallFolders().size() == 0) + "\n"); // NOI18N
234
}
235                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, message.toString());
236                 }
237             }
238             return platformCache;
239         }
240         
241         public synchronized void addChangeListener(ChangeListener JavaDoc l) {
242             if (this.listeners == null) {
243                 this.listeners = new ArrayList JavaDoc();
244             }
245             this.listeners.add(l);
246         }
247         
248         public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
249             if (this.listeners == null) {
250                 return;
251             }
252             this.listeners.remove(l);
253         }
254         
255         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
256             if (platformPropName.equals(evt.getPropertyName())) {
257                 platformCache = null;
258                 this.fireChange();
259             }
260         }
261         
262         private void fireChange() {
263             ChangeListener JavaDoc[] _listeners;
264             synchronized (this) {
265                 if (this.listeners == null) {
266                     return;
267                 }
268                 _listeners = (ChangeListener JavaDoc[]) this.listeners.toArray(new ChangeListener JavaDoc[listeners.size()]);
269             }
270             ChangeEvent JavaDoc event = new ChangeEvent JavaDoc(this);
271             for (int i=0; i< _listeners.length; i++) {
272                 _listeners[i].stateChanged(event);
273             }
274         }
275         
276     }
277     
278     private static class JavadocProvider implements ShowJavadocAction.JavadocProvider {
279         
280         PlatformProvider platformProvider;
281         
282         private JavadocProvider(PlatformProvider platformProvider) {
283             this.platformProvider = platformProvider;
284         }
285         
286         public boolean hasJavadoc() {
287             JavaPlatform platform = platformProvider.getPlatform();
288             if (platform == null) {
289                 return false;
290             }
291             URL JavaDoc[] javadocRoots = getJavadocRoots(platform);
292             return javadocRoots.length > 0;
293         }
294         
295         public void showJavadoc() {
296             JavaPlatform platform = platformProvider.getPlatform();
297             if (platform != null) {
298                 URL JavaDoc[] javadocRoots = getJavadocRoots(platform);
299                 URL JavaDoc pageURL = ShowJavadocAction.findJavadoc("overview-summary.html",javadocRoots);
300                 if (pageURL == null) {
301                     pageURL = ShowJavadocAction.findJavadoc("index.html",javadocRoots);
302                 }
303                 ShowJavadocAction.showJavaDoc(pageURL, platform.getDisplayName());
304             }
305         }
306         
307         private static URL JavaDoc[] getJavadocRoots(JavaPlatform platform) {
308             Set JavaDoc result = new HashSet JavaDoc();
309             List JavaDoc<ClassPath.Entry> l = platform.getBootstrapLibraries().entries();
310             for (Iterator JavaDoc it = l.iterator(); it.hasNext();) {
311                 ClassPath.Entry e = (ClassPath.Entry) it.next();
312                 result.addAll(Arrays.asList(JavadocForBinaryQuery.findJavadoc(e.getURL()).getRoots()));
313             }
314             return (URL JavaDoc[]) result.toArray(new URL JavaDoc[result.size()]);
315         }
316         
317     }
318     
319 }
320
321
322
Popular Tags