001: /*
002: * $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/utils/loaderwrappers/J3fLoader.java,v 1.1 2005/04/20 21:05:11 paulby Exp $
003: *
004: * Sun Public License Notice
005: *
006: * The contents of this file are subject to the Sun Public License Version
007: * 1.0 (the "License"). You may not use this file except in compliance with
008: * the License. A copy of the License is available at http://www.sun.com/
009: *
010: * The Original Code is Java 3D(tm) Fly Through.
011: * The Initial Developer of the Original Code is Paul Byrne.
012: * Portions created by Paul Byrne are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Paul Byrne.
016: *
017: **/
018: package org.jdesktop.j3dfly.utils.loaderwrappers;
019:
020: import com.sun.j3d.loaders.Scene;
021: import com.sun.j3d.loaders.SceneBase;
022:
023: import javax.media.j3d.BranchGroup;
024:
025: import com.sun.j3d.utils.scenegraph.io.SceneGraphFileReader;
026:
027: /**
028: * Provides a Loader which can load Java3D scene graphs saved using the
029: * SceneGraphFileWriter.
030: *
031: * The Java3D LoaderInterface implemented by this class does not provide access
032: * to all the features that can be stored in a .j3f file.
033: * Most importantly the file may contain a universe which will be ignored
034: * by this loader.
035: *
036: * @author Paul Byrne
037: * @version 1.8, 01/18/02
038: */
039: public class J3fLoader extends com.sun.j3d.loaders.LoaderBase {
040:
041: private SceneGraphFileReader reader = null;
042: //private com.sun.j3d.demos.utils.scenegraph.io.SceneGraphFileReader oldReader=null;
043: private static J3fLoaderListener loaderListener = null;
044: private BranchGroup[] graphs = null;
045: private static ClassLoader classLoader = null;
046:
047: /**
048: * Setup a listener to get notified when this loader is used
049: */
050: public static void setJ3fLoaderListener(J3fLoaderListener listener) {
051: loaderListener = listener;
052: }
053:
054: /**
055: * Set the ClassLoader which will be used by the SceneGraphIO system
056: * when loading the scene graph.
057: *
058: * The default null in which case the system will use system class loader
059: */
060: public static void setClassLoader(ClassLoader cLoader) {
061: classLoader = cLoader;
062: }
063:
064: /**
065: * Get the ClassLoader set by setClassLoader
066: *
067: * If setClassLoader has not been called the default null will be returned.
068: * This indicates that systemClassLoader will be used.
069: */
070: public static ClassLoader getClassLoader() {
071: return classLoader;
072: }
073:
074: /**
075: * Load all the BranchGraphs from the file called filename and return
076: * a Scene containing all the BranchGraphs
077: */
078: public Scene load(String filename)
079: throws java.io.FileNotFoundException,
080: com.sun.j3d.loaders.IncorrectFormatException,
081: com.sun.j3d.loaders.ParsingErrorException {
082:
083: SceneBase scenebase = null;
084: Scene scene = null;
085: try {
086: reader = new SceneGraphFileReader(
087: new java.io.File(filename));
088: if (classLoader != null)
089: reader.setClassLoader(classLoader);
090: int size = reader.getBranchGraphCount();
091: scenebase = new SceneBase();
092:
093: BranchGroup bg = new BranchGroup();
094: graphs = new BranchGroup[size];
095:
096: if (size > 1) {
097: for (int i = 0; i < size; i++) {
098: graphs[i] = reader.readBranchGraph(i)[0];
099: bg.addChild(graphs[i]);
100: }
101: } else {
102: bg = reader.readBranchGraph(0)[0];
103: graphs[0] = bg;
104: }
105:
106: scenebase.setSceneGroup(bg);
107:
108: String[] objectNames = reader.getNames();
109: for (int i = 0; i < objectNames.length; i++)
110: try {
111: scenebase.addNamedObject(objectNames[i], reader
112: .getNamedObject(objectNames[i]));
113: } catch (com.sun.j3d.utils.scenegraph.io.ObjectNotLoadedException e) {
114: } catch (com.sun.j3d.utils.scenegraph.io.NamedObjectException e) {
115: }
116:
117: if (loaderListener != null)
118: loaderListener.loadingJ3f(this );
119: reader.close();
120:
121: } catch (java.io.InvalidClassException ex) {
122: throw new com.sun.j3d.loaders.IncorrectFormatException(ex
123: .getMessage());
124: } catch (java.io.FileNotFoundException e) {
125: throw new java.io.FileNotFoundException(filename);
126: } catch (java.io.IOException exc) {
127: // if (exc.getMessage().startsWith("Use Java 3D Fly"))
128: // return oldLoad( filename );
129: // else
130: throw new java.io.FileNotFoundException(exc.getMessage());
131: }
132:
133: if (scenebase == null)
134: return scene;
135: else
136: return scenebase;
137: }
138:
139: /**
140: * Load all the BranchGraphs from the file called filename and return
141: * a Scene containing all the BranchGraphs
142: *
143: * This method uses the OLD io package which was included with J3dFly 1.0
144: */
145: // private Scene oldLoad( String filename ) throws java.io.FileNotFoundException,
146: // com.sun.j3d.loaders.IncorrectFormatException,
147: // com.sun.j3d.loaders.ParsingErrorException {
148: //
149: // SceneBase scenebase = null;
150: // Scene scene=null;
151: // try {
152: // oldReader = new com.sun.j3d.demos.utils.scenegraph.io.SceneGraphFileReader( new java.io.File( filename ));
153: // int size = oldReader.getBranchGraphCount();
154: // scenebase = new SceneBase();
155: //
156: // BranchGroup bg = new BranchGroup();
157: // graphs = new BranchGroup[ size ];
158: //
159: // if (size>1) {
160: // for(int i=0; i<size; i++) {
161: // graphs[i] = oldReader.readBranchGraph( i )[0];
162: // bg.addChild( graphs[i] );
163: // }
164: // } else {
165: // bg = oldReader.readBranchGraph(0)[0];
166: // graphs[0] = bg;
167: // }
168: //
169: // scenebase.setSceneGroup( bg );
170: //
171: // String[] objectNames = oldReader.getNames();
172: // for(int i=0; i<objectNames.length; i++)
173: // try {
174: // scenebase.addNamedObject( objectNames[i], oldReader.getNamedObject( objectNames[i] ) );
175: // } catch( com.sun.j3d.demos.utils.scenegraph.io.ObjectNotLoadedException e ) {}
176: // catch( com.sun.j3d.demos.utils.scenegraph.io.NamedObjectException e ) {}
177: //
178: // if (loaderListener!=null)
179: // loaderListener.loadingJ3f( this );
180: // oldReader.close();
181: //
182: // } catch( java.io.InvalidClassException ex ) {
183: // throw new com.sun.j3d.loaders.IncorrectFormatException( ex.getMessage() );
184: // } catch( java.io.FileNotFoundException e ) {
185: // throw new java.io.FileNotFoundException( filename );
186: // } catch( java.io.IOException exc ) {
187: // throw new java.io.FileNotFoundException( exc.getMessage() );
188: // }
189: //
190: //
191: // if (scenebase==null)
192: // return scene;
193: // else return scenebase;
194: // }
195: /**
196: * Scene only supports a single sceneBase.
197: *
198: * For any J3f files containing multiple BranchGraphs all the graphs
199: * will be added as children of SceneBase.
200: *
201: * This call will return the actual set of BranchGraphs (this will
202: * still be children of the sceneBase ). If their is only a single
203: * branchgraph getBranchGrapgs()[0] == sceneBase
204: */
205: public BranchGroup[] getBranchGraphs() {
206: return graphs;
207: }
208:
209: /**
210: * Get the user object for the file, should be called from within a J3fLoaderListener only
211: */
212: public Object getFileUserData() throws java.io.IOException {
213: // if (oldReader!=null)
214: // return oldReader.readUserData();
215:
216: return reader.readUserData();
217: }
218:
219: /** Not Implemented
220: */
221: public Scene load(java.net.URL url)
222: throws java.io.FileNotFoundException,
223: com.sun.j3d.loaders.IncorrectFormatException,
224: com.sun.j3d.loaders.ParsingErrorException {
225: throw new RuntimeException("NOT IMPLEMENTED");
226: }
227:
228: /** Not Implemented
229: */
230: public Scene load(java.io.Reader reader)
231: throws java.io.FileNotFoundException,
232: com.sun.j3d.loaders.IncorrectFormatException,
233: com.sun.j3d.loaders.ParsingErrorException {
234:
235: throw new RuntimeException("NOT IMPLEMENTED");
236: }
237:
238: }
|