001: /*
002: * JDKHelp.java
003: *
004: * Copyright (C) 1998-2002 Peter Graves
005: * $Id: JDKHelp.java,v 1.1.1.1 2002/09/24 16:08:06 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.awt.AWTEvent;
025: import java.awt.event.MouseEvent;
026: import java.util.List;
027:
028: public final class JDKHelp implements Constants {
029: public static void jdkHelp() {
030: final String className = getClassNameInCurrentEditor();
031: if (className != null && className.length() > 0)
032: jdkHelp(className);
033: }
034:
035: public static void jdkHelp(final String className) {
036: if (className == null || className.length() == 0)
037: return;
038: final String jdkDocPath = Editor.preferences()
039: .getStringProperty(Property.JDK_DOC_PATH);
040: if (jdkDocPath == null)
041: return;
042: final List dirnames = getDirectoriesInPath(jdkDocPath);
043: if (dirnames == null)
044: return;
045: final Editor editor = Editor.currentEditor();
046: final Frame frame = editor.getFrame();
047: final Buffer buffer = editor.getBuffer();
048: frame.setWaitCursor();
049: final String[] imports = JavaSource.getImports(buffer);
050: final int size = dirnames.size();
051: for (int i = 0; i < size; i++) {
052: final String dirname = (String) dirnames.get(i);
053: final File dir = File.getInstance(dirname);
054: File file = JavaSource.findImport(className, imports, dir,
055: ".html");
056: if (file == null || !file.isFile()) {
057: // Not found. Look in "api" subdirectory if it exists.
058: final File apiDir = File.getInstance(dir, "api");
059: if (apiDir != null && apiDir.isDirectory()) {
060: file = JavaSource.findImport(className, imports,
061: apiDir, ".html");
062: }
063: }
064: if (file != null && file.isFile()) {
065: Buffer buf = null;
066: // Look for existing buffer.
067: for (BufferIterator it = new BufferIterator(); it
068: .hasNext();) {
069: Buffer b = it.nextBuffer();
070: if (b instanceof WebBuffer
071: && b.getFile().equals(file)) {
072: buf = b;
073: break;
074: }
075: }
076: if (buf == null) {
077: buf = WebBuffer.createWebBuffer(file, null, null);
078: buf.setTransient(true);
079: }
080: if (editor.getBuffer() != buf) {
081: editor.makeNext(buf);
082: editor.activateInOtherWindow(buf);
083: }
084: frame.setDefaultCursor();
085: return;
086: }
087: }
088: frame.setDefaultCursor();
089: MessageDialog.showMessageDialog(editor,
090: "No help available for ".concat(className), "JDK Help");
091: }
092:
093: public static void source() {
094: final String className = getClassNameInCurrentEditor();
095: if (className != null && className.length() > 0)
096: source(className);
097: }
098:
099: public static void source(final String className) {
100: if (className == null || className.length() == 0)
101: return;
102: final Editor editor = Editor.currentEditor();
103: final Frame frame = editor.getFrame();
104: frame.setWaitCursor();
105: File file = JavaSource.findSource(editor.getBuffer(),
106: className, false);
107: if (file != null) {
108: Buffer buf = null;
109: // Look for existing buffer.
110: for (BufferIterator it = new BufferIterator(); it.hasNext();) {
111: Buffer b = it.nextBuffer();
112: if (file.equals(b.getFile())) {
113: buf = b;
114: break;
115: }
116: }
117: if (buf == null)
118: buf = Buffer.createBuffer(file);
119: if (editor.getBuffer() != buf) {
120: editor.makeNext(buf);
121: editor.activate(buf);
122: }
123: frame.setDefaultCursor();
124: } else {
125: frame.setDefaultCursor();
126: MessageDialog.showMessageDialog(editor,
127: "No source available for ".concat(className),
128: "Source");
129: }
130: }
131:
132: private static List getDirectoriesInPath(String path) {
133: final List dirNames = Utilities.getDirectoriesInPath(path);
134: final int size = dirNames.size();
135: if (size == 0) {
136: MessageDialog.showMessageDialog("Empty path", "Error");
137: return null;
138: }
139: for (int i = 0; i < size; i++) {
140: final String dirName = (String) dirNames.get(i);
141: final File dir = File.getInstance(dirName);
142: if (!dir.isDirectory()) {
143: FastStringBuffer sb = new FastStringBuffer(
144: "Directory \"");
145: sb.append(dir.canonicalPath());
146: sb.append("\" does not exist");
147: MessageDialog.showMessageDialog(sb.toString(), "Error");
148: }
149: }
150: return dirNames;
151: }
152:
153: private static String getClassNameInCurrentEditor() {
154: final Editor editor = Editor.currentEditor();
155: AWTEvent e = editor.getDispatcher().getLastEvent();
156: if (e instanceof MouseEvent)
157: editor.mouseMoveDotToPoint((MouseEvent) e);
158: String className = editor.getSelectionOnCurrentLine();
159: if (className == null || className.length() == 0)
160: className = getClassNameAtPosition(editor.getDot());
161: return className;
162: }
163:
164: // Supports both simple names ("String") and canonical names
165: // ("java.lang.String").
166: private static String getClassNameAtPosition(Position pos) {
167: if (pos == null)
168: return null;
169: final Line line = pos.getLine();
170: final int limit = line.length();
171: int offset = pos.getOffset();
172: if (offset >= limit)
173: return null;
174: char c = line.charAt(offset);
175: if (!Character.isJavaIdentifierPart(c) && c != '.')
176: return null;
177: // Go left until we encounter a character that can't be part of the name.
178: while (offset > 0) {
179: c = line.charAt(--offset);
180: if (!Character.isJavaIdentifierPart(c) && c != '.')
181: break;
182: }
183: // Go right to find start of name.
184: while (offset < limit
185: && !Character
186: .isJavaIdentifierStart(line.charAt(offset)))
187: ++offset;
188: if (offset == limit)
189: return null; // Nothing left.
190: // Now we're looking at the first char of the name.
191: Debug.assertTrue(Character.isJavaIdentifierStart(line
192: .charAt(offset)));
193: FastStringBuffer sb = new FastStringBuffer();
194: sb.append(line.charAt(offset));
195: while (++offset < limit) {
196: c = line.charAt(offset);
197: if (Character.isJavaIdentifierPart(c) || c == '.')
198: sb.append(c);
199: else
200: break; // Reached end.
201: }
202: return sb.toString();
203: }
204: }
|