KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > JDKHelp


1 /*
2  * JDKHelp.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: JDKHelp.java,v 1.1.1.1 2002/09/24 16:08:06 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.awt.AWTEvent JavaDoc;
25 import java.awt.event.MouseEvent JavaDoc;
26 import java.util.List JavaDoc;
27
28 public final class JDKHelp implements Constants
29 {
30     public static void jdkHelp()
31     {
32         final String JavaDoc className = getClassNameInCurrentEditor();
33         if (className != null && className.length() > 0)
34             jdkHelp(className);
35     }
36
37     public static void jdkHelp(final String JavaDoc className)
38     {
39         if (className == null || className.length() == 0)
40             return;
41         final String JavaDoc jdkDocPath =
42             Editor.preferences().getStringProperty(Property.JDK_DOC_PATH);
43         if (jdkDocPath == null)
44             return;
45         final List JavaDoc dirnames = getDirectoriesInPath(jdkDocPath);
46         if (dirnames == null)
47             return;
48         final Editor editor = Editor.currentEditor();
49         final Frame frame = editor.getFrame();
50         final Buffer buffer = editor.getBuffer();
51         frame.setWaitCursor();
52         final String JavaDoc[] imports = JavaSource.getImports(buffer);
53         final int size = dirnames.size();
54         for (int i = 0; i < size; i++) {
55             final String JavaDoc dirname = (String JavaDoc) dirnames.get(i);
56             final File dir = File.getInstance(dirname);
57             File file = JavaSource.findImport(className, imports, dir,
58                 ".html");
59             if (file == null || !file.isFile()) {
60                 // Not found. Look in "api" subdirectory if it exists.
61
final File apiDir = File.getInstance(dir, "api");
62                 if (apiDir != null && apiDir.isDirectory()) {
63                     file = JavaSource.findImport(className, imports, apiDir,
64                         ".html");
65                 }
66             }
67             if (file != null && file.isFile()) {
68                 Buffer buf = null;
69                 // Look for existing buffer.
70
for (BufferIterator it = new BufferIterator(); it.hasNext();) {
71                     Buffer b = it.nextBuffer();
72                     if (b instanceof WebBuffer && b.getFile().equals(file)) {
73                         buf = b;
74                         break;
75                     }
76                 }
77                 if (buf == null) {
78                     buf = WebBuffer.createWebBuffer(file, null, null);
79                     buf.setTransient(true);
80                 }
81                 if (editor.getBuffer() != buf) {
82                     editor.makeNext(buf);
83                     editor.activateInOtherWindow(buf);
84                 }
85                 frame.setDefaultCursor();
86                 return;
87             }
88         }
89         frame.setDefaultCursor();
90         MessageDialog.showMessageDialog(editor,
91             "No help available for ".concat(className), "JDK Help");
92     }
93
94     public static void source()
95     {
96         final String JavaDoc className = getClassNameInCurrentEditor();
97         if (className != null && className.length() > 0)
98             source(className);
99     }
100
101     public static void source(final String JavaDoc className)
102     {
103         if (className == null || className.length() == 0)
104             return;
105         final Editor editor = Editor.currentEditor();
106         final Frame frame = editor.getFrame();
107         frame.setWaitCursor();
108         File file = JavaSource.findSource(editor.getBuffer(), className, false);
109         if (file != null) {
110             Buffer buf = null;
111             // Look for existing buffer.
112
for (BufferIterator it = new BufferIterator(); it.hasNext();) {
113                 Buffer b = it.nextBuffer();
114                 if (file.equals(b.getFile())) {
115                     buf = b;
116                     break;
117                 }
118             }
119             if (buf == null)
120                 buf = Buffer.createBuffer(file);
121             if (editor.getBuffer() != buf) {
122                 editor.makeNext(buf);
123                 editor.activate(buf);
124             }
125             frame.setDefaultCursor();
126         } else {
127             frame.setDefaultCursor();
128             MessageDialog.showMessageDialog(editor,
129                 "No source available for ".concat(className), "Source");
130         }
131     }
132
133     private static List JavaDoc getDirectoriesInPath(String JavaDoc path)
134     {
135         final List JavaDoc dirNames = Utilities.getDirectoriesInPath(path);
136         final int size = dirNames.size();
137         if (size == 0) {
138             MessageDialog.showMessageDialog("Empty path", "Error");
139             return null;
140         }
141         for (int i = 0; i < size; i++) {
142             final String JavaDoc dirName = (String JavaDoc) dirNames.get(i);
143             final File dir = File.getInstance(dirName);
144             if (!dir.isDirectory()) {
145                 FastStringBuffer sb = new FastStringBuffer("Directory \"");
146                 sb.append(dir.canonicalPath());
147                 sb.append("\" does not exist");
148                 MessageDialog.showMessageDialog(sb.toString(), "Error");
149             }
150         }
151         return dirNames;
152     }
153
154     private static String JavaDoc getClassNameInCurrentEditor()
155     {
156         final Editor editor = Editor.currentEditor();
157         AWTEvent JavaDoc e = editor.getDispatcher().getLastEvent();
158         if (e instanceof MouseEvent JavaDoc)
159             editor.mouseMoveDotToPoint((MouseEvent JavaDoc)e);
160         String JavaDoc className = editor.getSelectionOnCurrentLine();
161         if (className == null || className.length() == 0)
162             className = getClassNameAtPosition(editor.getDot());
163         return className;
164     }
165
166     // Supports both simple names ("String") and canonical names
167
// ("java.lang.String").
168
private static String JavaDoc getClassNameAtPosition(Position pos)
169     {
170         if (pos == null)
171             return null;
172         final Line line = pos.getLine();
173         final int limit = line.length();
174         int offset = pos.getOffset();
175         if (offset >= limit)
176             return null;
177         char c = line.charAt(offset);
178         if (!Character.isJavaIdentifierPart(c) && c != '.')
179             return null;
180         // Go left until we encounter a character that can't be part of the name.
181
while (offset > 0) {
182             c = line.charAt(--offset);
183             if (!Character.isJavaIdentifierPart(c) && c != '.')
184                 break;
185         }
186         // Go right to find start of name.
187
while (offset < limit && !Character.isJavaIdentifierStart(line.charAt(offset)))
188             ++offset;
189         if (offset == limit)
190             return null; // Nothing left.
191
// Now we're looking at the first char of the name.
192
Debug.assertTrue(Character.isJavaIdentifierStart(line.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 }
205
Popular Tags