001: package net.xoetrope.builder.editor;
002:
003: import java.io.FileInputStream;
004: import java.io.InputStream;
005: import java.net.URL;
006:
007: import java.awt.Image;
008: import java.awt.Toolkit;
009:
010: import net.xoetrope.debug.DebugLogger;
011: import net.xoetrope.xui.XResourceManager;
012: import net.xoetrope.xui.build.BuildProperties;
013:
014: /**
015: * An extension of XResourceManager to allow greater interaction with the Editor-IDE.
016: * This class subclasses getInputStream to allow dynamic loading of pages via a
017: * custom classloader. The class also accomodates the slightly different startup
018: * sequence and the fact that the editor itself has a startup set of properties
019: * in addition to the startup properties of the application being edited.
020: * <p>Copyright (c) Xoetrope Ltd., 1998-2003</p>
021: * @version $Revision: 1.21 $
022: */
023: public class XEditorResourceManager extends XResourceManager {
024: /**
025: * Construct an instance of this class.
026: * @return the XEditorResourceManager instance
027: * @deprecated use XEditorProjectManager.getResourceManager() instead
028: */
029: public static XResourceManager getInstance() {
030: return XEditorProjectManager.getResourceManager();
031: }
032:
033: /**
034: * Gets a stream for a resource
035: * @param fileName the resource file name
036: * @return the InputStream
037: */
038: public InputStream getInputStream(String fileName) {
039: FileInputStream result = null;
040: ClassLoader cl = getClass().getClassLoader();
041:
042: try {
043: if (BuildProperties.DEBUG)
044: DebugLogger.trace("Opening resource file:" + fileName);
045: InputStream is = null;//cl.getResourceAsStream( fileName );
046: if (BuildProperties.DEBUG) {
047: if (is == null) {
048: int numClassLoaders = customClassLoaders.size();
049: if (numClassLoaders > 0) {
050: for (int i = 0; i < numClassLoaders; i++) {
051: is = ((ClassLoader) customClassLoaders
052: .elementAt(i))
053: .getResourceAsStream(fileName);
054: if (is != null)
055: break;
056: }
057: }
058: }
059: if (is == null)
060: DebugLogger
061: .logError("File not loaded from classpath: "
062: + fileName);
063: else
064: return is;
065: }
066: } catch (Exception ex1) {
067: ex1.printStackTrace();
068: }
069:
070: return super .getInputStream(fileName);
071:
072: }
073:
074: /*
075: * Sets the startup file and loads the associated resource.
076: * @param file the name of the startup resource file.
077: */
078: public static void setStartupFile(String fileName) {
079: XEditorProjectManager.getCurrentProject().setStartupFile(
080: fileName);
081: }
082:
083: /**
084: * Loads an image resource
085: * @param name the image resource name
086: * @return the image
087: */
088: public static Image getImage(String name) {
089: if (name == null)
090: return null;
091:
092: Image result = XResourceManager.getImage(name);
093: if ((result == null) && (customClassLoaders.size() > 0)) {
094: int numClassLoaders = customClassLoaders.size();
095: if (numClassLoaders > 0) {
096: for (int i = 0; i < numClassLoaders; i++) {
097: result = Toolkit.getDefaultToolkit().createImage(
098: ((ClassLoader) customClassLoaders
099: .elementAt(i)).getResource(name));
100: if (result != null)
101: break;
102: }
103: }
104: }
105:
106: if (BuildProperties.DEBUG) {
107: if (result == null)
108: DebugLogger.logWarning("Cannot load image: " + name);
109: }
110:
111: return result;
112: }
113:
114: /**
115: * Loads an image resource
116: * @param url the image resource url
117: * @return the image
118: */
119: public static Image getImage(URL url) {
120: if (url == null)
121: return null;
122:
123: Image result = Toolkit.getDefaultToolkit().getImage(url);
124: if ((result == null) && (customClassLoaders.size() > 0)) {
125: int numClassLoaders = customClassLoaders.size();
126: if (numClassLoaders > 0) {
127: for (int i = 0; i < numClassLoaders; i++) {
128: result = Toolkit.getDefaultToolkit().createImage(
129: ((ClassLoader) customClassLoaders
130: .elementAt(i)).getResource(url
131: .getFile()));
132: if (result != null)
133: break;
134: }
135: }
136: }
137:
138: if (BuildProperties.DEBUG) {
139: if (result == null)
140: DebugLogger.logWarning("Cannot load image: "
141: + url.getFile());
142: }
143:
144: return result;
145: }
146:
147: /**
148: * Loads an image resource
149: * @param clazz the class whos classloader will be used while attempting to load the image
150: * @param name the image resource name
151: * @return the image
152: */
153: public static Image getImage(Class clazz, String name) {
154: Image result = Toolkit.getDefaultToolkit().createImage(
155: clazz.getClassLoader().getResource(name));
156: if (result == null)
157: result = getImage(name);
158:
159: return result;
160: }
161:
162: /**
163: * Change or add a startup property
164: * @param key the object key
165: * @param value the object value
166: */
167: public void setStartupParam(String key, String value) {
168: ((XEditorProject) XEditorProjectManager.getCurrentProject())
169: .setStartupParam(key, value);
170: }
171: }
|