KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > ui > logicalview > libraries > 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.j2ee.ejbjarproject.ui.logicalview.libraries;
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.filesystems.URLMapper;
46 import org.openide.nodes.Children;
47 import org.openide.nodes.AbstractNode;
48 import org.openide.nodes.Node;
49 import org.openide.util.NbBundle;
50 import org.openide.util.Utilities;
51 import org.openide.util.WeakListeners;
52 import org.openide.ErrorManager;
53 import org.netbeans.api.java.platform.JavaPlatform;
54 import org.netbeans.api.java.platform.JavaPlatformManager;
55 import org.netbeans.api.project.SourceGroup;
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
63
64 /**
65  * PlatformNode represents Java platform in the logical view.
66  * Listens on the {@link PropertyEvaluator} for change of
67  * the ant property holding the platform name.
68  * It displays the content of boot classpath.
69  * @see JavaPlatform
70  * @author Tomas Zezula
71  */

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

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

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