KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > 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.java.j2seproject.ui;
21
22
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.io.CharConversionException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.text.MessageFormat JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Set JavaDoc;
35 import javax.swing.Action JavaDoc;
36 import javax.swing.Icon JavaDoc;
37 import javax.swing.ImageIcon JavaDoc;
38 import javax.swing.event.ChangeEvent JavaDoc;
39 import javax.swing.event.ChangeListener JavaDoc;
40 import org.netbeans.api.java.classpath.ClassPath;
41 import org.netbeans.api.java.queries.JavadocForBinaryQuery;
42 import org.netbeans.modules.java.j2seproject.J2SEProjectUtil;
43 import org.openide.filesystems.FileObject;
44 import org.openide.filesystems.FileStateInvalidException;
45 import org.openide.filesystems.FileUtil;
46 import org.openide.filesystems.URLMapper;
47 import org.openide.nodes.Children;
48 import org.openide.nodes.AbstractNode;
49 import org.openide.nodes.Node;
50 import org.openide.util.NbBundle;
51 import org.openide.util.Utilities;
52 import org.openide.util.WeakListeners;
53 import org.openide.ErrorManager;
54 import org.netbeans.api.java.platform.JavaPlatform;
55 import org.netbeans.api.java.platform.JavaPlatformManager;
56 import org.netbeans.api.project.SourceGroup;
57 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
58 import org.netbeans.spi.java.project.support.ui.PackageView;
59 import org.openide.util.actions.SystemAction;
60 import org.openide.util.lookup.Lookups;
61 import org.openide.xml.XMLUtil;
62
63
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 class PlatformNode extends AbstractNode implements ChangeListener JavaDoc {
74
75     private static final String JavaDoc PLATFORM_ICON = "org/netbeans/modules/java/j2seproject/ui/resources/platform"; //NOI18N
76
private static final String JavaDoc ARCHIVE_ICON = "org/netbeans/modules/java/j2seproject/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         setIconBase(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         }
97         else {
98             String JavaDoc platformId = pp.getPlatformId ();
99             if (platformId == null) {
100                 name = NbBundle.getMessage(PlatformNode.class,"TXT_BrokenPlatform");
101             }
102             else {
103                 name = MessageFormat.format(NbBundle.getMessage(PlatformNode.class,"FMT_BrokenPlatform"), new Object JavaDoc[] {platformId});
104             }
105         }
106         return name;
107     }
108     
109     public String JavaDoc getHtmlDisplayName () {
110         if (pp.getPlatform() == null) {
111             String JavaDoc displayName = this.getDisplayName();
112             try {
113                 displayName = XMLUtil.toElementContent(displayName);
114             } catch (CharConversionException JavaDoc ex) {
115                 // OK, no annotation in this case
116
return null;
117             }
118             return "<font color=\"#A40000\">" + displayName + "</font>"; //NOI18N
119
}
120         else {
121             return null;
122         }
123     }
124
125     public boolean canCopy() {
126         return false;
127     }
128     
129     public Action JavaDoc[] getActions(boolean context) {
130         return new Action JavaDoc[] {
131             SystemAction.get (ShowJavadocAction.class)
132         };
133     }
134
135     public void stateChanged(ChangeEvent JavaDoc e) {
136         this.fireNameChange(null,null);
137         this.fireDisplayNameChange(null,null);
138         //The caller holds ProjectManager.mutex() read lock
139
LibrariesNode.rp.post (new Runnable JavaDoc () {
140             public void run () {
141                 ((PlatformContentChildren)getChildren()).addNotify ();
142             }
143         });
144     }
145     
146     /**
147      * Creates new PlatformNode
148      * @param eval the PropertyEvaluator used for obtaining the active platform name
149      * and listening on the active platform change
150      * @param platformPropName the name of ant property holding the platform name
151      *
152      */

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