1 19 20 package org.openide.loaders; 21 22 23 import java.io.*; 24 import java.util.*; 25 import java.util.logging.*; 26 import javax.swing.Action ; 27 import org.openide.filesystems.*; 28 import org.openide.nodes.NodeOp; 29 import org.openide.util.*; 30 import org.openide.util.actions.SystemAction; 31 import org.openide.util.io.SafeException; 32 33 43 public abstract class DataLoader extends SharedClassObject { 44 45 static final Logger ERR = Logger.getLogger("org.openide.loaders.DataLoader"); 47 private static final long serialVersionUID = 1986614061378346169L; 50 51 52 public static final String PROP_DISPLAY_NAME = "displayName"; 54 public static final String PROP_ACTIONS = "actions"; 56 private static final String PROP_DEF_ACTIONS = "defaultActions"; 58 private static final Object ACTION_MANAGER = new Object (); 59 60 private static final Object PROP_REPRESENTATION_CLASS = new Object (); 61 62 private static final Object PROP_REPRESENTATION_CLASS_NAME = new Object (); 63 64 private static final int LOADER_VERSION = 1; 65 66 77 @Deprecated 78 protected DataLoader(Class <? extends DataObject> representationClass) { 79 putProperty (PROP_REPRESENTATION_CLASS, representationClass); 80 putProperty (PROP_REPRESENTATION_CLASS_NAME, representationClass.getName()); 81 if (representationClass.getClassLoader() == getClass().getClassLoader()) { 82 ERR.warning("Use of super(" + representationClass.getName() + ".class) in " + getClass().getName() + "() should be replaced with super(\"" + representationClass.getName() + "\") to reduce unnecessary class loading"); 83 } 84 } 85 86 100 protected DataLoader( String representationClassName ) { 101 putProperty (PROP_REPRESENTATION_CLASS_NAME, representationClassName); 102 } 103 104 108 public final Class <? extends DataObject> getRepresentationClass() { 109 Class <?> _cls = (Class <?>) getProperty(PROP_REPRESENTATION_CLASS); 110 if (_cls != null) { 111 return _cls.asSubclass(DataObject.class); 112 } 113 114 Class <? extends DataObject> cls; 115 String clsName = (String )getProperty (PROP_REPRESENTATION_CLASS_NAME); 116 try { 117 cls = Class.forName(clsName, false, getClass().getClassLoader()).asSubclass(DataObject.class); 118 } catch (ClassNotFoundException cnfe) { 119 throw new IllegalStateException (cnfe.toString ()); 120 } 121 122 putProperty (PROP_REPRESENTATION_CLASS, cls); 123 return cls; 124 } 125 126 133 public final String getRepresentationClassName() { 134 return (String )getProperty (PROP_REPRESENTATION_CLASS_NAME); 135 } 136 137 146 public final SystemAction[] getActions () { 147 Action [] arr = getSwingActions (); 148 149 List<SystemAction> list = new ArrayList<SystemAction>(); 150 for (int i = 0; i < arr.length; i++) { 151 if (arr[i] instanceof SystemAction || arr[i] == null) { 152 list.add((SystemAction) arr[i]); 153 } 154 } 155 156 return list.toArray(new SystemAction[list.size()]); 157 } 158 159 160 final Action [] getSwingActions () { 161 DataLdrActions mgr = findManager (); 162 if (mgr != null) { 163 Action [] actions; 164 try { 165 actions = (Action []) mgr.instanceCreate(); 166 } catch (IOException ex) { 167 Exceptions.printStackTrace(ex); 168 actions = null; 169 } catch (ClassNotFoundException ex) { 170 Exceptions.printStackTrace(ex); 171 actions = null; 172 } 173 if (actions == null) { 174 return new Action [0]; 175 } 176 177 return actions; 178 } else { 179 SystemAction[] actions = (SystemAction[])getProperty (PROP_ACTIONS); 181 if ( actions == null ) { 182 actions = (SystemAction[])getProperty (PROP_DEF_ACTIONS); 183 if ( actions == null ) { 184 actions = defaultActions(); 185 putProperty (PROP_DEF_ACTIONS, actions, false); 186 } 187 } 188 return actions; 189 } 190 } 191 192 193 210 protected String actionsContext () { 211 return null; 212 } 213 214 220 protected SystemAction[] defaultActions () { 221 SystemAction[] actions = NodeOp.getDefaultActions(); 222 return actions; 223 } 224 225 227 private final DataLdrActions findManager () { 228 Object manager = getProperty (ACTION_MANAGER); 229 if (manager instanceof Class ) { 230 return null; 231 } 232 DataLdrActions mgr = (DataLdrActions)manager; 233 boolean newlyCreated = false; 234 if (mgr == null) { 235 String context = actionsContext (); 236 if (context == null) { 237 putProperty (ACTION_MANAGER, getClass ()); 239 return null; 240 } 241 242 FileObject fo = Repository.getDefault ().getDefaultFileSystem ().findResource (context); 243 if (fo == null) { 244 fo = Repository.getDefault ().getDefaultFileSystem ().getRoot (); 245 try { 246 fo = FileUtil.createFolder (fo, context); 247 248 } catch (IOException ex) { 249 ERR.log(Level.WARNING, null, ex); 250 } 251 newlyCreated = true; 252 } 253 254 mgr = new DataLdrActions (DataFolder.findFolder (fo), this); 255 if (newlyCreated) { 256 SystemAction[] arr = defaultActions (); 257 if (arr != null) { 258 mgr.setActions (arr); 259 } 260 } 261 putProperty (ACTION_MANAGER, mgr); 262 } 263 return mgr; 264 } 265 266 269 final void waitForActions () { 270 DataLdrActions mgr = findManager (); 271 if (mgr != null) { 272 mgr.waitFinished (); 273 } 274 } 275 276 287 public final void setActions (SystemAction[] actions) { 288 DataLdrActions mgr = findManager (); 289 if (mgr != null) { 290 mgr.setActions (actions); 291 } else { 292 putProperty (PROP_ACTIONS, actions, true); 293 } 294 } 295 296 299 final void setSwingActions (List arr) { 300 firePropertyChange (PROP_ACTIONS, null, null); 301 } 302 303 306 public final String getDisplayName () { 307 String dn = (String ) getProperty (PROP_DISPLAY_NAME); 308 if (dn != null) { 309 return dn; 310 } else { 311 dn = defaultDisplayName(); 312 if (dn != null) { 313 return dn; 314 } else { 315 return getRepresentationClassName(); 316 } 317 } 318 } 319 320 323 protected final void setDisplayName (final String displayName) { 324 putProperty (PROP_DISPLAY_NAME, displayName, true); 325 } 326 327 330 protected String defaultDisplayName () { 331 return NbBundle.getBundle(DataLoader.class).getString ("LBL_loader_display_name"); 332 } 333 334 354 public final DataObject findDataObject ( 355 FileObject fo, RecognizedFiles recognized 356 ) throws IOException { 357 try { 358 return DataObjectPool.handleFindDataObject( this, fo, recognized ); 359 } catch (IOException ioe) { 360 throw ioe; 361 } catch (ThreadDeath td) { 362 throw td; 363 } catch (RuntimeException e) { 364 if (e.getClass().getName().startsWith("org.openide.util.lookup")) { throw e; 368 } 369 IOException ioe = new IOException (e.toString()); 374 Logger.getLogger(DataLoader.class.getName()).log(Level.WARNING, null, e); 375 ioe.initCause(e); 376 throw ioe; 377 } 378 379 387 } 388 389 398 protected abstract DataObject handleFindDataObject ( 399 FileObject fo, RecognizedFiles recognized 400 ) throws IOException; 401 402 410 public final void markFile (FileObject fo) throws IOException { 411 DataLoaderPool.setPreferredLoader(fo, this); 412 } 413 414 415 416 417 420 public void writeExternal (ObjectOutput oo) throws IOException { 421 oo.writeObject( new Integer (LOADER_VERSION) ); 422 423 SystemAction[] arr = (SystemAction[])getProperty (PROP_ACTIONS); 424 if (arr == null) { 425 oo.writeObject (null); 426 } else { 427 List<String > names = new LinkedList<String >(); 429 for (int i = 0; i < arr.length; i++) { 430 if (arr[i] == null) { 431 names.add (null); 432 } else { 433 names.add (arr[i].getClass ().getName ()); 434 } 435 } 436 oo.writeObject (names.toArray ()); 437 } 438 439 String dn = (String ) getProperty (PROP_DISPLAY_NAME); 440 if ( dn == null ) 441 dn = ""; oo.writeUTF ( dn ); 443 } 444 445 451 public void readExternal (ObjectInput oi) 452 throws IOException, ClassNotFoundException { 453 Exception main = null; 454 int version = 0; 455 456 Object first = oi.readObject (); 457 if ( first instanceof Integer ) { 458 version = ((Integer )first).intValue(); 459 first = oi.readObject (); 460 } 461 Object [] arr = (Object [])first; 463 boolean isdefault = true; 464 465 SystemAction[] defactions = getActions (); 466 467 if ( version > 0 || ( version == 0 && arr.length != defactions.length )) 468 isdefault = false; 469 if (arr != null) { 470 List<SystemAction> ll = new ArrayList<SystemAction>(arr.length); 471 for (int i = 0; i < arr.length; i++) { 472 if (arr[i] == null) { 473 ll.add (null); 474 if ( version == 0 && isdefault && defactions[i] != null) 475 isdefault = false; 476 continue; 477 } 478 479 try { 480 ClassLoader loader = Lookup.getDefault().lookup(ClassLoader .class); 481 if (loader == null) { 482 loader = getClass ().getClassLoader (); 483 } 484 Class <? extends SystemAction> c = Class.forName ( 485 Utilities.translate((String )arr[i]), 486 false, loader 488 ).asSubclass(SystemAction.class); 489 SystemAction ac = SystemAction.get(c); 490 491 ll.add (ac); 492 if ( version == 0 && isdefault && !defactions[i].equals(ac)) 493 isdefault = false; 494 } catch (ClassNotFoundException ex) { 495 if (main == null) { 496 main = ex; 497 } else { 498 Throwable t = main; 499 while (t.getCause() != null) { 500 t = t.getCause(); 501 } 502 t.initCause(ex); 503 } 504 } 505 } 506 if (main == null && !isdefault) { 507 setActions(ll.toArray(new SystemAction[ll.size()])); 509 } } 511 512 String displayName = oi.readUTF (); 513 if ( displayName.equals("") || ( version == 0 && displayName.equals(defaultDisplayName()))) displayName = null; 515 setDisplayName( displayName ); 516 517 if (main != null) { 518 SafeException se = new SafeException (main); 520 String message = NbBundle.getMessage (DataLoader.class, "EXC_missing_actions_in_loader", getDisplayName ()); 522 Exceptions.attachLocalizedMessage(se, message); 523 throw se; 524 } 525 } 526 527 protected boolean clearSharedData () { 528 return false; 529 } 530 531 536 public static <T extends DataLoader> T getLoader(Class <T> loaderClass) { 537 return findObject(loaderClass, true); 538 } 539 540 545 public interface RecognizedFiles { 546 551 public void markRecognized (FileObject fo); 552 } 553 554 } 555 | Popular Tags |