KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > JavadocFrame


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.ui;
35
36 import javax.swing.event.HyperlinkListener JavaDoc;
37 import javax.swing.event.HyperlinkEvent JavaDoc;
38 import java.net.URL JavaDoc;
39 import java.net.MalformedURLException JavaDoc;
40 import java.io.File JavaDoc;
41 import java.io.FileReader JavaDoc;
42 import java.io.BufferedReader JavaDoc;
43 import java.io.IOException JavaDoc;
44
45 import edu.rice.cs.util.UnexpectedException;
46
47 /**
48  * DrJava's Javadoc viewing frame
49  * @version $Id: JavadocFrame.java 3562 2006-02-27 19:14:22Z rcartwright $
50  */

51 public class JavadocFrame extends HTMLFrame {
52
53   private static final int MAX_READ_PACKAGES_LINES = 100;
54   private static final int MAX_READ_FOR_LINK_LINES = 100;
55   private static final String JavaDoc[] INTRO_PAGE= {
56     "overview-summary.html",
57     "packages.html"
58   };
59   private static final String JavaDoc INDEX_PAGE= "allclasses-frame.html";
60
61   private static String JavaDoc introPagePath(File JavaDoc destDir, String JavaDoc curClass) {
62     // Iterate through possible intro pages, looking for one that exists.
63
File JavaDoc test = new File JavaDoc(destDir, curClass + ".html");
64     for (int i = 0; !test.exists() && (i < INTRO_PAGE.length); i++) {
65       test = new File JavaDoc(destDir, INTRO_PAGE[i]);
66     }
67
68     // Packages.html might just be a pointer to another file
69
if (test.exists()) {
70       if (test.getName().equals("packages.html")) {
71       test = _parsePackagesFile(test, destDir);
72       }
73     }
74     else {
75       throw new IllegalStateException JavaDoc("No Javadoc HTML output files found!");
76     }
77     return test.getAbsolutePath();
78   }
79
80   /**
81    * Reads through the beginning of the packages.html file to determine
82    * if it is just a pointer to another file. Returns either the same
83    * file (if it's not a pointer), or the file used for "No frames" (if
84    * it is a pointer).
85    * @param packages Full path to the packages.html file
86    */

87   private static File JavaDoc _parsePackagesFile(File JavaDoc packages, File JavaDoc destDir) {
88     try {
89       FileReader JavaDoc fr = new FileReader JavaDoc(packages);
90       BufferedReader JavaDoc br = new BufferedReader JavaDoc(fr);
91       try { // process the opened file
92
String JavaDoc line = br.readLine();
93         int numLinesRead = 1;
94         boolean found = false;
95         while ((!found) &&
96                (numLinesRead < MAX_READ_PACKAGES_LINES) &&
97                (line != null)) {
98           found = (line.indexOf("The front page has been relocated") != -1);
99           if (!found) {
100             line = br.readLine();
101             numLinesRead++;
102           }
103         }
104         
105         // Replace packages.html with the No Frames link.
106
if (found) {
107           boolean foundLink = false;
108           while ((!foundLink) &&
109                  (numLinesRead < MAX_READ_FOR_LINK_LINES) &&
110                  (line != null)) {
111             foundLink = (line.indexOf("Non-frame version") != -1);
112             if (!foundLink) {
113               line = br.readLine();
114               numLinesRead++;
115             }
116           }
117           
118           if (foundLink) {
119             String JavaDoc start = "HREF=\"";
120             int startIndex = line.indexOf(start) + start.length();
121             int endIndex = line.indexOf("\">");
122             if ((startIndex != -1) && (endIndex != -1)) {
123               String JavaDoc fileName = line.substring(startIndex, endIndex);
124               return new File JavaDoc(destDir, fileName);
125             }
126           }
127         }
128       }
129       finally { br.close(); }
130     }
131     catch (IOException JavaDoc ioe) { throw new UnexpectedException(ioe); }
132     return packages;
133   }
134
135   /**
136    * Constructor.
137    * @param destDir Directory holding the Javadoc
138    * @param curClass Name of the class to try to show by default
139    * @param allDocs Whether Javadoc was run for all open documents
140    */

141   public JavadocFrame(File JavaDoc destDir, String JavaDoc curClass, boolean allDocs)
142     throws MalformedURLException JavaDoc
143   {
144     // This call has to happen first!
145
super("Javadoc Viewer",
146           new URL JavaDoc("file", "", introPagePath(destDir, curClass)),
147           new URL JavaDoc("file", "", (new File JavaDoc(destDir, INDEX_PAGE)).getAbsolutePath()),
148            "DrJavadoc.png", destDir);
149
150     addHyperlinkListener(new HyperlinkListener JavaDoc() {
151       public void hyperlinkUpdate(HyperlinkEvent JavaDoc event) {
152         if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
153           URL JavaDoc url = event.getURL();
154           jumpTo(url);
155         }
156       }
157     });
158
159     if (!allDocs) {
160       _hideNavigationPane();
161     }
162   }
163 }
164
Popular Tags