PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/macros/Misc/Write_HyperSearch_Results.bsh

#
Unknown | 215 lines | 198 code | 17 blank | 0 comment | 0 complexity | 32fedb88ccd3a36c4276998ca2624d1c 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 3912 2001-11-24 03:56:51Z jgellene $
  36. *
  37. * Checked for jEdit 4.0 API
  38. *
  39. */
  40. import java.text.SimpleDateFormat;
  41. import javax.swing.tree.*;
  42. void writeHyperSearchResults()
  43. {
  44. title = "Write HyperSearch results";
  45. hsearch = view.getDockableWindowManager()
  46. .getDockableWindow("hypersearch-results");
  47. if(hsearch == null)
  48. {
  49. JOptionPane.showMessageDialog(view,
  50. "The \"HyperSearch Results\" window is not open.",
  51. title, JOptionPane.ERROR_MESSAGE);
  52. return;
  53. }
  54. if(jEdit.getBooleanProperty("search.hypersearch.toggle") == false)
  55. {
  56. answer = JOptionPane.showConfirmDialog(view,
  57. "Search settings have changed;\nthe report may not be accurate."
  58. + "\nDo you wish to proceed?",
  59. title, JOptionPane.YES_NO_OPTION);
  60. if(answer != JOptionPane.YES_OPTION) return;
  61. }
  62. jEdit.newFile(view);
  63. writeHeader();
  64. treeModel = hsearch.getTreeModel();
  65. root = treeModel.getRoot();
  66. childCount = root.getChildCount();
  67. if(childCount == 0)
  68. {
  69. textArea.setSelectedText(
  70. "Search term not found\n\nEnd of report\n");
  71. }
  72. else
  73. {
  74. fileNameNode = root.getFirstChild();
  75. for(int i = 0; i < childCount; ++i)
  76. {
  77. writeResultsForFile(fileNameNode);
  78. fileNameNode = fileNameNode.getNextSibling();
  79. }
  80. writeFooter();
  81. }
  82. }
  83. void writeResultsForFile( node)
  84. {
  85. node = (DefaultMutableTreeNode)node;
  86. if(node == null) return;
  87. childCount = node.getChildCount();
  88. if( childCount == 0) return;
  89. ++super.fileCount;
  90. super.hitCount += childCount;
  91. obj = node.getUserObject();
  92. sb.setLength(0);
  93. sb.append("Results for file:\n");
  94. sb.append(node.getUserObject().toString());
  95. sb.append("\n\n");
  96. lineNode = (DefaultMutableTreeNode)node.getFirstChild();
  97. if(lineNode == null) return;
  98. for( int i = 0; i < childCount; ++i)
  99. {
  100. if(lineNode == null)
  101. {
  102. sb.append(" Null node for i = " + String.valueOf(i));
  103. }
  104. else
  105. {
  106. sb.append("\tline ");
  107. sb.append(lineNode.getUserObject().toString());
  108. }
  109. sb.append('\n');
  110. lineNode = lineNode.getNextSibling();
  111. }
  112. sb.append("\nNumber of occurrences: ");
  113. sb.append(String.valueOf(childCount));
  114. sb.append("\n\n");
  115. textArea.setSelectedText(sb.toString());
  116. }
  117. void writeHeader()
  118. {
  119. sb.append("Search results for ");
  120. if(SearchAndReplace.getRegexp())
  121. sb.append("regular expression: ");
  122. else
  123. sb.append("search term: ");
  124. sb.append(SearchAndReplace.getSearchString());
  125. sb.append(" (case ");
  126. if(SearchAndReplace.getIgnoreCase())
  127. sb.append("in");
  128. sb.append("sensitive)\n");
  129. sb.append("Search conducted on ");
  130. sb.append(writeSearchFileSetType());
  131. sb.append('\n');
  132. sb.append("Report written on ");
  133. SimpleDateFormat f = new SimpleDateFormat("EE MMM d, yyyy h:mm a z");
  134. sb.append(f.format( new Date()));
  135. sb.append("\n\n");
  136. textArea.setSelectedText(sb.toString());
  137. }
  138. void writeFooter()
  139. {
  140. sb.setLength(0);
  141. sb.append("Total of ");
  142. sb.append(String.valueOf(hitCount));
  143. sb.append(" occurrences found in ");
  144. sb.append(String.valueOf(fileCount));
  145. sb.append(" files\n\nEnd of report\n");
  146. textArea.setSelectedText(sb.toString());
  147. }
  148. String writeSearchFileSetType()
  149. {
  150. result = new StringBuffer();
  151. fileSet = SearchAndReplace.getSearchFileSet();
  152. if(fileSet instanceof CurrentBufferSet)
  153. result.append("current buffer");
  154. else if(fileSet instanceof AllBufferSet)
  155. result.append("all open buffers with file mask '")
  156. .append(((AllBufferSet)fileSet).getFileFilter())
  157. .append('\'');
  158. else if(fileSet instanceof DirectoryListSet)
  159. {
  160. fileSet = (DirectoryListSet)fileSet;
  161. result.append("all files in \n")
  162. .append(fileSet.getDirectory())
  163. .append('\n');
  164. if(fileSet.isRecursive())
  165. result.append("(and subdirectories) ");
  166. result.append("with file mask '")
  167. .append(fileSet.getFileFilter())
  168. .append('\'');
  169. }
  170. else
  171. result.append("unknown file set");
  172. return result.toString();
  173. }
  174. sb = new StringBuffer();
  175. fileCount = 0;
  176. hitCount = 0;
  177. writeHyperSearchResults();
  178. /*
  179. Macro index data (in DocBook format)
  180. <listitem>
  181. <para><filename>Write_HyperSearch_Results.bsh</filename></para>
  182. <abstract><para>
  183. This macro writes the contents of the <quote>HyperSearch
  184. Results</quote> window to a new text buffer.
  185. </para></abstract>
  186. <para>
  187. The macro employs a simple text report format. Since
  188. the HyperSearch window's object does not maintain the search
  189. settings that produced the displayed results, the macro examines the
  190. current settings in the <classname>SearchAndReplace</classname> object.
  191. It confirms that the HyperSearch option is selected before writing
  192. the report. However, the only way to be sure that the report's contents
  193. are completely accurate is to run the macro immediately after a
  194. HyperSearch.
  195. </para>
  196. </listitem>
  197. */
  198. // end Write_HyperSearch_Results.bsh