1 19 20 package org.netbeans.core.startup.layers; 21 22 import java.awt.Image ; 23 import java.awt.Toolkit ; 24 import java.beans.BeanInfo ; 25 import java.beans.PropertyVetoException ; 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.io.NotSerializableException ; 29 import java.io.ObjectStreamException ; 30 import java.io.Serializable ; 31 import java.net.URL ; 32 import java.util.HashSet ; 33 import java.util.Iterator ; 34 import java.util.MissingResourceException ; 35 import java.util.ResourceBundle ; 36 import java.util.Set ; 37 import java.util.logging.Level ; 38 import org.openide.filesystems.FileObject; 39 import org.openide.filesystems.FileStatusEvent; 40 import org.openide.filesystems.FileSystem; 41 import org.openide.filesystems.FileUtil; 42 import org.openide.filesystems.LocalFileSystem; 43 import org.openide.filesystems.MultiFileSystem; 44 import org.openide.filesystems.Repository; 45 import org.openide.util.NbBundle; 46 47 51 public final class SystemFileSystem extends MultiFileSystem implements FileSystem.Status { 52 54 55 static final long serialVersionUID = -7761052280240991668L; 56 57 58 private static final String SYSTEM_NAME = "SystemFileSystem"; 60 61 private static final String ATTR_BUNDLE = "SystemFileSystem.localizingBundle"; 63 64 private static final String ATTR_ICON_16 = "SystemFileSystem.icon"; 66 private static final String ATTR_ICON_32 = "SystemFileSystem.icon32"; 68 69 private ModuleLayeredFileSystem user; 70 71 private ModuleLayeredFileSystem home; 72 73 75 @SuppressWarnings ("deprecation") 76 private SystemFileSystem (FileSystem[] fss) throws PropertyVetoException { 77 super (fss); 78 user = (ModuleLayeredFileSystem) fss[0]; 79 home = fss.length > 2 ? (ModuleLayeredFileSystem) fss[1] : null; 80 81 setSystemName (SYSTEM_NAME); 82 setHidden (true); 83 } 84 85 86 87 public String getDisplayName () { 88 return NbBundle.getMessage(SystemFileSystem.class, "CTL_SystemFileSystem"); } 90 91 94 public ModuleLayeredFileSystem getInstallationLayer () { 95 return home; 96 } 97 98 100 public ModuleLayeredFileSystem getUserLayer () { 101 return user; 102 } 103 104 108 public final void setLayers (FileSystem[] arr) throws IllegalArgumentException { 109 Set <FileSystem> s = new HashSet <FileSystem> (); 110 for (int i = 0; i < arr.length; i++) 111 if (s.contains (arr[i])) 112 throw new IllegalArgumentException ("Overlap in filesystem layers"); else 114 s.add (arr[i]); 115 116 setDelegates(arr.clone()); 118 firePropertyChange ("layers", null, null); } 120 121 126 public FileSystem[] getLayers() { 127 return getDelegates().clone(); 129 } 130 131 protected FileSystem createWritableOnForRename (String oldName, String newName) throws IOException { 132 return createWritableOn (oldName); 133 } 134 135 protected FileSystem createWritableOn (String name) throws IOException { 136 FileSystem[] fss = getDelegates (); 137 for (int index = 0; index < fss.length; index++) { 138 if (! fss[index].isReadOnly ()) 139 return fss[index]; 140 } 141 throw new IOException ("No writable filesystems in our delegates"); } 144 145 protected java.util.Set createLocksOn (String name) throws IOException { 146 LocalFileSystemEx.potentialLock (name); 147 return super.createLocksOn (name); 148 } 149 150 152 @Deprecated 153 public boolean isPersistent () { 154 return true; 155 } 156 157 public FileSystem.Status getStatus () { 158 return this; 159 } 160 161 163 public String annotateName (String s, Set set) { 164 165 Iterator it = set.iterator (); 169 while (it.hasNext ()) { 170 FileObject fo = (FileObject) it.next (); 172 173 String bundleName = (String )fo.getAttribute (ATTR_BUNDLE); if (bundleName != null) { 175 try { 176 bundleName = org.openide.util.Utilities.translate(bundleName); 177 ResourceBundle b = NbBundle.getBundle(bundleName); 178 try { 179 return b.getString (fo.getPath()); 180 } catch (MissingResourceException ex) { 181 } 183 } catch (MissingResourceException ex) { 184 ModuleLayeredFileSystem.err.log( 185 Level.WARNING, 186 "Computing display name for " + fo, ex); } 189 } 190 191 String fixedName = FixedFileSystem.deflt.annotateName(fo.getPath()); 192 if (fixedName != null) return fixedName; 193 } 194 195 return s; 196 } 197 198 200 public Image annotateIcon (Image im, int type, Set s) { 201 String attr; 202 if (type == BeanInfo.ICON_COLOR_16x16) { 203 attr = ATTR_ICON_16; 204 } else if (type == BeanInfo.ICON_COLOR_32x32) { 205 attr = ATTR_ICON_32; 206 } else { 207 return im; 209 } 210 Iterator it = s.iterator (); 211 while (it.hasNext ()) { 212 FileObject fo = (FileObject) it.next (); 213 Object value = fo.getAttribute (attr); 214 if (value != null) { 215 if (value instanceof URL ) { 216 return Toolkit.getDefaultToolkit ().getImage ((URL ) value); 217 } else if (value instanceof Image ) { 218 return (Image )value; 220 } else { 221 ModuleLayeredFileSystem.err.warning("Attribute " + attr + " on " + fo + " expected to be a URL or Image; was: " + value); 222 } 223 } 224 Image anntIm = FixedFileSystem.deflt.annotateIcon(fo.getPath()); 225 if (anntIm != null) { 226 return anntIm; 227 } 228 } 229 return im; 230 } 231 232 241 static SystemFileSystem create (File userDir, File homeDir, File [] extradirs) 242 throws java.beans.PropertyVetoException , IOException { 243 FileSystem user; 244 LocalFileSystem home; 245 246 if (userDir != null) { 247 if (!userDir.exists ()) { 249 userDir.mkdirs (); 250 } 251 LocalFileSystem l = new LocalFileSystemEx ( true ); 252 l.setRootDirectory (userDir); 253 user = l; 254 } else { 255 user = FileUtil.createMemoryFileSystem (); 257 } 258 259 if (homeDir == null) { 260 home = null; 261 } else { 262 home = new LocalFileSystemEx (); 263 home.setRootDirectory (homeDir); 264 home.setReadOnly (true); 265 } 266 LocalFileSystem[] extras = new LocalFileSystem[extradirs.length]; 267 for (int i = 0; i < extradirs.length; i++) { 268 extras[i] = new LocalFileSystemEx(); 269 extras[i].setRootDirectory(extradirs[i]); 270 extras[i].setReadOnly(true); 271 } 272 273 FileSystem[] arr = new FileSystem[home == null ? 2 : 3]; 274 arr[0] = new ModuleLayeredFileSystem(user, new FileSystem[0], null); 275 if (home != null) { 276 File cachedir = new File (new File (userDir.getParentFile(), "var"), "cache"); arr[1] = new ModuleLayeredFileSystem(home, extras, cachedir); 278 } 279 FixedFileSystem.deflt = new FixedFileSystem 280 ("org.netbeans.core.projects.FixedFileSystem", "Automatic Manifest Installation"); arr[home == null ? 1 : 2] = FixedFileSystem.deflt; 282 283 return new SystemFileSystem (arr); 284 } 285 286 295 protected void notifyMigration (FileObject fo) { 296 fireFileStatusChanged (new FileStatusEvent (this, fo, false, true)); 297 } 298 299 private Object writeReplace () throws ObjectStreamException { 301 new NotSerializableException ("WARNING - SystemFileSystem is not designed to be serialized").printStackTrace (); return new SingletonSerializer (); 303 } 304 305 private static final class SingletonSerializer extends Object implements Serializable { 306 private static final long serialVersionUID = 6436781994611L; 307 SingletonSerializer() {} 308 private Object readResolve () throws ObjectStreamException { 309 return Repository.getDefault().getDefaultFileSystem (); 310 } 311 } 312 314 } 315 | Popular Tags |