KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > 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.web.project.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.openide.filesystems.FileObject;
43 import org.openide.filesystems.FileStateInvalidException;
44 import org.openide.filesystems.FileUtil;
45 import org.openide.nodes.Children;
46 import org.openide.nodes.AbstractNode;
47 import org.openide.nodes.Node;
48 import org.openide.util.NbBundle;
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.spi.project.support.ant.PropertyEvaluator;
56 import org.netbeans.spi.java.project.support.ui.PackageView;
57 import org.openide.util.actions.SystemAction;
58 import org.openide.util.lookup.Lookups;
59 import org.openide.xml.XMLUtil;
60
61
62
63 /**
64  * PlatformNode represents Java platform in the logical view.
65  * Listens on the {@link PropertyEvaluator} for change of
66  * the ant property holding the platform name.
67  * It displays the content of boot classpath.
68  * @see JavaPlatform
69  * @author Tomas Zezula
70  */

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

76     private final PlatformProvider pp;
77
78     private PlatformNode(PlatformProvider pp) {
79         super (new PlatformContentChildren (), Lookups.singleton (new JavadocProvider(pp)));
80         this.pp = pp;
81         this.pp.addChangeListener(this);
82         setIconBase(PLATFORM_ICON);
83     }
84
85     public String JavaDoc getName () {
86         return this.getDisplayName();
87     }
88
89     public String JavaDoc getDisplayName () {
90         JavaPlatform plat = pp.getPlatform();
91         String JavaDoc name;
92         if (plat != null) {
93             name = plat.getDisplayName();
94         }
95         else {
96             String JavaDoc platformId = pp.getPlatformId ();
97             if (platformId == null) {
98                 name = NbBundle.getMessage(PlatformNode.class,"TXT_BrokenPlatform");
99             }
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
}
118         else {
119             return null;
120         }
121     }
122
123     public boolean canCopy() {
124         return false;
125     }
126     
127     public Action JavaDoc[] getActions(boolean context) {
128         return new Action JavaDoc[] {
129             SystemAction.get (ShowJavadocAction.class)
130         };
131     }
132
133     public void stateChanged(ChangeEvent JavaDoc e) {
134         this.fireNameChange(null,null);
135         this.fireDisplayNameChange(null,null);
136         //The caller holds ProjectManager.mutex() read lock
137
LibrariesNode.rp.post (new Runnable JavaDoc () {
138             public void run () {
139                 ((PlatformContentChildren)getChildren()).addNotify ();
140             }
141         });
142     }
143     
144     /**
145      * Creates new PlatformNode
146      * @param eval the PropertyEvaluator used for obtaining the active platform name
147      * and listening on the active platform change
148      * @param platformPropName the name of ant property holding the platform name
149      *
150      */

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