001: /*
002: * $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/utils/loadercontrol/SceneLoader.java,v 1.1 2005/04/20 21:05:07 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.loadercontrol;
019:
020: import com.sun.j3d.loaders.Loader;
021: import com.sun.j3d.loaders.Scene;
022: import java.io.*;
023: import javax.swing.SwingUtilities;
024: import javax.swing.Timer;
025: import java.awt.event.ActionEvent;
026: import java.lang.reflect.Method;
027:
028: class SceneLoader extends Object {
029:
030: public static final int USE_READER = 0;
031: public static final int USE_FILENAME = 1;
032: public static final int USE_URL = 2;
033: public static final int USE_NONSTANDARD = 3;
034:
035: private InputStream inputStream;
036: private Thread worker;
037: private Timer timer;
038: private String filename;
039: private int progress;
040: private Runnable setProgress;
041:
042: /** Creates new SceneLoader */
043: public SceneLoader(final Loader loader,
044: final LoaderControlListener loaderControl, final File file,
045: final int loaderID, final int useLoaderInterface,
046: final org.jdesktop.j3dfly.utils.gui.WorkMonitor monitor) {
047:
048: if (monitor != null) {
049: setProgress = new Runnable() {
050: public void run() {
051: monitor.setProgress(progress);
052: }
053: };
054: } else
055: setProgress = null;
056:
057: worker = new Thread() {
058: public void run() {
059: long startTime = System.currentTimeMillis();
060: progress = 0;
061: if (setProgress != null)
062: SwingUtilities.invokeLater(setProgress);
063: try {
064: Scene scene = null;
065:
066: switch (useLoaderInterface) {
067: case USE_READER:
068: try {
069: inputStream = new BufferedInputStream(
070: new FileInputStream(file));
071: } catch (FileNotFoundException e) {
072: loaderControl.sceneLoaded(
073: LoaderControlListener.LOAD_FAILED,
074: null, loaderID, file, e);
075: return;
076: }
077: loader
078: .setBasePath(file
079: .getAbsolutePath()
080: .substring(
081: 0,
082: file
083: .getAbsolutePath()
084: .lastIndexOf(
085: java.io.File.separator)));
086: scene = loader
087: .load(new java.io.InputStreamReader(
088: inputStream));
089: break;
090: case USE_FILENAME:
091: scene = loader.load(file.getAbsolutePath());
092: break;
093:
094: case USE_URL:
095: scene = loader.load(file.toURL());
096: break;
097: case USE_NONSTANDARD:
098: if (loader.getClass().getName().equals(
099: "ncsa.j3d.loaders.ModelLoader")) {
100: String fileext = file.getName()
101: .substring(
102: file.getName().lastIndexOf(
103: '.') + 1);
104: Method loadMethod = loader
105: .getClass()
106: .getMethod(
107: "load",
108: new Class[] {
109: java.io.Reader.class,
110: String.class });
111: try {
112: inputStream = new BufferedInputStream(
113: new FileInputStream(file));
114: } catch (FileNotFoundException e) {
115: loaderControl
116: .sceneLoaded(
117: LoaderControlListener.LOAD_FAILED,
118: null, loaderID, file, e);
119: return;
120: }
121: scene = (com.sun.j3d.loaders.Scene) loadMethod
122: .invoke(
123: loader,
124: new Object[] {
125: new java.io.InputStreamReader(
126: inputStream),
127: fileext });
128: } else {
129: RuntimeException e = new RuntimeException(
130: "No Nonstandard loader for "
131: + loader.getClass()
132: .getName());
133: loaderControl.sceneLoaded(
134: LoaderControlListener.LOAD_FAILED,
135: null, loaderID, file, e);
136: progress = -1;
137: timer.stop();
138: if (setProgress != null)
139: SwingUtilities.invokeLater(setProgress);
140: return;
141: }
142: break;
143: }
144:
145: //System.out.println("Load Time "+(System.currentTimeMillis()-startTime));
146: if (setProgress != null) {
147: progress = -1;
148: timer.stop();
149: SwingUtilities.invokeLater(setProgress);
150: }
151:
152: // The NCSA Loader sometimes fails without throwing an exception
153: // and returns a null scene
154: if (scene != null) {
155: loaderControl.sceneLoaded(
156: LoaderControlListener.LOAD_COMPLETE,
157: scene, loaderID, file, null);
158: } else {
159: loaderControl.sceneLoaded(
160: LoaderControlListener.LOAD_FAILED,
161: null, loaderID, file, null);
162: }
163: } catch (java.io.FileNotFoundException e) {
164: if (setProgress != null) {
165: progress = -1;
166: timer.stop();
167: SwingUtilities.invokeLater(setProgress);
168: }
169: loaderControl.sceneLoaded(
170: LoaderControlListener.LOAD_FAILED, null,
171: loaderID, file, e);
172: return;
173: //e.printStackTrace();
174: } catch (IOException ex) {
175: if (setProgress != null) {
176: progress = -1;
177: timer.stop();
178: SwingUtilities.invokeLater(setProgress);
179: }
180: loaderControl.sceneLoaded(
181: LoaderControlListener.LOAD_FAILED, null,
182: loaderID, file, ex);
183: return;
184: //System.out.println("User Cancel");
185: } catch (Exception exc) {
186: //exc.printStackTrace();
187: if (setProgress != null) {
188: progress = -1;
189: timer.stop();
190: SwingUtilities.invokeLater(setProgress);
191: }
192: loaderControl.sceneLoaded(
193: LoaderControlListener.LOAD_FAILED, null,
194: loaderID, file, exc);
195: return;
196: }
197:
198: }
199: };
200:
201: if (setProgress != null) {
202: timer = new Timer(1000, new ProgressTimer());
203: timer.start();
204: }
205:
206: worker.start();
207: }
208:
209: /**
210: * Wait for the load to complete
211: */
212: public void waitForCompletion() {
213: try {
214: worker.join();
215: } catch (InterruptedException e) {
216: e.printStackTrace();
217: }
218: }
219:
220: class ProgressTimer implements java.awt.event.ActionListener {
221: public void actionPerformed(ActionEvent e) {
222: progress++;
223:
224: SwingUtilities.invokeLater(setProgress);
225: }
226: }
227:
228: }
|