001: /*
002: * $RCSfile: J3fLoader.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.4 $
041: * $Date: 2007/02/09 17:17:00 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.loaders.wrappers;
046:
047: import com.sun.j3d.loaders.Scene;
048: import com.sun.j3d.loaders.SceneBase;
049:
050: import javax.media.j3d.BranchGroup;
051:
052: import com.sun.j3d.utils.scenegraph.io.SceneGraphFileReader;
053:
054: /**
055: * Provides a Loader which can load Java3D scene graphs saved using the
056: * SceneGraphFileWriter.
057: *
058: * The Java3D LoaderInterface implemented by this class does not provide access
059: * to all the features that can be stored in a .j3f file.
060: * Most importantly the file may contain a universe which will be ignored
061: * by this loader.
062: *
063: * @author Paul Byrne
064: * @version 1.8, 01/18/02
065: */
066: public class J3fLoader extends com.sun.j3d.loaders.LoaderBase {
067:
068: private SceneGraphFileReader reader = null;
069: // private com.sun.j3d.demos.utils.scenegraph.io.SceneGraphFileReader oldReader=null;
070: // private static J3fLoaderListener loaderListener = null;
071: private BranchGroup[] graphs = null;
072: private static ClassLoader classLoader = null;
073:
074: /**
075: * Setup a listener to get notified when this loader is used
076: */
077: // public static void setJ3fLoaderListener( J3fLoaderListener listener ) {
078: // loaderListener = listener;
079: // }
080: /**
081: * Set the ClassLoader which will be used by the SceneGraphIO system
082: * when loading the scene graph.
083: *
084: * The default null in which case the system will use system class loader
085: */
086: public static void setClassLoader(ClassLoader cLoader) {
087: classLoader = cLoader;
088: }
089:
090: /**
091: * Get the ClassLoader set by setClassLoader
092: *
093: * If setClassLoader has not been called the default null will be returned.
094: * This indicates that systemClassLoader will be used.
095: */
096: public static ClassLoader getClassLoader() {
097: return classLoader;
098: }
099:
100: /**
101: * Load all the BranchGraphs from the file called filename and return
102: * a Scene containing all the BranchGraphs
103: */
104: public Scene load(String filename)
105: throws java.io.FileNotFoundException,
106: com.sun.j3d.loaders.IncorrectFormatException,
107: com.sun.j3d.loaders.ParsingErrorException {
108:
109: SceneBase scenebase = null;
110: Scene scene = null;
111: try {
112: reader = new SceneGraphFileReader(
113: new java.io.File(filename));
114: if (classLoader != null)
115: reader.setClassLoader(classLoader);
116: int size = reader.getBranchGraphCount();
117: scenebase = new SceneBase();
118:
119: BranchGroup bg = new BranchGroup();
120: graphs = new BranchGroup[size];
121:
122: if (size > 1) {
123: for (int i = 0; i < size; i++) {
124: graphs[i] = reader.readBranchGraph(i)[0];
125: bg.addChild(graphs[i]);
126: }
127: } else {
128: bg = reader.readBranchGraph(0)[0];
129: graphs[0] = bg;
130: }
131:
132: scenebase.setSceneGroup(bg);
133:
134: String[] objectNames = reader.getNames();
135: for (int i = 0; i < objectNames.length; i++)
136: try {
137: scenebase.addNamedObject(objectNames[i], reader
138: .getNamedObject(objectNames[i]));
139: } catch (com.sun.j3d.utils.scenegraph.io.ObjectNotLoadedException e) {
140: } catch (com.sun.j3d.utils.scenegraph.io.NamedObjectException e) {
141: }
142:
143: // if (loaderListener!=null)
144: // loaderListener.loadingJ3f( this );
145: reader.close();
146:
147: } catch (java.io.InvalidClassException ex) {
148: throw new com.sun.j3d.loaders.IncorrectFormatException(ex
149: .getMessage());
150: } catch (java.io.FileNotFoundException e) {
151: throw new java.io.FileNotFoundException(filename);
152: } catch (java.io.IOException exc) {
153: throw new java.io.FileNotFoundException(exc.getMessage());
154: }
155:
156: if (scenebase == null)
157: return scene;
158: else
159: return scenebase;
160: }
161:
162: /**
163: * Scene only supports a single sceneBase.
164: *
165: * For any J3f files containing multiple BranchGraphs all the graphs
166: * will be added as children of SceneBase.
167: *
168: * This call will return the actual set of BranchGraphs (this will
169: * still be children of the sceneBase ). If their is only a single
170: * branchgraph getBranchGrapgs()[0] == sceneBase
171: */
172: public BranchGroup[] getBranchGraphs() {
173: return graphs;
174: }
175:
176: /**
177: * Get the user object for the file, should be called from within a J3fLoaderListener only
178: */
179: public Object getFileUserData() throws java.io.IOException {
180: return reader.readUserData();
181: }
182:
183: /** Not Implemented
184: */
185: public Scene load(java.net.URL url)
186: throws java.io.FileNotFoundException,
187: com.sun.j3d.loaders.IncorrectFormatException,
188: com.sun.j3d.loaders.ParsingErrorException {
189: throw new RuntimeException("NOT IMPLEMENTED");
190: }
191:
192: /** Not Implemented
193: */
194: public Scene load(java.io.Reader reader)
195: throws java.io.FileNotFoundException,
196: com.sun.j3d.loaders.IncorrectFormatException,
197: com.sun.j3d.loaders.ParsingErrorException {
198:
199: throw new RuntimeException("NOT IMPLEMENTED");
200: }
201:
202: }
|