KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > layers > LayerNode


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.layers;
21
22 import java.awt.Image JavaDoc;
23 import java.io.CharConversionException JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Set JavaDoc;
28 import org.netbeans.api.java.classpath.ClassPath;
29 import org.netbeans.api.project.FileOwnerQuery;
30 import org.netbeans.modules.apisupport.project.NbModuleProject;
31 import org.netbeans.modules.apisupport.project.spi.NbModuleProvider;
32 import org.netbeans.modules.apisupport.project.Util;
33 import org.netbeans.modules.apisupport.project.suite.SuiteProject;
34 import org.netbeans.modules.apisupport.project.ui.customizer.SuiteUtils;
35 import org.openide.ErrorManager;
36 import org.openide.filesystems.FileAttributeEvent;
37 import org.openide.filesystems.FileChangeListener;
38 import org.openide.filesystems.FileEvent;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.FileRenameEvent;
41 import org.openide.filesystems.FileStatusEvent;
42 import org.openide.filesystems.FileStatusListener;
43 import org.openide.filesystems.FileSystem;
44 import org.openide.filesystems.FileUtil;
45 import org.openide.filesystems.MultiFileSystem;
46 import org.openide.loaders.DataObject;
47 import org.openide.loaders.DataObjectNotFoundException;
48 import org.openide.nodes.AbstractNode;
49 import org.openide.nodes.Children;
50 import org.openide.nodes.FilterNode;
51 import org.openide.nodes.Node;
52 import org.openide.util.NbBundle;
53 import org.openide.util.RequestProcessor;
54 import org.openide.util.actions.SystemAction;
55 import org.openide.xml.XMLUtil;
56
57 /**
58  * Displays two views of a layer.
59  * @author Jesse Glick
60  */

61 public final class LayerNode extends FilterNode {
62     
63     public LayerNode(LayerUtils.LayerHandle handle) {
64         super(getDataNode(handle), new LayerChildren(handle));
65     }
66     
67     private static Node getDataNode(LayerUtils.LayerHandle handle) {
68         FileObject layer = handle.getLayerFile();
69         try {
70             return DataObject.find(layer).getNodeDelegate();
71         } catch (DataObjectNotFoundException e) {
72             assert false : e;
73             return Node.EMPTY;
74         }
75     }
76     
77     private static final class LayerChildren extends Children.Keys {
78         
79         private static final String JavaDoc KEY_WAIT = "wait"; // NOI18N
80
private static final Object JavaDoc KEY_RAW = "raw"; // NOI18N
81
private static final Object JavaDoc KEY_CONTEXTUALIZED = "contextualized"; // NOI18N
82

83         private final LayerUtils.LayerHandle handle;
84         private ClassPath cp;
85         private NbModuleProject p;
86         private FileSystem layerfs;
87         private FileSystem sfs;
88         
89         public LayerChildren(LayerUtils.LayerHandle handle) {
90             this.handle = handle;
91         }
92         
93         protected void addNotify() {
94             super.addNotify();
95             handle.setAutosave(true);
96             setKeys(new Object JavaDoc[] {KEY_WAIT});
97             RequestProcessor.getDefault().post(new Runnable JavaDoc() {
98                 public void run() {
99                     try {
100                         FileObject layer = handle.getLayerFile();
101                         p = (NbModuleProject) FileOwnerQuery.getOwner(layer);
102                         assert p != null : layer;
103                         try {
104                             cp = createClasspath(p);
105                         } catch (IOException JavaDoc e) {
106                             Util.err.notify(ErrorManager.INFORMATIONAL, e);
107                         }
108                         layerfs = handle.layer(false);
109                         setKeys(new Object JavaDoc[] {KEY_RAW, KEY_WAIT});
110                         FileSystem _sfs = LayerUtils.getEffectiveSystemFilesystem(p);
111                         if (cp != null) { // has not been removeNotify()d yet
112
sfs = _sfs;
113                             setKeys(new Object JavaDoc[] {KEY_RAW, KEY_CONTEXTUALIZED});
114                         }
115                     } catch (IOException JavaDoc e) {
116                         Util.err.notify(ErrorManager.INFORMATIONAL, e);
117                     }
118                 }
119             });
120         }
121         
122         protected void removeNotify() {
123             setKeys(Collections.EMPTY_SET);
124             cp = null;
125             p = null;
126             layerfs = null;
127             sfs = null;
128             super.removeNotify();
129         }
130         
131         protected Node[] createNodes(Object JavaDoc key) {
132             try {
133                 if (key == KEY_RAW) {
134                     FileSystem fs = badge(layerfs, cp, handle.getLayerFile(), NbBundle.getMessage(LayerNode.class, "LBL_this_layer"), null);
135                     return new Node[] {DataObject.find(fs.getRoot()).getNodeDelegate()};
136                 } else if (key == KEY_CONTEXTUALIZED) {
137                     FileSystem fs = badge(sfs, cp, handle.getLayerFile(), NbBundle.getMessage(LayerNode.class, "LBL_this_layer_in_context"), handle.layer(false));
138                     return new Node[] {DataObject.find(fs.getRoot()).getNodeDelegate()};
139                 } else if (key == KEY_WAIT) {
140                     return new Node[] {new AbstractNode(Children.LEAF) {
141                         public String JavaDoc getName() {
142                             return KEY_WAIT;
143                         }
144                         public String JavaDoc getDisplayName() {
145                             return NbBundle.getMessage(LayerNode.class, "LayerNode_please_wait");
146                         }
147                     }};
148                 } else {
149                     throw new AssertionError JavaDoc(key);
150                 }
151             } catch (IOException JavaDoc e) {
152                 Util.err.notify(ErrorManager.INFORMATIONAL, e);
153                 return new Node[0];
154             }
155         }
156         
157     }
158     
159     /**
160      * Add badging support to the plain layer.
161      */

162     private static FileSystem badge(final FileSystem base, final ClassPath cp, final FileObject layer, final String JavaDoc rootLabel, final FileSystem highlighted) {
163         class BadgingMergedFileSystem extends MultiFileSystem {
164             private final BadgingSupport status;
165             public BadgingMergedFileSystem() {
166                 super(new FileSystem[] {base});
167                 status = new BadgingSupport(this);
168                 status.addFileStatusListener(new FileStatusListener() {
169                     public void annotationChanged(FileStatusEvent ev) {
170                         fireFileStatusChanged(ev);
171                     }
172                 });
173                 status.setClasspath(cp);
174                 // XXX loc/branding suffix?
175
addFileChangeListener(new FileChangeListener() { // #65564
176
private void fire() {
177                         fireFileStatusChanged(new FileStatusEvent(BadgingMergedFileSystem.this, true, true));
178                     }
179                     public void fileAttributeChanged(FileAttributeEvent fe) {
180                         fire();
181                     }
182                     public void fileChanged(FileEvent fe) {
183                         fire();
184                     }
185                     public void fileDataCreated(FileEvent fe) {
186                         fire();
187                     }
188                     public void fileDeleted(FileEvent fe) {
189                         fire();
190                     }
191                     public void fileFolderCreated(FileEvent fe) {
192                         fire();
193                     }
194                     public void fileRenamed(FileRenameEvent fe) {
195                         fire();
196                     }
197                 });
198             }
199             public FileSystem.Status getStatus() {
200                 return new FileSystem.HtmlStatus() {
201                     public String JavaDoc annotateNameHtml(String JavaDoc name, Set JavaDoc files) {
202                         String JavaDoc nonHtmlLabel = status.annotateName(name, files);
203                         if (files.size() == 1 && ((FileObject) files.iterator().next()).isRoot()) {
204                             nonHtmlLabel = rootLabel;
205                         }
206                         String JavaDoc htmlLabel;
207                         try {
208                             htmlLabel = XMLUtil.toElementContent(nonHtmlLabel);
209                         } catch (CharConversionException JavaDoc e) {
210                             Util.err.notify(ErrorManager.INFORMATIONAL, e);
211                             htmlLabel = nonHtmlLabel;
212                         }
213                         if (highlighted != null) {
214                             // Boldface resources which do come from this project.
215
boolean local = false;
216                             Iterator JavaDoc it = files.iterator();
217                             while (it.hasNext()) {
218                                 FileObject f = (FileObject) it.next();
219                                 if (!f.isRoot() && highlighted.findResource(f.getPath()) != null) {
220                                     local = true;
221                                     break;
222                                 }
223                             }
224                             if (local) {
225                                 htmlLabel = "<b>" + htmlLabel + "</b>"; // NOI18N
226
}
227                         }
228                         return htmlLabel;
229                     }
230                     public String JavaDoc annotateName(String JavaDoc name, Set JavaDoc files) {
231                         // Complex to explain why this is even called, but it is.
232
// Weird b/c hacks in the way DataNode.getHtmlDisplayName works.
233
return name;
234                     }
235                     public Image JavaDoc annotateIcon(Image JavaDoc icon, int iconType, Set JavaDoc files) {
236                         return status.annotateIcon(icon, iconType, files);
237                     }
238                 };
239             }
240             public String JavaDoc getDisplayName() {
241                 return FileUtil.getFileDisplayName(layer);
242             }
243             public SystemAction[] getActions(Set JavaDoc<FileObject> foSet) {
244                 return new SystemAction[] {
245                     SystemAction.get(PickNameAction.class),
246                     SystemAction.get(PickIconAction.class),
247                 };
248             }
249         }
250         return new BadgingMergedFileSystem();
251         /* XXX loc/branding suffix possibilities:
252         Matcher m = Pattern.compile("(.*" + "/)?[^_/.]+(_[^/.]+)?(\\.[^/]+)?").matcher(u);
253         assert m.matches() : u;
254         suffix = m.group(2);
255         if (suffix == null) {
256             suffix = "";
257         }
258         status.setSuffix(suffix);
259          */

260     }
261     
262     public String JavaDoc getDisplayName() {
263         return NbBundle.getMessage(LayerNode.class, "LayerNode_label");
264     }
265     
266     /**
267      * Make a runtime classpath indicative of what is accessible from a sample resource.
268      */

269     private static ClassPath createClasspath(NbModuleProject p) throws IOException JavaDoc {
270         NbModuleProvider.NbModuleType type = Util.getModuleType(p);
271         if (type == NbModuleProvider.STANDALONE) {
272             return LayerUtils.createLayerClasspath(Collections.singleton(p), LayerUtils.getPlatformJarsForStandaloneProject(p));
273         } else if (type == NbModuleProvider.SUITE_COMPONENT) {
274             SuiteProject suite = SuiteUtils.findSuite(p);
275             if (suite == null) {
276                 throw new IOException JavaDoc("Could not load suite for " + p); // NOI18N
277
}
278             Set JavaDoc<NbModuleProject> modules = SuiteUtils.getSubProjects(suite);
279             return LayerUtils.createLayerClasspath(modules, LayerUtils.getPlatformJarsForSuiteComponentProject(p, suite));
280         } else if (type == NbModuleProvider.NETBEANS_ORG) {
281             return LayerUtils.createLayerClasspath(LayerUtils.getProjectsForNetBeansOrgProject(p), Collections.EMPTY_SET);
282         } else {
283             throw new AssertionError JavaDoc(type);
284         }
285     }
286     
287 }
288
Popular Tags