/plugins/Javadoc/trunk/javadoc/JavadocPlugin.java

# · Java · 171 lines · 139 code · 10 blank · 22 comment · 38 complexity · 40780f50d86ce18704713cf66fd34958 MD5 · raw file

  1. package javadoc;
  2. //{{{ imports
  3. import java.util.StringTokenizer;
  4. import java.util.ArrayList;
  5. import java.io.File;
  6. import javax.swing.JOptionPane;
  7. import infoviewer.InfoViewerPlugin;
  8. import org.gjt.sp.jedit.ActionSet;
  9. import org.gjt.sp.jedit.EBMessage;
  10. import org.gjt.sp.jedit.EBPlugin;
  11. import org.gjt.sp.jedit.EditAction;
  12. import org.gjt.sp.jedit.EditPlugin;
  13. import org.gjt.sp.jedit.View;
  14. import org.gjt.sp.jedit.jEdit;
  15. import org.gjt.sp.jedit.MiscUtilities;
  16. import org.gjt.sp.jedit.Buffer;
  17. import org.gjt.sp.jedit.msg.PropertiesChanged;
  18. import org.gjt.sp.util.Log;
  19. //}}}
  20. public class JavadocPlugin extends EBPlugin {
  21. private static ActionSet apiCommands;
  22. public void start() {
  23. apiCommands = new ActionSet("Plugin: Javadoc - Api Trees");
  24. updateActions();
  25. }
  26. public void stop() {}
  27. public void handleMessage(EBMessage message) {
  28. if (message instanceof PropertiesChanged) {
  29. updateActions();
  30. }
  31. }
  32. public static EditAction getApiAction(String name) {
  33. return (EditAction) apiCommands.getAction(name);
  34. }
  35. public static void updateActions() {
  36. // TODO: This isn't quite working correctly, fix it
  37. jEdit.removeActionSet(apiCommands);
  38. apiCommands.removeAllActions();
  39. StringTokenizer tokenizer = new StringTokenizer(
  40. jEdit.getProperty("options.javadoc.path", ""), File.pathSeparator);
  41. while (tokenizer.hasMoreTokens()) {
  42. String token = tokenizer.nextToken();
  43. try {
  44. apiCommands.addAction(ApiAction.create(token));
  45. } catch (IllegalArgumentException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. jEdit.addActionSet(apiCommands);
  50. }
  51. /**
  52. * Searches the list for the given unqualified class name,
  53. * displaying it in InfoViewer if found
  54. * @param view the view
  55. * @param name the unqualified class name
  56. */
  57. public static void search(final View view, final String name) {
  58. view.getStatus().setMessage(jEdit.getProperty("msg.javadoc.searching"));
  59. // TODO: re-write this to use ThreadUtilities
  60. new Thread() {
  61. public void run() {
  62. String path = jEdit.getProperty("options.javadoc.path", "");
  63. StringTokenizer tokenizer = new StringTokenizer(path, File.pathSeparator);
  64. ArrayList<String> pathList = new ArrayList<String>();
  65. while (tokenizer.hasMoreTokens()) {
  66. String dir = tokenizer.nextToken();
  67. Buffer packageList = jEdit.openTemporary(view, dir, "package-list", false);
  68. if (packageList == null || packageList.isNewFile()) {
  69. Log.log(Log.ERROR, JavadocPlugin.class, "Invalid API root: "+dir);
  70. continue;
  71. }
  72. for (int i = 0; i<packageList.getLineCount(); i++) {
  73. String pkg = packageList.getLineText(i).replace(".", File.separator);
  74. File pkgDir = new File(dir, pkg);
  75. String pkgDirPath = pkgDir.getPath();
  76. String[] pages = pkgDir.list();
  77. for (int j = 0; j<pages.length; j++) {
  78. if (pages[j].equalsIgnoreCase(name+".html"))
  79. pathList.add(MiscUtilities.constructPath(pkgDirPath, pages[j]));
  80. }
  81. }
  82. }
  83. if (pathList.size() == 0) {
  84. view.getStatus().setMessageAndClear(jEdit.getProperty("msg.javadoc.not-found"));
  85. return;
  86. }
  87. String chosenDoc = null;
  88. if (pathList.size() > 1) {
  89. chosenDoc = (String) JOptionPane.showInputDialog(view,
  90. jEdit.getProperty("msg.javadoc.resolve-class.message"),
  91. jEdit.getProperty("msg.javadoc.resolve-class.title"),
  92. JOptionPane.QUESTION_MESSAGE, null, pathList.toArray(), pathList.get(0));
  93. } else {
  94. chosenDoc = pathList.get(0);
  95. }
  96. if (chosenDoc != null) {
  97. InfoViewerPlugin.openURL(view, new File(chosenDoc).toURI().toString());
  98. }
  99. view.getStatus().setMessage("");
  100. }
  101. }.start();
  102. }
  103. /**
  104. * Extracts the title of an api from its index.html file
  105. * @param path the root of the api tree
  106. */
  107. public static String getApiName(String path) {
  108. Buffer buffer = jEdit.openTemporary(jEdit.getActiveView(), path, "index.html", false);
  109. if (buffer == null || buffer.isNewFile()) {
  110. return null;
  111. }
  112. int start = -1, end = -1;
  113. for (int i = 0; i<buffer.getLineCount(); i++) {
  114. String line = buffer.getLineText(i).toLowerCase();
  115. int j = line.indexOf("<title>");
  116. if (start == -1 && j != -1) {
  117. start = buffer.getLineStartOffset(i)+j+7;
  118. }
  119. j = line.indexOf("</title>");
  120. if (start != -1 && end == -1 && j != -1) {
  121. end = buffer.getLineStartOffset(i)+j;
  122. }
  123. if (start != -1 && end != -1) {
  124. break;
  125. }
  126. }
  127. return buffer.getText(start, end-start).trim();
  128. }
  129. /**
  130. * Adds an api root to the list only if it is not already added
  131. * @param api the path of the api to add
  132. */
  133. public static void addApi(String api) {
  134. String path = jEdit.getProperty("options.javadoc.path", "");
  135. if (path.indexOf(api) != -1) {
  136. return;
  137. }
  138. if (path.length() > 0) {
  139. path += File.pathSeparator;
  140. }
  141. path += api;
  142. jEdit.setProperty("options.javadoc.path", path);
  143. jEdit.propertiesChanged();
  144. }
  145. /**
  146. * Removes an api root from the list
  147. * @param api the api to remove
  148. */
  149. public static void removeApi(String api) {
  150. String path = jEdit.getProperty("options.javadoc.path", "");
  151. int index;
  152. if ((index = path.indexOf(api)) == -1) {
  153. return;
  154. }
  155. String sep = File.pathSeparator;
  156. path = path.substring(0, index)+path.substring(index+api.length());
  157. path = path.replace(sep+sep, sep);
  158. jEdit.setProperty("options.javadoc.path", path);
  159. jEdit.propertiesChanged();
  160. }
  161. }