XEditorResourceManager.java :  » XML-UI » XUI » net » xoetrope » builder » editor » Java Open Source

Java Open Source » XML UI » XUI 
XUI » net » xoetrope » builder » editor » XEditorResourceManager.java
package net.xoetrope.builder.editor;

import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;

import java.awt.Image;
import java.awt.Toolkit;

import net.xoetrope.debug.DebugLogger;
import net.xoetrope.xui.XResourceManager;
import net.xoetrope.xui.build.BuildProperties;

/**
 * An extension of XResourceManager to allow greater interaction with the Editor-IDE.
 * This class subclasses getInputStream to allow dynamic loading of pages via a
 * custom classloader. The class also accomodates the slightly different startup
 * sequence and the fact that the editor itself has a startup set of properties
 * in addition to the startup properties of the application being edited.
 * <p>Copyright (c) Xoetrope Ltd., 1998-2003</p>
 * @version $Revision: 1.21 $
 */
public class XEditorResourceManager extends XResourceManager
{
  /**
   * Construct an instance of this class.
   * @return the XEditorResourceManager instance
   * @deprecated use XEditorProjectManager.getResourceManager() instead
   */
  public static XResourceManager getInstance()
  {
    return XEditorProjectManager.getResourceManager();
  }

  /**
   * Gets a stream for a resource
   * @param fileName the resource file name
   * @return the InputStream
   */
  public InputStream getInputStream( String fileName )
  {
    FileInputStream result = null;
    ClassLoader cl = getClass().getClassLoader();

    try {
      if ( BuildProperties.DEBUG )
        DebugLogger.trace( "Opening resource file:" + fileName );
      InputStream is = null;//cl.getResourceAsStream( fileName );
      if ( BuildProperties.DEBUG ) {
        if ( is == null ) {
          int numClassLoaders = customClassLoaders.size();
          if ( numClassLoaders > 0 ) {
            for ( int i = 0; i < numClassLoaders; i++ ) {
              is = ( ( ClassLoader )customClassLoaders.elementAt( i ) ).getResourceAsStream( fileName );
              if ( is != null )
                break;
            }
          }
        }
        if ( is == null )
          DebugLogger.logError( "File not loaded from classpath: " + fileName );
        else
          return is;
      }
    }
    catch ( Exception ex1 ) {
      ex1.printStackTrace();
    }

    return super.getInputStream( fileName );

  }

  /*
   * Sets the startup file and loads the associated resource.
   * @param file the name of the startup resource file.
   */
  public static void setStartupFile( String fileName )
  {
    XEditorProjectManager.getCurrentProject().setStartupFile( fileName );
  }

  /**
   * Loads an image resource
   * @param name the image resource name
   * @return the image
   */
  public static Image getImage( String name )
  {
    if ( name == null )
      return null;

    Image result = XResourceManager.getImage( name );
      if (( result == null ) && ( customClassLoaders.size() > 0 )) {
        int numClassLoaders = customClassLoaders.size();
        if ( numClassLoaders > 0 ) {
          for ( int i = 0; i < numClassLoaders; i++ ) {
            result = Toolkit.getDefaultToolkit().createImage( ((ClassLoader)customClassLoaders.elementAt( i )).getResource( name ));
            if ( result != null )
              break;
          }
        }
      }

    if ( BuildProperties.DEBUG ) {
      if ( result == null )
        DebugLogger.logWarning( "Cannot load image: " + name );
    }

    return result;
  }

  /**
   * Loads an image resource
   * @param url the image resource url
   * @return the image
   */
  public static Image getImage( URL url )
  {
    if ( url == null )
      return null;

    Image result = Toolkit.getDefaultToolkit().getImage( url );
      if (( result == null ) && ( customClassLoaders.size() > 0 )) {
        int numClassLoaders = customClassLoaders.size();
        if ( numClassLoaders > 0 ) {
          for ( int i = 0; i < numClassLoaders; i++ ) {
            result = Toolkit.getDefaultToolkit().createImage( ((ClassLoader)customClassLoaders.elementAt( i )).getResource( url.getFile() ));
            if ( result != null )
              break;
          }
        }
      }

    if ( BuildProperties.DEBUG ) {
      if ( result == null )
        DebugLogger.logWarning( "Cannot load image: " + url.getFile() );
    }

    return result;
  }

  /**
   * Loads an image resource
   * @param clazz the class whos classloader will be used while attempting to load the image
   * @param name the image resource name
   * @return the image
   */
  public static Image getImage( Class clazz, String name )
  {
    Image result = Toolkit.getDefaultToolkit().createImage( clazz.getClassLoader().getResource( name ));
    if ( result == null )
        result = getImage( name );

    return result;
  }

  /**
   * Change or add a startup property
   * @param key the object key
   * @param value the object value
   */
  public void setStartupParam( String key, String value )
  {
    ((XEditorProject)XEditorProjectManager.getCurrentProject()).setStartupParam( key, value );
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.