1 19 20 package org.netbeans.modules.apisupport.project.layers; 21 22 import java.awt.Image ; 23 import java.io.CharConversionException ; 24 import java.io.IOException ; 25 import java.util.Collections ; 26 import java.util.Iterator ; 27 import java.util.Set ; 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 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 KEY_WAIT = "wait"; private static final Object KEY_RAW = "raw"; private static final Object KEY_CONTEXTUALIZED = "contextualized"; 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 [] {KEY_WAIT}); 97 RequestProcessor.getDefault().post(new Runnable () { 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 e) { 106 Util.err.notify(ErrorManager.INFORMATIONAL, e); 107 } 108 layerfs = handle.layer(false); 109 setKeys(new Object [] {KEY_RAW, KEY_WAIT}); 110 FileSystem _sfs = LayerUtils.getEffectiveSystemFilesystem(p); 111 if (cp != null) { sfs = _sfs; 113 setKeys(new Object [] {KEY_RAW, KEY_CONTEXTUALIZED}); 114 } 115 } catch (IOException 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 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 getName() { 142 return KEY_WAIT; 143 } 144 public String getDisplayName() { 145 return NbBundle.getMessage(LayerNode.class, "LayerNode_please_wait"); 146 } 147 }}; 148 } else { 149 throw new AssertionError (key); 150 } 151 } catch (IOException e) { 152 Util.err.notify(ErrorManager.INFORMATIONAL, e); 153 return new Node[0]; 154 } 155 } 156 157 } 158 159 162 private static FileSystem badge(final FileSystem base, final ClassPath cp, final FileObject layer, final String 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 addFileChangeListener(new FileChangeListener() { 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 annotateNameHtml(String name, Set files) { 202 String nonHtmlLabel = status.annotateName(name, files); 203 if (files.size() == 1 && ((FileObject) files.iterator().next()).isRoot()) { 204 nonHtmlLabel = rootLabel; 205 } 206 String htmlLabel; 207 try { 208 htmlLabel = XMLUtil.toElementContent(nonHtmlLabel); 209 } catch (CharConversionException e) { 210 Util.err.notify(ErrorManager.INFORMATIONAL, e); 211 htmlLabel = nonHtmlLabel; 212 } 213 if (highlighted != null) { 214 boolean local = false; 216 Iterator 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>"; } 227 } 228 return htmlLabel; 229 } 230 public String annotateName(String name, Set files) { 231 return name; 234 } 235 public Image annotateIcon(Image icon, int iconType, Set files) { 236 return status.annotateIcon(icon, iconType, files); 237 } 238 }; 239 } 240 public String getDisplayName() { 241 return FileUtil.getFileDisplayName(layer); 242 } 243 public SystemAction[] getActions(Set <FileObject> foSet) { 244 return new SystemAction[] { 245 SystemAction.get(PickNameAction.class), 246 SystemAction.get(PickIconAction.class), 247 }; 248 } 249 } 250 return new BadgingMergedFileSystem(); 251 260 } 261 262 public String getDisplayName() { 263 return NbBundle.getMessage(LayerNode.class, "LayerNode_label"); 264 } 265 266 269 private static ClassPath createClasspath(NbModuleProject p) throws IOException { 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 ("Could not load suite for " + p); } 278 Set <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 (type); 284 } 285 } 286 287 } 288 | Popular Tags |