/jEdit/tags/jedit-4-0-pre8/macros/Misc/Write_HyperSearch_Results.bsh
# · Unknown · 215 lines · 198 code · 17 blank · 0 comment · 0 complexity · 715b185eb272e703ab9ccf9fb07f83a8 MD5 · raw file
- /*
- * Write_HyperSearch_Results.bsh - a BeanShell macro script
- * for the jEdit text editor - writes the contents of the
- * "HyperSearch Results" window to a new text buffer
- * Copyright (C) 2001 John Gellene
- * jgellene@nyc.rr.com
- * http://community.jedit.org
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with the jEdit program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- *
- * Notes on use:
- *
- * The macro operates by dumping the contents of the HyperSearch Results
- * window into a simple text report format. Because the HyperSearchResults
- * object does not know the search parameters that produced its data, the
- * parameters must be retrieved from the SearchAndReplace object. If those
- * parameters have changed since the HyperSearch, the report header will not be
- * accurate (although the body of the report will not be affected). A simple
- * test checks whether the HyperSearch flag is set before running the dumping
- * routines. To be completely reliable, the macro should be run immediately
- * after a HyperSearch.
- *
- * $Id: Write_HyperSearch_Results.bsh 4108 2002-03-23 03:20:26Z spestov $
- *
- * Checked for jEdit 4.0 API
- *
- */
- import java.text.SimpleDateFormat;
- import javax.swing.tree.*;
- void writeHyperSearchResults()
- {
- title = "Write HyperSearch results";
- hsearch = view.getDockableWindowManager()
- .getDockable("hypersearch-results");
- if(hsearch == null)
- {
- JOptionPane.showMessageDialog(view,
- "The \"HyperSearch Results\" window is not open.",
- title, JOptionPane.ERROR_MESSAGE);
- return;
- }
- if(jEdit.getBooleanProperty("search.hypersearch.toggle") == false)
- {
- answer = JOptionPane.showConfirmDialog(view,
- "Search settings have changed;\nthe report may not be accurate."
- + "\nDo you wish to proceed?",
- title, JOptionPane.YES_NO_OPTION);
- if(answer != JOptionPane.YES_OPTION) return;
- }
- jEdit.newFile(view);
- writeHeader();
- treeModel = hsearch.getTreeModel();
- root = treeModel.getRoot();
- childCount = root.getChildCount();
- if(childCount == 0)
- {
- textArea.setSelectedText(
- "Search term not found\n\nEnd of report\n");
- }
- else
- {
- fileNameNode = root.getFirstChild();
- for(int i = 0; i < childCount; ++i)
- {
- writeResultsForFile(fileNameNode);
- fileNameNode = fileNameNode.getNextSibling();
- }
- writeFooter();
- }
- }
- void writeResultsForFile( node)
- {
- node = (DefaultMutableTreeNode)node;
- if(node == null) return;
- childCount = node.getChildCount();
- if( childCount == 0) return;
- ++super.fileCount;
- super.hitCount += childCount;
- obj = node.getUserObject();
- sb.setLength(0);
- sb.append("Results for file:\n");
- sb.append(node.getUserObject().toString());
- sb.append("\n\n");
- lineNode = (DefaultMutableTreeNode)node.getFirstChild();
- if(lineNode == null) return;
- for( int i = 0; i < childCount; ++i)
- {
- if(lineNode == null)
- {
- sb.append(" Null node for i = " + String.valueOf(i));
- }
- else
- {
- sb.append("\tline ");
- sb.append(lineNode.getUserObject().toString());
- }
- sb.append('\n');
- lineNode = lineNode.getNextSibling();
- }
- sb.append("\nNumber of occurrences: ");
- sb.append(String.valueOf(childCount));
- sb.append("\n\n");
- textArea.setSelectedText(sb.toString());
- }
- void writeHeader()
- {
- sb.append("Search results for ");
- if(SearchAndReplace.getRegexp())
- sb.append("regular expression: ");
- else
- sb.append("search term: ");
- sb.append(SearchAndReplace.getSearchString());
- sb.append(" (case ");
- if(SearchAndReplace.getIgnoreCase())
- sb.append("in");
- sb.append("sensitive)\n");
- sb.append("Search conducted on ");
- sb.append(writeSearchFileSetType());
- sb.append('\n');
- sb.append("Report written on ");
- SimpleDateFormat f = new SimpleDateFormat("EE MMM d, yyyy h:mm a z");
- sb.append(f.format( new Date()));
- sb.append("\n\n");
- textArea.setSelectedText(sb.toString());
- }
- void writeFooter()
- {
- sb.setLength(0);
- sb.append("Total of ");
- sb.append(String.valueOf(hitCount));
- sb.append(" occurrences found in ");
- sb.append(String.valueOf(fileCount));
- sb.append(" files\n\nEnd of report\n");
- textArea.setSelectedText(sb.toString());
- }
- String writeSearchFileSetType()
- {
- result = new StringBuffer();
- fileSet = SearchAndReplace.getSearchFileSet();
- if(fileSet instanceof CurrentBufferSet)
- result.append("current buffer");
- else if(fileSet instanceof AllBufferSet)
- result.append("all open buffers with file mask '")
- .append(((AllBufferSet)fileSet).getFileFilter())
- .append('\'');
- else if(fileSet instanceof DirectoryListSet)
- {
- fileSet = (DirectoryListSet)fileSet;
- result.append("all files in \n")
- .append(fileSet.getDirectory())
- .append('\n');
- if(fileSet.isRecursive())
- result.append("(and subdirectories) ");
- result.append("with file mask '")
- .append(fileSet.getFileFilter())
- .append('\'');
- }
- else
- result.append("unknown file set");
- return result.toString();
- }
- sb = new StringBuffer();
- fileCount = 0;
- hitCount = 0;
- writeHyperSearchResults();
- /*
- Macro index data (in DocBook format)
- <listitem>
- <para><filename>Write_HyperSearch_Results.bsh</filename></para>
- <abstract><para>
- This macro writes the contents of the <quote>HyperSearch
- Results</quote> window to a new text buffer.
- </para></abstract>
- <para>
- The macro employs a simple text report format. Since
- the HyperSearch window's object does not maintain the search
- settings that produced the displayed results, the macro examines the
- current settings in the <classname>SearchAndReplace</classname> object.
- It confirms that the HyperSearch option is selected before writing
- the report. However, the only way to be sure that the report's contents
- are completely accurate is to run the macro immediately after a
- HyperSearch.
- </para>
- </listitem>
- */
- // end Write_HyperSearch_Results.bsh