001: /*
002: * $RCSfile: SceneGraphFileReader.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:20:27 $
042: * $State: Exp $
043: */
044:
045: package com.sun.j3d.utils.scenegraph.io;
046:
047: import java.io.File;
048: import java.io.IOException;
049:
050: import javax.media.j3d.VirtualUniverse;
051: import javax.media.j3d.BranchGroup;
052: import javax.media.j3d.SceneGraphObject;
053: import javax.media.j3d.Canvas3D;
054: import com.sun.j3d.utils.universe.ConfiguredUniverse;
055:
056: import com.sun.j3d.utils.scenegraph.io.retained.RandomAccessFileControl;
057:
058: /**
059: * Read Java3D BranchGraphs and/or Universe from a file. Individual branchgraphs or an
060: * entire Universe can be read and references (shared nodes and components)
061: * between the graphs are handled correctly.
062: */
063: public class SceneGraphFileReader extends java.lang.Object {
064:
065: private RandomAccessFileControl fileControl;
066:
067: /**
068: * Creates new SceneGraphFileReader.
069: */
070: public SceneGraphFileReader(java.io.File file) throws IOException {
071: fileControl = new RandomAccessFileControl();
072: fileControl.openFile(file);
073: }
074:
075: /**
076: * Create and return a ConfiguredUniverse with the PlatformGeometry, ViewerAvatar,
077: * and Locales saved in the file. The MultiTransformGroup between the ViewingPlatform
078: * and the View is also restored. Universe configuration information is retrieved
079: * via <code>ConfiguredUniverse.getConfigURL()</code>.<p>
080: * If the file does not contain universe information, null is returned.<p>
081: *
082: * @param attachBranchGraphs load and attach all the branchgraphs
083: * to the universe.
084: * @see ConfiguredUniverse#getConfigURL
085: */
086: public ConfiguredUniverse readUniverse(boolean attachBranchGraphs)
087: throws IOException {
088: return fileControl.readUniverse(attachBranchGraphs, null);
089: }
090:
091: /**
092: * Set the ClassLoader used to load the scene graph objects and
093: * deserialize user data
094: */
095: public void setClassLoader(ClassLoader classLoader) {
096: fileControl.setClassLoader(classLoader);
097: }
098:
099: /**
100: * Get the ClassLoader used to load the scene graph objects and
101: * deserialize user data
102: */
103: public ClassLoader getClassLoader() {
104: return fileControl.getClassLoader();
105: }
106:
107: /**
108: * Create and return a ConfiguredUniverse with the PlatformGeometry, ViewerAvatar,
109: * and Locales saved in the file. The MultiTransformGroup between the ViewingPlatform
110: * and the View is also restored.<p>
111: * If the file does not contain universe information, null is returned.<p>
112: *
113: * @param attachBranchGraphs load and attach all the branchgraphs
114: * to the universe.
115: * @param canvas The canvas to be associated with the Universe.
116: */
117: public ConfiguredUniverse readUniverse(boolean attachBranchGraphs,
118: Canvas3D canvas) throws IOException {
119: return fileControl.readUniverse(attachBranchGraphs, canvas);
120: }
121:
122: /**
123: * Get the UserData in the File header
124: */
125: public Object readUserData() throws IOException {
126: return fileControl.getUserData();
127: }
128:
129: /**
130: * Get the Description of this file's contents
131: */
132: public String readDescription() throws IOException {
133: return fileControl.readFileDescription();
134: }
135:
136: /**
137: * Return the number of BranchGraphs in the file
138: */
139: public int getBranchGraphCount() {
140: return fileControl.getBranchGraphCount();
141: }
142:
143: /**
144: * Read the BranchGraph at index in the file. If the graph
145: * contains references to nodes in other BranchGraphs that have not already been
146: * loaded, they will also be loaded and returned.<p>
147: *
148: * The requested graph will always be the first element in the array.<p>
149: *
150: * The file index of all the Graphs can be discovered using <code>getBranchGraphPosition</code>.<p>
151: *
152: * @param index The index of the Graph in the file. First graph is at index 0
153: *
154: * @see #getBranchGraphPosition( BranchGroup graph )
155: *
156: */
157: public BranchGroup[] readBranchGraph(int index) throws IOException {
158: return fileControl.readBranchGraph(index);
159: }
160:
161: /**
162: * Read and return all the branchgraphs in the file
163: */
164: public BranchGroup[] readAllBranchGraphs() throws IOException {
165: return fileControl.readAllBranchGraphs();
166: }
167:
168: /**
169: * Remove the IO system's reference to this branchgraph and all its nodes.<p>
170: *
171: * References to all loaded graphs are maintained by the IO system in
172: * order to facilitate node and component sharing between the graphs.<p>
173: *
174: * This call removes the references to graph <code>index</code><p>
175: *
176: * NOT CURRENTLY IMPLEMENTED
177: */
178: public void dereferenceBranchGraph(BranchGroup graph) {
179: throw new RuntimeException("Not implemented");
180: }
181:
182: /**
183: * Given a BranchGraph that has been loaded return the index of the
184: * graph in the file. The the Branchgroup isn't found, -1 is returned.
185: */
186: public int getBranchGraphPosition(BranchGroup graph) {
187: return fileControl.getBranchGraphPosition(graph);
188: }
189:
190: /**
191: * Read the userdata for the branchgraph at 'index' in the file
192: *
193: * @param index the index of the graph in the file
194: */
195: public Object readBranchGraphUserData(int index) throws IOException {
196: return fileControl.readBranchGraphUserData(index);
197: }
198:
199: /**
200: * Return the names of all the named objects
201: */
202: public String[] getNames() {
203: return fileControl.getNames();
204: }
205:
206: /**
207: * Return the named object.
208: *
209: * @param name The name of the object
210: *
211: * @exception NamedObjectException is thrown if the name is not known to the system
212: * @exception ObjectNotLoadedException is thrown if the named object has not been loaded yet
213: */
214: public SceneGraphObject getNamedObject(String name)
215: throws NamedObjectException, ObjectNotLoadedException {
216: return fileControl.getNamedObject(name);
217: }
218:
219: /**
220: * Close the file and cleanup internal data structures
221: */
222: public void close() throws IOException {
223: fileControl.close();
224: }
225:
226: }
|