001: /*
002: * $RCSfile: SceneGraphFileWriter.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: import java.util.HashMap;
050: import java.util.Map;
051: import java.util.Iterator;
052:
053: import javax.media.j3d.BranchGroup;
054: import javax.media.j3d.SceneGraphObject;
055: import javax.media.j3d.CapabilityNotSetException;
056: import com.sun.j3d.utils.scenegraph.io.retained.RandomAccessFileControl;
057: import com.sun.j3d.utils.universe.SimpleUniverse;
058:
059: /**
060: * Write a (set) of Java3D BranchGraphs and/or Universe to a file. The BranchGraphs
061: * are stored in the order in which they are written, they can be read in any order
062: * using SceneGraphFileReader.
063: *
064: * The API handles Nodes and NodeComponents that are shared between seperate
065: * graphs. It will handle all Java3D 1.3 core classes and any user
066: * subclass of a Node or NodeComponent that implements the SceneGraphIO
067: * interface.
068: */
069: public class SceneGraphFileWriter extends java.lang.Object {
070:
071: private RandomAccessFileControl fileControl;
072: private File file;
073:
074: /** Creates new SceneGraphFileWriter and opens the file for writing.
075: *
076: * <P>Writes the
077: * Java3D Universe structure to the file. This includes the number and position of
078: * the Locales, PlatformGeometry, ViewerAvatar, and the MultitransformGroup between
079: * the ViewingPlatform and the View. However this
080: * call does not write the content of the branch graphs unless writeUniverseContent is true.
081: * <code>universe</code> may be null.
082: * This call will overwrite any existing universe, fileDescription and
083: * userData in the file.</P>
084: *
085: * <P>close() MUST be called when IO is complete. If close() is not called
086: * the file contents will be undefined.</P>
087: *
088: * @param file The file to write the data to
089: * @param universe The SimpleUniverse to write
090: * @param writeUniverseContent If true, the content of the Locales will be written.
091: * Otherwise just the universe configuration data will be written.
092: * @param fileDescription A description of the file's content
093: * @param fileUserData User defined object
094: *
095: * @exception IOException Thrown if there are any IO errors
096: * @exception UnsupportedUniverseException Thrown if <code>universe</code> is not
097: * a supported universe class. Currently SimpleUniverse and ConfiguredUniverse
098: * are supported.
099: */
100: public SceneGraphFileWriter(java.io.File file,
101: SimpleUniverse universe, boolean writeUniverseContent,
102: String fileDescription, java.io.Serializable fileUserData)
103: throws IOException, UnsupportedUniverseException {
104: fileControl = new RandomAccessFileControl();
105: this .file = file;
106: file.createNewFile();
107:
108: if (!file.canWrite())
109: throw new IOException("Can not Write to File");
110:
111: fileControl.createFile(file, universe, writeUniverseContent,
112: fileDescription, fileUserData);
113: }
114:
115: /**
116: * Write the graph to the end of the file.
117: *
118: * close() MUST be called when IO is complete. If close() is not called
119: * the file contents will be undefined.
120: */
121: public void writeBranchGraph(BranchGroup graph) throws IOException {
122: writeBranchGraph(graph, null);
123: }
124:
125: /**
126: * Write a branch graph and some user associated data to the
127: * end of the file.
128: *
129: * close() MUST be called when IO is complete. If close() is not called
130: * the file contents will be undefined.
131: */
132: public void writeBranchGraph(BranchGroup graph,
133: java.io.Serializable data) throws IOException {
134: fileControl.writeBranchGraph(graph, data);
135: }
136:
137: /**
138: * Add a named reference to a SceneGraphObject in the file.
139: *
140: * <code>object</code> must have been written to the file before this method is
141: * called. If the object is not in the file a NamedObjectException will be thrown.
142: *
143: * Adding duplicate names will result in the old name being overwritten.
144: * Different names can reference the same object
145: */
146: public void addObjectName(String name, SceneGraphObject object)
147: throws NamedObjectException {
148: fileControl.addNamedObject(name, object);
149: }
150:
151: /**
152: * Close the file and cleanup internal data structures.
153: */
154: public void close() throws IOException {
155: fileControl.close();
156: }
157:
158: }
|