1 19 20 package org.netbeans.core; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.File ; 25 import java.net.MalformedURLException ; 26 import java.net.URL ; 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.Enumeration ; 30 import java.util.Iterator ; 31 import java.util.LinkedHashSet ; 32 import java.util.Set ; 33 import java.util.logging.Level ; 34 import java.util.logging.Logger ; 35 import javax.swing.Action ; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileStateInvalidException; 38 import org.openide.filesystems.FileSystem; 39 import org.openide.filesystems.Repository; 40 import org.openide.filesystems.RepositoryEvent; 41 import org.openide.filesystems.RepositoryListener; 42 import org.openide.filesystems.RepositoryReorderedEvent; 43 import org.openide.filesystems.URLMapper; 44 import org.openide.loaders.DataFilter; 45 import org.openide.loaders.DataFolder; 46 import org.openide.loaders.DataObject; 47 import org.openide.loaders.DataObjectNotFoundException; 48 import org.openide.loaders.InstanceSupport; 49 import org.openide.loaders.RepositoryNodeFactory; 50 import org.openide.nodes.AbstractNode; 51 import org.openide.nodes.Children; 52 import org.openide.nodes.FilterNode; 53 import org.openide.nodes.Node; 54 import org.openide.util.HelpCtx; 55 import org.openide.util.Lookup; 56 import org.openide.util.NbBundle; 57 import org.openide.util.WeakListeners; 58 import org.openide.util.actions.SystemAction; 59 60 65 public final class DataSystem extends AbstractNode 66 implements RepositoryListener { 67 68 private static DataSystem def; 69 70 71 private transient Repository fileSystemPool; 72 73 74 DataFilter filter; 75 76 80 private DataSystem(Children ch, Repository fsp, DataFilter filter) { 81 super (ch); 82 fileSystemPool = fsp; 83 this.filter = filter; 84 initialize(); 85 setIconBaseWithExtension ("org/netbeans/core/resources/repository.gif"); setName (NbBundle.getBundle (DataSystem.class).getString ("dataSystemName")); 87 setShortDescription (NbBundle.getBundle (DataSystem.class).getString ("CTL_Repository_Hint")); 88 getCookieSet ().add (new InstanceSupport.Instance (fsp)); 89 } 90 91 94 private DataSystem(Children ch, DataFilter filter) { 95 this (ch, Repository.getDefault(), filter); 96 } 97 98 public HelpCtx getHelpCtx () { 99 return new HelpCtx (DataSystem.class); 100 } 101 102 103 public static Node getDataSystem(DataFilter filter) { 104 if (filter == null) { 105 if (def != null) { 106 return def; 107 } 108 return def = new DataSystem(new DSMap (), DataFilter.ALL); 109 } else { 110 return new DataSystem(new DSMap (), filter); 111 } 112 } 113 114 115 public static Node getDataSystem() { 116 return getDataSystem(null); 117 } 118 119 void initialize () { 120 fileSystemPool.addRepositoryListener(WeakListeners.create(RepositoryListener.class, 121 this, 122 fileSystemPool)); 123 Enumeration en = fileSystemPool.getFileSystems (); 124 while (en.hasMoreElements ()) { 125 FileSystem fs = (FileSystem)en.nextElement (); 126 fs.addPropertyChangeListener (org.openide.util.WeakListeners.propertyChange ((DSMap)getChildren (), fs)); 127 } 128 refresh (); 129 } 130 131 133 public Node.Handle getHandle() { 134 return filter == DataFilter.ALL ? new DSHandle (null) : new DSHandle(filter); 135 } 136 137 138 public Action [] getActions(boolean context) { 139 return new Action [] { 140 SystemAction.get (org.openide.actions.FindAction.class), 141 SystemAction.get (org.openide.actions.ToolsAction.class), 145 }; 148 } 149 150 153 public void fileSystemAdded (RepositoryEvent ev) { 154 ev.getFileSystem ().addPropertyChangeListener ( 155 org.openide.util.WeakListeners.propertyChange ((DSMap)getChildren (), ev.getFileSystem ()) 156 ); 157 refresh (); 158 } 159 160 163 public void fileSystemRemoved (RepositoryEvent ev) { 164 refresh (); 165 } 166 167 public void fileSystemPoolReordered(RepositoryReorderedEvent ev) { 168 refresh (); 169 } 170 171 173 void refresh () { 174 refresh (null); 175 } 176 177 180 void refresh (FileSystem fs) { 181 URLMapper mapper = getMasterFsURLMapper(); 186 if (mapper == null) { 187 ((DSMap)getChildren ()).refresh (fileSystemPool, fs); 189 } else { 190 ((DSMap)getChildren ()).refreshListRoots(mapper); 191 } 192 } 193 194 private static URLMapper getMasterFsURLMapper() { 195 URLMapper retVal = null; 196 Lookup.Result result = Lookup.getDefault().lookupResult(URLMapper.class); 197 Collection c = result.allInstances(); 198 for (Iterator iterator = c.iterator(); iterator.hasNext();) { 199 URLMapper mapper = (URLMapper) iterator.next(); 200 if (mapper != null && "org.netbeans.modules.masterfs.MasterURLMapper".equals(mapper.getClass().getName())) { retVal = mapper; 202 break; 203 } 204 } 205 return retVal; 206 } 207 208 209 public boolean hasCustomizer() { 210 return true; 211 } 212 213 215 static class DSMap extends Children.Keys implements PropertyChangeListener { 216 217 public void propertyChange (PropertyChangeEvent ev) { 218 DataSystem ds = getDS (); 220 if (ds == null) return; 221 222 if ("root".equals(ev.getPropertyName())) { 223 FileSystem fs = (FileSystem)ev.getSource (); 224 ds.refresh (fs); 225 ds.refresh (); 226 } 227 } 228 229 230 private DataSystem getDS() { 231 return (DataSystem)getNode (); 232 } 233 234 protected Node[] createNodes (Object key) { 235 DataFolder df = (DataFolder)key; 236 Node n = new FilterNode(df.getNodeDelegate(), df.createNodeChildren (getDS ().filter)); 237 return new Node[] {n}; 238 } 239 240 244 public void refresh(Repository fileSystemPool, FileSystem fs) { 245 @SuppressWarnings ("unchecked") Enumeration <FileSystem> en = (Enumeration <FileSystem>)fileSystemPool.getFileSystems(); 246 ArrayList <DataFolder> list = new ArrayList <DataFolder>(); 247 while (en.hasMoreElements()) { 248 FileSystem fsystem = en.nextElement(); 249 DataObject root = null; 250 try { 251 root = DataObject.find(fsystem.getRoot()); 252 } 253 catch (DataObjectNotFoundException e) { 254 Logger.getLogger(DataSystem.class.getName()).log(Level.WARNING, null, e); 255 } 258 if ((root instanceof DataFolder) && getDS().filter.acceptDataObject(root)) { 259 list.add((DataFolder)root); 260 } 261 } 262 setKeys(list); 263 } 264 265 private void refreshListRoots(URLMapper mapper) { 266 File [] files = File.listRoots(); 267 Set <DataFolder> rootSet = new LinkedHashSet <DataFolder>(); 268 269 for (int i = 0; i < files.length; i++) { 270 File file = files[i]; 271 FileObject fo = fetchFileObject(file, mapper); 272 273 if (fo != null) { 274 try { 275 fo = fo.getFileSystem().getRoot(); 276 } 277 catch (FileStateInvalidException e) { 278 continue; 279 } 280 DataObject root = null; 281 282 try { 283 root = DataObject.find(fo); 284 } 285 catch (DataObjectNotFoundException e) { 286 Logger.getLogger(DataSystem.class.getName()).log(Level.WARNING, null, e); 287 } 288 if ((root instanceof DataFolder) && 289 getDS().filter.acceptDataObject(root)) { 290 rootSet.add((DataFolder) root); 291 } 292 } 293 } 294 setKeys(rootSet); 295 } 296 297 298 private FileObject fetchFileObject(File file, URLMapper mapper) { 299 302 FileObject retVal = null; 303 try { 304 FileObject[] all = mapper.getFileObjects(toUrl(file)); if (all != null && all.length > 0) { 306 retVal = all [0]; 307 } 308 } catch (MalformedURLException e) { 309 retVal = null; 310 } 311 return retVal; 312 } 313 314 private URL toUrl(File file) throws MalformedURLException { 315 return (org.openide.util.Utilities.isWindows()) ? new URL ("file:/"+file.getAbsolutePath ()) : file.toURI().toURL(); } 317 318 } 319 320 321 322 323 private static class DSHandle implements Node.Handle { 324 DataFilter filter; 325 326 static final long serialVersionUID =-2266375092419944364L; 327 public DSHandle(DataFilter f) { 328 filter = f; 329 } 330 331 public Node getNode() { 332 return getDataSystem (filter); 333 } 334 } 335 336 337 public static final class NbRepositoryNodeFactory extends RepositoryNodeFactory { 338 339 public Node repository(DataFilter f) { 340 return DataSystem.getDataSystem(f == DataFilter.ALL ? null : f); 341 } 342 343 } 344 345 } 346 | Popular Tags |