PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/macros/Misc/Write_HyperSearch_Results.bsh

#
Unknown | 274 lines | 256 code | 18 blank | 0 comment | 0 complexity | af5fbf3f1758215fabd9b412cf114b8b MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * Write_HyperSearch_Results.bsh - a BeanShell macro script
  3. * for the jEdit text editor - writes the contents of the
  4. * "HyperSearch Results" window to a new text buffer
  5. * Copyright (C) 2001 John Gellene
  6. * jgellene@nyc.rr.com
  7. * http://community.jedit.org
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with the jEdit program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. * Notes on use:
  24. *
  25. * The macro operates by dumping the contents of the HyperSearch Results
  26. * window into a simple text report format. Because the HyperSearchResults
  27. * object does not know the search parameters that produced its data, the
  28. * parameters must be retrieved from the SearchAndReplace object. If those
  29. * parameters have changed since the HyperSearch, the report header will not be
  30. * accurate (although the body of the report will not be affected). A simple
  31. * test checks whether the HyperSearch flag is set before running the dumping
  32. * routines. To be completely reliable, the macro should be run immediately
  33. * after a HyperSearch.
  34. *
  35. * $Id: Write_HyperSearch_Results.bsh 4422 2003-01-10 19:48:25Z spestov $
  36. *
  37. * Checked for jEdit 4.0 API
  38. * Adapted and checked for jEdit 4.1 API by Rudi Widmann
  39. *
  40. */
  41. import java.text.SimpleDateFormat;
  42. import javax.swing.tree.*;
  43. void writeHyperSearchResults()
  44. {
  45. title = "Write HyperSearch results";
  46. hsearch = view.getDockableWindowManager()
  47. .getDockable("hypersearch-results");
  48. if(hsearch == null)
  49. {
  50. JOptionPane.showMessageDialog(view,
  51. "The \"HyperSearch Results\" window is not open.",
  52. title, JOptionPane.ERROR_MESSAGE);
  53. return;
  54. }
  55. if(jEdit.getBooleanProperty("search.hypersearch.toggle") == false)
  56. {
  57. answer = JOptionPane.showConfirmDialog(view,
  58. "Search settings have changed;\nthe report may not be accurate."
  59. + "\nDo you wish to proceed?",
  60. title, JOptionPane.YES_NO_OPTION);
  61. if(answer != JOptionPane.YES_OPTION) return;
  62. }
  63. /*******************************************************************
  64. * Hyper search result tree structure
  65. * root
  66. * + searchNode
  67. * + fileNode
  68. * + results
  69. * + fileNode
  70. * + results
  71. * + searchNode
  72. * + fileNode
  73. * + results
  74. *******************************************************************/
  75. jEdit.newFile(view);
  76. writeHeader();
  77. treeModel = hsearch.getTreeModel();
  78. root = treeModel.getRoot();
  79. rootChildCount = root.getChildCount();
  80. if(rootChildCount == 0)
  81. {
  82. textArea.setSelectedText(
  83. "Search items not found\n\nEnd of report\n");
  84. }
  85. else
  86. {
  87. searchNode = root.getFirstChild();
  88. for(int i = 0; i < rootChildCount; ++i)
  89. {
  90. super.fileCount = 0;
  91. super.hitCount = 0;
  92. writeSearchHeader(searchNode);
  93. if (i == rootChildCount-1)
  94. writeSearchParameters(); // write only for last result
  95. super.searchCount++;
  96. searchChildCount = searchNode.getChildCount();
  97. if(searchChildCount == 0)
  98. {
  99. textArea.setSelectedText(
  100. "Search term not found\n\nEnd of report\n");
  101. }
  102. fileNode = searchNode.getFirstChild();
  103. for(int j = 0; j < searchChildCount; ++j)
  104. {
  105. writeResultsForFile(fileNode);
  106. fileNode = fileNode.getNextSibling();
  107. }
  108. searchNode = searchNode.getNextSibling();
  109. writeFileFooter();
  110. }
  111. writeFooter();
  112. }
  113. }
  114. void writeSearchHeader( node)
  115. {
  116. node = (DefaultMutableTreeNode)node;
  117. if(node == null) return;
  118. childCount = node.getChildCount();
  119. if( childCount == 0) return;
  120. sb.setLength(0);
  121. sb.append("Results for search item: \"");
  122. sb.append(node.getUserObject().toString());
  123. sb.append("\"\n\n");
  124. textArea.setSelectedText(sb.toString());
  125. }
  126. void writeResultsForFile( node)
  127. {
  128. node = (DefaultMutableTreeNode)node;
  129. if(node == null) return;
  130. childCount = node.getChildCount();
  131. if( childCount == 0) return;
  132. ++super.fileCount;
  133. super.hitCount += childCount;
  134. obj = node.getUserObject();
  135. sb.setLength(0);
  136. sb.append("\tMatched file:\n\t");
  137. sb.append(node.getUserObject().toString());
  138. sb.append("\n\n");
  139. lineNode = (DefaultMutableTreeNode)node.getFirstChild();
  140. if(lineNode == null) return;
  141. for( int i = 0; i < childCount; ++i)
  142. {
  143. if(lineNode == null)
  144. {
  145. sb.append("\t\tNull node for i = " + String.valueOf(i));
  146. }
  147. else
  148. {
  149. sb.append("\t\tline ");
  150. sb.append(lineNode.getUserObject().toString());
  151. }
  152. sb.append('\n');
  153. lineNode = lineNode.getNextSibling();
  154. }
  155. sb.append("\n\tNumber of occurrences: ");
  156. sb.append(String.valueOf(childCount));
  157. sb.append("\n\n");
  158. textArea.setSelectedText(sb.toString());
  159. }
  160. void writeHeader()
  161. {
  162. sb.append("Hypersearch report written on ");
  163. SimpleDateFormat f = new SimpleDateFormat("EE MMM d, yyyy h:mm a z");
  164. sb.append(f.format( new Date()));
  165. sb.append("\n\n");
  166. textArea.setSelectedText(sb.toString());
  167. }
  168. void writeSearchParameters()
  169. {
  170. sb.setLength(0);
  171. sb.append("\tUsed search parameters for ");
  172. if(SearchAndReplace.getRegexp())
  173. sb.append("regular expression: ");
  174. else
  175. sb.append("search term: ");
  176. sb.append(SearchAndReplace.getSearchString());
  177. sb.append(" (case ");
  178. if(SearchAndReplace.getIgnoreCase())
  179. sb.append("in");
  180. sb.append("sensitive)\n");
  181. sb.append("\tSearch conducted on ");
  182. sb.append(writeSearchFileSetType());
  183. sb.append('\n');
  184. sb.append('\n');
  185. textArea.setSelectedText(sb.toString());
  186. }
  187. void writeFileFooter()
  188. {
  189. sb.setLength(0);
  190. sb.append("\tTotal of ");
  191. sb.append(String.valueOf(hitCount));
  192. sb.append(" occurrences found in ");
  193. sb.append(String.valueOf(fileCount));
  194. sb.append(" files\n\n");
  195. textArea.setSelectedText(sb.toString());
  196. }
  197. void writeFooter()
  198. {
  199. sb.setLength(0);
  200. sb.append("Total of ");
  201. sb.append(String.valueOf(searchCount));
  202. sb.append(" search results reported \n\nEnd of report\n");
  203. textArea.setSelectedText(sb.toString());
  204. }
  205. String writeSearchFileSetType()
  206. {
  207. result = new StringBuffer();
  208. fileSet = SearchAndReplace.getSearchFileSet();
  209. if(fileSet instanceof CurrentBufferSet)
  210. result.append("current buffer");
  211. else if(fileSet instanceof AllBufferSet)
  212. result.append("all open buffers with file mask '")
  213. .append(((AllBufferSet)fileSet).getFileFilter())
  214. .append('\'');
  215. else if(fileSet instanceof DirectoryListSet)
  216. {
  217. fileSet = (DirectoryListSet)fileSet;
  218. result.append("all files in \n")
  219. .append(fileSet.getDirectory())
  220. .append('\n');
  221. if(fileSet.isRecursive())
  222. result.append("(and subdirectories) ");
  223. result.append("with file mask '")
  224. .append(fileSet.getFileFilter())
  225. .append('\'');
  226. }
  227. else
  228. result.append("unknown file set");
  229. return result.toString();
  230. }
  231. sb = new StringBuffer();
  232. searchCount = 0;
  233. fileCount = 0;
  234. hitCount = 0;
  235. writeHyperSearchResults();
  236. /*
  237. Macro index data (in DocBook format)
  238. <listitem>
  239. <para><filename>Write_HyperSearch_Results.bsh</filename></para>
  240. <abstract><para>
  241. This macro writes the contents of the <quote>HyperSearch
  242. Results</quote> window to a new text buffer.
  243. </para></abstract>
  244. <para>
  245. The macro employs a simple text report format. Since
  246. the HyperSearch window's object does not maintain the search
  247. settings that produced the displayed results, the macro examines the
  248. current settings in the <classname>SearchAndReplace</classname> object.
  249. It confirms that the HyperSearch option is selected before writing
  250. the report. However, the only way to be sure that the report's contents
  251. are completely accurate is to run the macro immediately after a
  252. HyperSearch.
  253. </para>
  254. </listitem>
  255. */
  256. // end Write_HyperSearch_Results.bsh