1 21 22 package org.armedbear.j; 23 24 import java.awt.AWTEvent ; 25 import java.awt.event.MouseEvent ; 26 import java.util.List ; 27 28 public final class JDKHelp implements Constants 29 { 30 public static void jdkHelp() 31 { 32 final String className = getClassNameInCurrentEditor(); 33 if (className != null && className.length() > 0) 34 jdkHelp(className); 35 } 36 37 public static void jdkHelp(final String className) 38 { 39 if (className == null || className.length() == 0) 40 return; 41 final String jdkDocPath = 42 Editor.preferences().getStringProperty(Property.JDK_DOC_PATH); 43 if (jdkDocPath == null) 44 return; 45 final List 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 [] imports = JavaSource.getImports(buffer); 53 final int size = dirnames.size(); 54 for (int i = 0; i < size; i++) { 55 final String dirname = (String ) 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 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 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 className = getClassNameInCurrentEditor(); 97 if (className != null && className.length() > 0) 98 source(className); 99 } 100 101 public static void source(final String 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 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 getDirectoriesInPath(String path) 134 { 135 final List 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 dirName = (String ) 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 getClassNameInCurrentEditor() 155 { 156 final Editor editor = Editor.currentEditor(); 157 AWTEvent e = editor.getDispatcher().getLastEvent(); 158 if (e instanceof MouseEvent ) 159 editor.mouseMoveDotToPoint((MouseEvent )e); 160 String className = editor.getSelectionOnCurrentLine(); 161 if (className == null || className.length() == 0) 162 className = getClassNameAtPosition(editor.getDot()); 163 return className; 164 } 165 166 private static String 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 while (offset > 0) { 182 c = line.charAt(--offset); 183 if (!Character.isJavaIdentifierPart(c) && c != '.') 184 break; 185 } 186 while (offset < limit && !Character.isJavaIdentifierStart(line.charAt(offset))) 188 ++offset; 189 if (offset == limit) 190 return null; 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; } 202 return sb.toString(); 203 } 204 } 205 | Popular Tags |