1 19 20 package org.netbeans.core.startup.layers; 21 22 import java.beans.*; 23 import java.io.*; 24 import java.net.URL ; 25 import java.util.*; 26 27 import org.openide.filesystems.*; 28 import org.openide.util.Enumerations; 29 import org.openide.util.Lookup; 30 import org.openide.util.Utilities; 31 32 36 public class FixedFileSystem extends AbstractFileSystem implements 37 AbstractFileSystem.List, AbstractFileSystem.Change, AbstractFileSystem.Attr, AbstractFileSystem.Info { 38 40 42 static FixedFileSystem deflt = null; 43 46 public static FixedFileSystem getDefault () { 47 return deflt; 48 } 49 50 51 public static class Instance implements Serializable { 52 55 final boolean folder; 56 58 final String contentType; 59 61 final byte[] contents; 62 64 final String displayName; 65 67 final URL icon; 68 70 final String beanName; 71 72 Map<String , Object > attributes; 80 public Instance (boolean folder, String contentType, byte[] contents, String displayName, URL icon) { 81 this.folder = folder; 82 this.contentType = contentType; 83 this.contents = contents; 84 this.displayName = displayName; 85 this.icon = icon; 86 beanName = null; 87 } 88 89 96 public Instance (boolean folder, String contentType, byte[] contents, String displayName, String beanName) { 97 this.folder = folder; 98 this.contentType = contentType; 99 this.contents = contents; 100 this.displayName = displayName; 101 this.beanName = beanName; 102 icon = null; 103 } 104 105 public void writeAttribute(String name, Object value) { 106 if (attributes == null) { 107 attributes = new HashMap<String , Object >(); 108 } 109 attributes.put(name, value); 110 } 111 } 112 113 115 public interface AttributeCreator { 116 public Object createValue(FixedFileSystem thisFS, 117 String thisFilePath, String thisAttr); 118 } 119 120 121 122 123 125 126 private final Map<String , Instance> instances = new HashMap<String , Instance> (); 127 128 129 131 133 135 137 private String displayName; 138 139 141 private transient long date = System.currentTimeMillis (); 142 143 @SuppressWarnings ("deprecation") 144 private void _setSystemName(String s) throws PropertyVetoException { 145 setSystemName(s); 146 } 147 148 152 public FixedFileSystem (String name, String displayName) { 153 list = this; 154 change = this; 155 info = this; 156 attr = this; 157 try { 158 _setSystemName (name); 159 } catch (PropertyVetoException pve) { 160 throw new InternalError (pve.toString ()); 161 } 162 this.displayName = displayName; 163 } 165 166 169 public boolean isNonTrivial () { 170 return ! instances.isEmpty (); 171 } 172 173 176 public String getDisplayName () { 177 return displayName; 178 } 179 180 183 public boolean isReadOnly () { 184 return true; 185 } 186 187 189 private static final Instance autoFolder = new Instance (true, null, null, null, (String ) null); 190 195 public void add (String path, Instance inst) { 196 if (path.length () == 0) return; 197 String folder; 199 int idx = path.lastIndexOf ('/'); 200 if (idx == -1) { 201 folder = ""; } else { 203 folder = path.substring (0, idx); 204 } 205 add (folder, autoFolder); 206 boolean nue; 207 synchronized (instances) { 208 Object old = instances.get (path); 209 if (inst == old) return; 210 nue = (old == null); 211 if (nue) instances.put (path, inst); 212 } 213 refreshResource (folder, false); 214 if (! nue) refreshResource (path, false); 215 date = System.currentTimeMillis (); 216 } 217 218 222 public void remove (String path) { 223 synchronized (instances) { 225 if (instances.remove (path) == null) { 226 return; 227 } 228 } 229 int idx = path.lastIndexOf ('/'); 231 if (idx != -1) { 232 refreshResource (path.substring (0, idx), false); 234 } else { 235 refreshResource ("", false); } 238 } 239 240 244 public FixedFileSystem.Instance get (String path) { 245 return instances.get(path); 246 } 247 248 252 public void addDefault (String folder, String file) { 253 Instance inst = instances.get(folder); 254 if (inst == null) { 255 inst = new Instance(true, null, null, null, (String ) null); 256 add(folder, inst); 257 } 258 String attrName = "OpenIDE-Folder-Order"; String attrValue = null; 260 Map attrs = inst.attributes; 261 if (attrs != null) { 262 attrValue = (String ) attrs.get(attrName); 263 } 264 265 if (attrValue == null) { 266 attrValue = file; 267 } else { 268 attrValue += '/' + file; 269 } 270 inst.writeAttribute(attrName, attrValue); 271 } 272 273 276 @SuppressWarnings ("deprecation") 277 public String toString () { 278 return "FixedFileSystem[" + getSystemName () + "]" + instances.keySet (); } 280 281 286 String annotateName (String resource) { 287 synchronized (instances) { 288 Instance inst = instances.get(resource); 289 return (inst == null) ? null : inst.displayName; 290 } 291 } 292 296 java.awt.Image annotateIcon (String resource) { 297 Instance inst; 298 synchronized (instances) { 299 inst = instances.get(resource); 300 } 301 302 if (inst == null) return null; 303 if (inst.icon != null) { 304 return java.awt.Toolkit.getDefaultToolkit ().getImage (inst.icon); 305 } 306 307 if (inst.beanName == null) return null; 308 309 try { 310 Class <?> clazz = Lookup.getDefault().lookup(ClassLoader .class).loadClass(inst.beanName); 311 return Utilities.getBeanInfo(clazz).getIcon(BeanInfo.ICON_COLOR_16x16); 312 } catch (Exception ex) { 313 return null; 314 } 315 } 316 317 348 349 351 355 public String [] children (String f) { 356 synchronized (instances) { 357 ArrayList<String > l = new ArrayList<String > (10); 358 for (String path : instances.keySet()) { 359 if (f.length () == 0) { 360 if (path.lastIndexOf ('/') == -1) 362 l.add (path); 363 } else { 364 if (path.startsWith (f + '/') && path.lastIndexOf ('/') == f.length ()) 366 l.add (path.substring (f.length () + 1)); 367 } 368 } 369 return l.toArray (new String [l.size ()]); 371 } 372 } 373 374 376 380 public void createFolder (String name) throws java.io.IOException { 381 throw new IOException ("unsupported"); } 383 384 388 public void createData (String name) throws IOException { 389 throw new IOException ("unsupported"); } 391 392 397 public void rename (String oldName, String newName) throws IOException { 398 throw new IOException ("unsupported"); } 400 401 405 public void delete (String name) throws IOException { 406 throw new IOException ("unsupported"); } 408 409 411 416 public Object readAttribute (String name, String attrName) { 417 if ("OpenIDE-Folder-SortMode".equals (attrName)) { return "O"; } 420 421 430 431 Instance inst = instances.get(name); 432 if (inst == null) { 433 return null; 434 } 435 Map m = inst.attributes; 436 if (m == null) { 437 return null; 438 } 439 440 Object ret = m.get (attrName); 441 if(ret instanceof AttributeCreator) { 442 return ((AttributeCreator)ret).createValue(this, name, attrName); 443 } 444 445 return ret; 446 447 } 449 450 456 public void writeAttribute (String name, String attrName, Object value) throws IOException { 457 throw new IOException ("unsupported"); } 459 460 464 public synchronized Enumeration<String > attributes (String name) { 465 Instance inst = instances.get(name); 466 if (inst == null) return Enumerations.<String >empty(); 467 Map<String , Object > m = inst.attributes; 468 if (m == null) return Enumerations.<String >empty(); 469 return Collections.enumeration(m.keySet()); 470 471 } 472 473 477 public void renameAttributes (String oldName, String newName) { 478 } 480 481 484 public void deleteAttributes (String name) { 485 } 487 488 490 495 public Date lastModified (String name) { 496 return new Date (date); 497 } 498 499 503 public boolean folder (String name) { 504 synchronized (instances) { 505 Instance inst = instances.get(name); 506 if (inst == null) return false; 507 return inst.folder; 508 } 509 } 510 511 515 public boolean readOnly (String name) { 516 return true; 517 } 518 519 523 public String mimeType (String name) { 524 synchronized (instances) { 525 Instance inst = instances.get(name); 526 return (inst == null) ? null : inst.contentType; 527 } 528 } 529 530 534 public long size (String name) { 535 synchronized (instances) { 536 Instance inst = instances.get(name); 537 if (inst == null) return 0L; 538 if (inst.folder) return 0L; 539 if (inst.contents == null) return 0L; 540 return inst.contents.length; 541 } 542 } 543 544 549 public InputStream inputStream (String name) throws FileNotFoundException { 550 Instance inst; 551 synchronized (instances) { 552 inst = instances.get(name); 553 } 554 if (inst == null) throw new FileNotFoundException (name); 555 if (inst.folder) throw new FileNotFoundException (name); 556 byte[] contents = inst.contents; 557 if (contents == null) contents = new byte[0]; 558 return new ByteArrayInputStream (contents); 559 } 560 561 566 public OutputStream outputStream (String name) throws IOException { 567 throw new IOException ("unsupported"); } 569 570 574 public void lock (String name) throws IOException { 575 throw new IOException ("unsupported"); } 577 578 581 public void unlock (String name) { 582 } 584 585 588 public void markUnimportant (String name) { 589 } 591 592 691 } 692 | Popular Tags |