001: /*
002: * $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/doctools/DocManager.java,v 1.1 2005/04/20 21:04:23 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.doctools;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.io.Reader;
023: import java.io.BufferedReader;
024: import java.io.FileReader;
025: import java.io.StreamTokenizer;
026: import java.util.HashMap;
027: import java.net.URL;
028: import javax.swing.event.HyperlinkListener;
029:
030: import org.jdesktop.j3dfly.namecontrol.NameControl;
031:
032: /**
033: * Manages all the document pages for current j3f file
034: *
035: * @author paulby
036: * @version
037: */
038: public class DocManager extends java.lang.Object implements
039: HyperlinkListener {
040:
041: private HashMap objectMap;
042: private String documentPath = null;
043: private DocPanel docPanel;
044: private String defaultURL;
045:
046: /** Creates new DocManager */
047: public DocManager(DocPanel docPanel) {
048: this .docPanel = docPanel;
049: objectMap = new HashMap();
050: }
051:
052: public URL getDoc(Object obj) {
053: String url = (String) objectMap.get(obj);
054: if (url == null)
055: url = defaultURL;
056:
057: try {
058: return new URL("file://" + documentPath + "/" + url);
059: } catch (Exception e) {
060: e.printStackTrace();
061: }
062:
063: return null;
064: }
065:
066: /**
067: * Load the index file
068: */
069: public void loadIndex(File file) {
070: String currentObject = null;
071: String currentScene = null;
072: documentPath = file.getAbsolutePath();
073: documentPath = documentPath.substring(0, documentPath
074: .lastIndexOf(File.separatorChar));
075:
076: try {
077: Reader reader = new BufferedReader(new FileReader(file));
078: StreamTokenizer tok = new StreamTokenizer(reader);
079:
080: while (tok.nextToken() != StreamTokenizer.TT_EOF) {
081: if (tok.ttype != StreamTokenizer.TT_WORD)
082: throw new IOException("Syntax Error at line "
083: + tok.lineno() + " " + tok);
084:
085: if (tok.sval.equals("object")) {
086: checkForEquals(tok);
087: System.out.println("Object " + tok.sval);
088: currentObject = tok.sval;
089: } else if (tok.sval.equals("doc")) {
090: checkForEquals(tok);
091: System.out.println("Doc " + tok.sval);
092:
093: if (currentObject.equals("default"))
094: defaultURL = tok.sval;
095: else {
096: Object obj = docPanel.getJ3dFlyContext()
097: .getNameControl().getNamedObject(
098: currentScene, currentObject);
099: if (obj == null)
100: System.out
101: .println("Documentation error, can't find object "
102: + currentObject
103: + " in scene "
104: + currentScene);
105: else
106: objectMap.put(obj, tok.sval);
107: }
108: } else if (tok.sval.equals("scene")) {
109: checkForEquals(tok);
110: currentScene = tok.sval;
111: } else
112: throw new RuntimeException("Unknown token at line "
113: + tok.lineno() + " '" + tok.sval + "'");
114: }
115: } catch (IOException e) {
116: e.printStackTrace();
117: }
118: }
119:
120: /**
121: *Ensure the next token in the stream is an equals sign
122: */
123: private void checkForEquals(StreamTokenizer tok) throws IOException {
124: if (tok.nextToken() != '=')
125: throw new IOException("Syntax Error at line "
126: + tok.lineno() + " " + tok + " expecting =");
127: tok.nextToken();
128: }
129:
130: public void hyperlinkUpdate(
131: final javax.swing.event.HyperlinkEvent evt) {
132: if (evt.getEventType() != javax.swing.event.HyperlinkEvent.EventType.ACTIVATED)
133: return;
134:
135: System.out.println(evt.getURL());
136:
137: try {
138: docPanel.showPage(evt.getURL());
139: } catch (IOException ex) {
140: System.out.println(evt.getURL());
141: ex.printStackTrace();
142: }
143: }
144:
145: }
|